Fortran: Integrating Different Functions

In summary, a user is seeking advice on how to create a Fortran program that can integrate different functions based on user selection. They have already written a subroutine for integration and are looking for guidance on how to add the option for selecting a specific function.
  • #1
porkpie
1
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
  • #2
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.
 

What is Fortran and why is it useful for integrating different functions?

Fortran is a high-level programming language commonly used in scientific and engineering applications. It is useful for integrating different functions because it has built-in capabilities for handling mathematical computations and allows for efficient coding of complex mathematical algorithms.

How does Fortran handle integration of different functions?

Fortran uses built-in functions and subroutines to handle integration of different functions. These include routine libraries such as QUADPACK and the GNU Scientific Library, which provide efficient and accurate methods for numerical integration.

What are some common challenges when integrating different functions in Fortran?

Some common challenges when integrating different functions in Fortran include selecting the appropriate integration method for a specific function, dealing with numerical errors and precision, and optimizing the code for efficiency.

Can Fortran be used for symbolic integration of functions?

No, Fortran is primarily a numerical programming language and does not have built-in capabilities for symbolic integration. However, it can be used to numerically evaluate integrals using various methods.

Are there any limitations to using Fortran for integrating different functions?

One limitation of using Fortran for integrating different functions is that it may not be as user-friendly as other programming languages, such as Python or MATLAB. Additionally, some complex functions may require advanced numerical methods that may be challenging to implement in Fortran.

Similar threads

  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
953
  • Calculus and Beyond Homework Help
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
735
Back
Top