Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran - Functions

  1. Nov 17, 2008 #1
    Hi!
    Im using fortran and am trying to create a integrating program that is able to use different functions.

    I have written the subroutine that will integrate but would also like it to be able to use different functions determine by user selection.

    Here is my subroutine:
    subroutine comptrap
    implicit none
    double precision h,f,f1,f2,x,x1,x2,int
    double precision func1
    integer n,i
    print*, 'Enter x1, the lower boundary.'
    read*, x1
    print*, 'Enter x2, the upper boundary.'
    read*,x2
    print*, 'Enter the number of intervals, n.'
    read*, n
    h=(x2-x1)/n
    int=0
    do i=1,(n-1)

    x=x1+(h*i)
    f=func1(x)
    print*, f
    int=int+f
    end do
    f1=func1(x1)
    f2=func1(x2)
    int=h*(int+(f1+f2)/2)


    print*, f1,f2,'Integral=',int,h
    end subroutine

    And an example of a function:
    double precision function func1(x)
    implicit none
    double precision x
    func1=x**2
    end

    If anyone could advise me on how to program it to allow a function to be picked for integration that that would be great.

    Thanks in advance.
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?
Draft saved Draft deleted



Similar Discussions: Fortran - Functions
  1. Fortran 90 (Replies: 1)

  2. FORTRAN Help (Replies: 1)

  3. Fortran Help (Replies: 2)

  4. Infinity in fortran (Replies: 5)

  5. Linking Fortran (Replies: 5)

Loading...