Skip to content

numerical integration using the Trapezoidal Rule

Notifications You must be signed in to change notification settings

bdubs-astro/NumInt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Numerical Integration

Compares the results of the trapezoid method with a simple summation.

For N+1 evenly spaced data points (i.e., N equal segments), the definite integral can be approximated by:

Here (b-a)/N is the spacing between data points.

Note that this equates to a simple summation multiplied by the data point spacing, minus a correction term:
( f(1) + f(end) )/2

If the spacing between the data points is not constant, the formula generalizes to:

Sample output:



Test code:

for N = logspace(1, 4, 4)
    x = pi*linspace(0, 1, N+1);
    y = sin(x);
    fprintf('N = %-6d \tTrapz = %.6f\n', N, trapz(x, y))
end
N = 10     	Trapz = 1.983524
N = 100    	Trapz = 1.999836
N = 1000   	Trapz = 1.999998
N = 10000  	Trapz = 2.000000

Resources:

https://www.mathworks.com/help/matlab/ref/trapz.html

https://web.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section%208%20Integration.pdf

Releases

No releases published

Packages

No packages published

Languages