Hi!(adsbygoogle = window.adsbygoogle || []).push({});

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.

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Fortran - Functions

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

**Physics Forums | Science Articles, Homework Help, Discussion**