Solving 2D Integrals Using Fortran 95 and the Trapezoidal Method

  • Thread starter CMJ96
  • Start date
  • Tags
    2d Method
In summary, the code attempts to use a nested for loop to approximate a two variable integral. However, the function values at the endpoints are not included and the trapezoidal method is not used.
  • #1
CMJ96
50
0

Homework Statement


I'd like to solve the following integral using the 2D trapezoidal method in fortran 95
$$I=\int^{1.40406704}_{-1.40406704} \int^{x+1.40406704}_{x-1.40406704} exp(x+y) dy dx$$

Homework Equations


$$I= \frac{h}{2} \left(f_0+ 2 \sum_{i=1}^{n-1} f_i +f_n \right)+O(h^2) $$

The Attempt at a Solution


So I have wrote my program below, attempting to use a nested do loop to calculate the integral over the two dimensions. However I am not sure how to use the result of my do loop to arrive at a final answer, I'm also not sure if my loop is entirely correct, here is a copy of my script

program trap2d
implicit none
real :: hy, hx, c, d, integralx, integraly, func, I_numeric,x,y,integral
integer :: nx,ny,ix,iy

c=-(1.140406704)
d=(1.140406704)
ny=1000
nx=1000
hx=(d-c)/nx
integralx=0.0

do ix=1,nx-1
x=c+ix*hx
hy=((x+1.140406704)-(x-1.140406704))/ny
integraly=0.0

do iy=1,ny-1
y=x-1.140406704+iy*hy
integraly=integraly+hy*func(x,y)
end do

integralx=integralx+hx*integraly
end do

end program

function func(x,y)
implicit none
real :: func,x,y
func=exp(x+y)
end function func
 
Physics news on Phys.org
  • #2
From what I follow in your code, it appears to be doing the integration sums, but I don't think it's doing trapezoidal. It appears to be just doing (left point) sums. I don't see it utilizing your relevant equation either. Can you go into a little more detail about your relevant equation, and how it's used to approximate the integral?
 
  • #3
Your code looks pretty good. I think you have forgotten to add in the function values at the endpoints (##f_0/2## and ##f_n/2##) in both in the inner and outer do loops. Aside from that, I think you need a print statement to show the value of integralx before your end program statement.
 
  • #4
scottdave said:
Can you go into a little more detail about your relevant equation
So the relevant equation breaks the integral up into trapeziums, the area of a trapezium is the following $$A=h\frac{f_i+f_{i+1}}{2} $$and sums them up to find an approximation, the more strips the more accurate.
The expansion of the integral goes along the following lines,
$$I= \frac{hf_0}{2}+hf_1+hf_2+...hf_{n-1} +\frac{h}{2}f_n$$
Just to clarify this is the composite 2D trapezoid method
tnich said:
I think you have forgotten to add in the function values at the endpoints (f0/2f0/2f_0/2 and fn/2fn/2f_n/2) in both in the inner and outer do loops
For a two variable function how do I define the end points? in my case would it be $$f(1.40406704, 2(1.40406704)) ,f(1.40406704, 0), f(-1.40406704,0), f(-1.40406704, 2(-1.40406704)) $$ ?
 
Last edited:
  • #5
Now I see it, thanks. So as @tnich pointed out, you would need the f0/2 and fn/2 to make it a trapezoidal method.
Then you need to print the variable integralx
 
  • #6
scottdave said:
Now I see it, thanks. So as @tnich pointed out, you would need the f0/2 and fn/2 to make it a trapezoidal method.
Then you need to print the variable integralx
How exactly do I define $$f_n$$ and $$f_0$$ in 2D?
 
  • #7
CMJ96 said:
How exactly do I define $$f_n$$ and $$f_0$$ in 2D?
You are doing nested 1D integrals. At each iteration in ix, you need to add the case for iy = 0 and iy = ny. Then, in addition to the loop over ix, you need code for ix = 0 and ix = nx.
 
  • Like
Likes scottdave
  • #8
Would adding in func(x, x+1.140406704) and func(x,x-1.140406704) both multiplied by a factor of hy/2 for the y loop, and would func(1.140406704,y) and func(-1.140406704,y) multiplied by hx/2 work for the x loop?
 
  • #9
CMJ96 said:
Would adding in func(x, x+1.140406704) and func(x,x-1.140406704) both multiplied by a factor of hy/2 for the y loop, and would func(1.140406704,y) and func(-1.140406704,y) multiplied by hx/2 work for the x loop?
In the second case, you still have to loop over y.

The simplest way to do it (from a programmer's point of view) is to extend the loops to 0 to nx (and 0 to ny), and use an if statement to set the multiplication factor to hx or hx/2 (and correspondingly hy or hy/2) depending in the value of ix (iy).
 
Last edited:
  • Like
Likes scottdave and CMJ96
  • #10
Thank you so much guys, you've helped a lot, my integral now works to within 0.01 of the analytical solution. I appreciate the help so much :)
 

1. What is the 2D Trapezoidal Method?

The 2D Trapezoidal Method is a numerical integration technique used to approximate the area under a curve in a two-dimensional space. It is commonly used in physics, engineering, and other fields to solve problems involving the integration of a function over a specific region.

2. How does the 2D Trapezoidal Method work?

The 2D Trapezoidal Method divides the region under the curve into a series of trapezoids, with the top and bottom edges of each trapezoid aligned with the curve. The area of each trapezoid is then calculated using the formula for the area of a trapezoid, and the sum of these areas is taken as an approximation of the total area under the curve.

3. What are the advantages of using the 2D Trapezoidal Method?

The 2D Trapezoidal Method is relatively easy to implement and can provide accurate results for a wide range of functions. It is also more accurate than other numerical integration methods, such as the midpoint and Simpson's rule, when used on functions with steep curves or sharp corners.

4. What are the limitations of the 2D Trapezoidal Method?

The 2D Trapezoidal Method can only provide an approximation of the area under a curve, and the accuracy of this approximation depends on the number of trapezoids used. It is also not suitable for functions that have discontinuities or infinite limits. Additionally, the method may become computationally expensive for complex functions or large numbers of trapezoids.

5. How can the accuracy of the 2D Trapezoidal Method be improved?

The accuracy of the 2D Trapezoidal Method can be improved by increasing the number of trapezoids used in the approximation. Additionally, using a smaller interval size and incorporating more advanced techniques, such as adaptive quadrature, can also improve accuracy. It is important to note that the accuracy of the method is also limited by the smoothness of the function being integrated.

Similar threads

  • Programming and Computer Science
Replies
4
Views
615
  • Calculus and Beyond Homework Help
Replies
10
Views
476
  • Programming and Computer Science
Replies
1
Views
943
  • Calculus and Beyond Homework Help
Replies
3
Views
571
  • Calculus and Beyond Homework Help
Replies
7
Views
707
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • Differential Equations
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
750
Replies
1
Views
911
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
Back
Top