×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

fortran question

fortran question

RE: fortran question

(OP)
and yb=0.1
    yt=0.005

RE: fortran question

Quote (Karkouri):


..have the following and want it to put it in fortran..
When you want to to put it in fortran or any other programming language you need first to know how you want to compute it, also the algorithm.
There are several formulas for numerical integration, for example the Newton–Cotes formulas

 

RE: fortran question

(OP)
ok but i cant learn fortran in one day .
can you just tell me the algorithm for my example?

RE: fortran question

Hi Karkouri,

What you ask is not a fortran speciifc question. It's more a basic question from numerical mathematics. You should be clear how the numerical integration works and what method you have to choose. Then try to code it and post your code here.

This is a free help forum, but not a free coding service.
You should first show some effort. Try to code self something and if you have a problem post your code here. Then we try to help you.

You can google for infos about numerical integration incl. fortran code. E.g. here is something:
http://oregonstate.edu/instruct/ch490/lessons/lesson11.htm

 

RE: fortran question

(OP)
mirkom , my problem is not to find the answer using mathematics (i already did this). I just want to solve this problem using fortran with any method. As i 'm not expert in this programming language , i cannot write any algorithm.
i dont even know any commands in fortran.

I m asking if anyone knows how to type my example in fortran so as to get a result.
I didnt ask something so tragic imo , some help maybe by anyone?


 

RE: fortran question

Hi Karkouri,

Here is an example for you taken from the pseudo (python) code on wikipedia
http://en.wikipedia.org/wiki/Trapezoid_rule
http://en.wikipedia.org/wiki/Simpson%27s_rule

integration.f95

CODE

program integration
 implicit none
 external f1, f2 ! define function to be integrated as external
real :: integral
 ! integrating he function f1
 integral=trapezoid_rule(f1, 0.0, 1.0, 100)
 write (*,*) 'Trapezoid rule = ', integral
 integral=simpson_rule(f1, 0.0, 1.0, 100)
 write (*,*) "Simpson's rule = ", integral

 ! integrating the function f2
 integral=trapezoid_rule(f2, 0.005, 0.1, 100)
 write (*,*) 'Trapezoid rule = ', integral
 integral=simpson_rule(f2, 0.005, 0.1, 100)
 write (*,*) "Simpson's rule = ", integral

contains
! ********************** Functions/Procedures *************************
real function trapezoid_rule(f, a, b, n)
  ! Approximate the definite integral of f from a to b by the
  ! composite trapezoidal rule, using N subintervals
  ! see: <a href="http://en.wikipedia.org/wiki/Trapezoid_rule">http://en.wikipedia.org/wiki/Trapezoid_rule</a>
  ! function arguments ---------------------
  ! f: real function
  real :: f
  ! [a, b] : the interval of integration
  real, intent(in) ::  a, b
  ! n : number of subintervals used
  integer, intent(in) :: n
  ! ----------------------------------------
  ! temporary variables
  integer :: k
  real :: s
  s = 0
  do k=1, n-1
    s = s + f(a + (b-a)*k/n)
  end do  
  trapezoid_rule = (b-a) * (f(a)/2 + f(b)/2 + s) / n
end function trapezoid_rule

real function simpson_rule(f, a, b, n)
  ! Approximate the definite integral of f from a to b by the
  ! composite Simpson's rule, using N subintervals
  ! see: <a href="http://en.wikipedia.org/wiki/Simpson%27s_rule">http://en.wikipedia.org/wiki/Simpson%27s_rule</a>
  ! function arguments ---------------------
  ! f: real function
  real :: f
  ! [a, b] : the interval of integration
  real, intent(in) ::  a, b
  ! n : number of subintervals used
  integer, intent(in) :: n
  ! ----------------------------------------
  ! temporary variables
  integer :: k
  real :: s
  s = 0
  do k=1, n-1
    s = s + 2**(mod(k,2)+1) * f(a + (b-a)*k/n)
  end do  
  simpson_rule = (b-a) * (f(a) + f(b) + s) / (3*n)
end function simpson_rule

end program integration

! define here your own functions to be integrated
real function f1(x)
  implicit none
  real, intent(in) :: x
  f1 = x*x
end function f1

real function f2(y)
  implicit none
  real, intent(in) :: y
  f2 = 1/(y-(0.55*y**2 + 0.0165*y + 0.00012375))
end function f2
Output:

CODE

$ g95 integration.f95 -o integration

$ integration
 Trapezoid rule =  0.33335
 Simpson's rule =  0.3333334
 Trapezoid rule =  3.1299372
 Simpson's rule =  3.1267815
The first funcion f1(x)=x^2 which integrated on [0,1] gives exactly 1/3. You can compare here the accuracy of the 2 methods (trapezoid and Simpson's rule)
The secon function is yours. You can increase the value of n to get better accuracy. The result seems to be 3.1267..
 

RE: fortran question

What I posted above compiles fine with g95 but doesn't compile with gfortran because of these errors

CODE

$ gfortran integration.f95 -o integration
integration.f95:6.25:

 integral=trapezoid_rule(f1, 0.0, 1.0, 100)
                        1
Error: Type/rank mismatch in argument 'f' at (1)
integration.f95:8.23:

 integral=simpson_rule(f1, 0.0, 1.0, 100)
                      1
Error: Type/rank mismatch in argument 'f' at (1)
integration.f95:12.25:
So, here is another version which uses modules. I created 2 modules: one for the numerical methods and other for the user defined functions.
This version compiles fine with both compilers

integration.f95

CODE

module integration_methods
  ! This module contains the integration methods  
contains
  real function trapezoid_rule(f, a, b, n)
    ! Approximate the definite integral of f from a to b by the
    ! composite trapezoidal rule, using N subintervals
    ! see: <a href="http://en.wikipedia.org/wiki/Trapezoid_rule">http://en.wikipedia.org/wiki/Trapezoid_rule</a>
    ! function arguments ---------------------
    ! f: real function
    real :: f
    ! [a, b] : the interval of integration
    real, intent(in) ::  a, b
    ! n : number of subintervals used
    integer, intent(in) :: n
    ! ----------------------------------------
    ! temporary variables
    integer :: k
    real :: s
    s = 0
    do k=1, n-1
      s = s + f(a + (b-a)*k/n)
    end do  
    trapezoid_rule = (b-a) * (f(a)/2 + f(b)/2 + s) / n
  end function trapezoid_rule

  real function simpson_rule(f, a, b, n)
    ! Approximate the definite integral of f from a to b by the
    ! composite Simpson's rule, using N subintervals
    ! see: <a href="http://en.wikipedia.org/wiki/Simpson%27s_rule">http://en.wikipedia.org/wiki/Simpson%27s_rule</a>
    ! function arguments ---------------------
    ! f: real function
    real :: f
    ! [a, b] : the interval of integration
    real, intent(in) ::  a, b
    ! n : number of subintervals used
    integer, intent(in) :: n
    ! ----------------------------------------
    ! temporary variables
    integer :: k
    real :: s
    s = 0
    do k=1, n-1
      s = s + 2**(mod(k,2)+1) * f(a + (b-a)*k/n)
    end do  
    simpson_rule = (b-a) * (f(a) + f(b) + s) / (3*n)
  end function simpson_rule
end module integration_methods

module user_functions
  ! Define here your own functions to be integrated
contains
  real function f1(x)
    implicit none
    real, intent(in) :: x
    f1 = x*x
  end function f1

  real function f2(y)
    implicit none
    real, intent(in) :: y
    f2 = 1/(y-(0.55*y**2 + 0.0165*y + 0.00012375))
  end function f2
end module user_functions

program integration
  use integration_methods
  use user_functions
  
  implicit none
  real :: integral

  ! integrating he function f1
  integral=trapezoid_rule(f1, 0.0, 1.0, 1000)
  write (*,*) 'Trapezoid rule = ', integral
  integral=simpson_rule(f1, 0.0, 1.0, 1000)
  write (*,*) "Simpson's rule = ", integral

  ! integrating the function f2
  integral=trapezoid_rule(f2, 0.005, 0.1, 1000)
  write (*,*) 'Trapezoid rule = ', integral
  integral=simpson_rule(f2, 0.005, 0.1, 1000)
  write (*,*) "Simpson's rule = ", integral
end program integration
Output:

CODE

$ g95 integration.f95 -o integration

$ integration
 Trapezoid rule =  0.33333343
 Simpson's rule =  0.33333328
 Trapezoid rule =  3.1267667
 Simpson's rule =  3.126737

$ gfortran integration.f95 -o integration

$ integration
 Trapezoid rule =   0.33333343    
 Simpson's rule =   0.33333328    
 Trapezoid rule =    3.1267667    
 Simpson's rule =    3.1267371    

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close