Fortran: Integrating Different Functions

  • Context: Fortran 
  • Thread starter Thread starter porkpie
  • Start date Start date
  • Tags Tags
    Fortran Functions
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 3K views
porkpie
Messages
1
Reaction score
0
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.
 
on Phys.org
Add an argument to func1 to specify which function to integrate, then in func1 put only a select case to call the function that will contain the desired function definition.