Fortran Fortran: Integrating Different Functions

  • Thread starter Thread starter porkpie
  • Start date Start date
  • Tags Tags
    Fortran Functions
AI Thread Summary
The discussion centers on creating a Fortran program that integrates different user-selected functions. The user has shared a subroutine for the trapezoidal integration method but seeks advice on modifying it to allow for the selection of various functions for integration. The current implementation includes a specific function, func1, which calculates the square of x. To enhance flexibility, a suggestion is made to add an argument to func1 that specifies which function to integrate, utilizing a select case structure to call the appropriate function definition based on user input. This approach would enable the integration of multiple functions within the same program.
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.
 
Technology news 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
8
Views
4K
Replies
5
Views
3K
Replies
8
Views
2K
Replies
12
Views
1K
Replies
6
Views
2K
Replies
13
Views
3K
Replies
11
Views
2K
Back
Top