How to Compute an Integral Using FORTRAN?

Click For Summary
SUMMARY

This discussion focuses on computing integrals using FORTRAN with three numerical methods: the composite rectangle method, trapezoidal rule, and Simpson's rule. The integral in question is \int_{0}^{1.8} f(x) dx with specified step sizes of h=0.3 and h=0.4. Key insights include the necessity of ensuring that the step size divides the interval evenly and the recommendation to implement separate functions for each numerical method in FORTRAN. The discussion emphasizes the importance of correctly calculating the number of subintervals and using appropriate methods based on the data points available.

PREREQUISITES
  • Understanding of numerical integration methods: composite rectangle method, trapezoidal rule, and Simpson's rule.
  • Familiarity with FORTRAN programming language.
  • Knowledge of how to handle arrays and user input in programming.
  • Basic calculus concepts, particularly integration and step sizes.
NEXT STEPS
  • Implement the composite rectangle method in FORTRAN for numerical integration.
  • Learn how to apply the composite trapezoidal rule in FORTRAN, ensuring correct handling of step sizes.
  • Research the implementation of Simpson's 1/3 rule in FORTRAN for numerical integration.
  • Explore file handling in FORTRAN to read function values for f(x) dynamically.
USEFUL FOR

Students and professionals in computational mathematics, software developers working with numerical methods, and anyone interested in implementing numerical integration techniques in FORTRAN.

Chris Red
Messages
1
Reaction score
0
Thread moved from a technical section, so is missing the homework template
I have a question set for programming to compute integral for the following unknown function using composite rectangle method, trapezoidal rule and simpsons rule.

\int_{0}^{1.8} f(x) dx

for h=0.3 and h=0.4

I have a table of results for f(x) and x as follows
x=0, 0.3, 0.6, 0.9, 0.12, 0.15, 0.18
f(x)=0.5, 0.6, 0.8, 1.3, 2, 3.2, 4.8

My interval is 6 using the formula n=1.8-0/0.3 for the first case. I am just wondering how I should approach this problem in my programming.

For the rectangle method does anyone have a step by step guide of how this would be done analytically. Surely this would also require the user to enter all values for f(x) too or they be read from a file in some way? I am mainly struggling because I am not sure how to implement this into my program which is in FORTRAN. I have not even thought about how to tackle the trapezoidal or simpson rule yet! Ideally they should be all within one program where the user selects the method of approximation.

Thanks for any advice
 
Physics news on Phys.org
Chris Red said:
I have a question set for programming to compute integral for the following unknown function using composite rectangle method, trapezoidal rule and simpsons rule.
I don't know the composite rectangle method. I'm familiar with using rectangles, where you use either the left endpoint of each subinterval or the right endpoint.
Chris Red said:
\int_{0}^{1.8} f(x) dx

for h=0.3 and h=0.4

I have a table of results for f(x) and x as follows
x=0, 0.3, 0.6, 0.9, 0.12, 0.15, 0.18
f(x)=0.5, 0.6, 0.8, 1.3, 2, 3.2, 4.8
Are you also given a table of results for h = 0.4? Also, a stepsize of 0.4 is problematic, since 0.4 doesn't divide 1.8 evenly.
Chris Red said:
My interval is 6
No, the number of subintervals is 6.
Chris Red said:
using the formula n=1.8-0/0.3 for the first case. I am just wondering how I should approach this problem in my programming.
The right side of your formula should be written as (1.8 - 0)/0.3. Otherwise, due to the order of operations, what you wrote is 1.8 - (0/0.3) = 1.8.
Chris Red said:
For the rectangle method does anyone have a step by step guide of how this would be done analytically.
?
Are you asking how you should do the calculations? The term analytically implies the opposite of integrating numerically; i.e., by finding the antiderivative of your function and evaluating it at the two endpoints.
Chris Red said:
Surely this would also require the user to enter all values for f(x) too or they be read from a file in some way? I am mainly struggling because I am not sure how to implement this into my program which is in FORTRAN. I have not even thought about how to tackle the trapezoidal or simpson rule yet! Ideally they should be all within one program where the user selects the method of approximation.

Thanks for any advice
 
For ##h = 0.3##.

You should numerically estimate the integral using the composite trapezoidal rule. This will give you a good idea of the the correct answer at first.

Then, you should notice that the step size causes there to be an even number of segments (6) and an odd number of data points (7). This tells you that a Simpson's 1/3 approach would be most appropriate.

Write functions in FORTRAN that return the values for the respective methods (trapezoidal/Simpson's).

For ##h = 0.4##.

There is a serious problem. You can't even numerically estimate the integral with the composite trapezoidal rule using unequal step sizes for each segment. This is because the step size makes your data obsolete.

A step size of ##0.6## would prove more interesting.
 

Similar threads

Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K