Fortran Fortran - Functions

  • Thread starter porkpie
  • Start date
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.
 

DrClaude

Mentor
6,807
2,923
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.
 

Want to reply to this thread?

"Fortran - Functions" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top