Fortran: Integrating Different Functions

  • Context: Fortran 
  • Thread starter Thread starter porkpie
  • Start date Start date
  • Tags Tags
    Fortran Functions
Click For Summary
SUMMARY

This discussion focuses on creating a Fortran program that integrates different mathematical functions based on user selection. The user has implemented a subroutine named comptrap for the trapezoidal integration method but seeks guidance on modifying the func1 function to allow for multiple function definitions. The suggested solution involves adding an argument to func1 and utilizing a SELECT CASE statement to determine which function to integrate. This approach enables flexibility in function selection during integration.

PREREQUISITES
  • Fortran programming language knowledge
  • Understanding of numerical integration techniques, specifically the trapezoidal rule
  • Familiarity with subroutines and functions in Fortran
  • Basic knowledge of control structures, such as SELECT CASE
NEXT STEPS
  • Implement a SELECT CASE structure in Fortran to handle multiple function definitions
  • Explore advanced numerical integration methods beyond the trapezoidal rule
  • Learn about function pointers or equivalent in Fortran for dynamic function selection
  • Investigate error handling techniques in Fortran for user input validation
USEFUL FOR

This discussion is beneficial for Fortran developers, numerical analysts, and anyone interested in implementing flexible mathematical integration solutions in their programs.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K