bdubs-astro / NumIntSimpson

numerical integration using Simpson's Rule

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Numerical Integration

Performs numerical integration on a data set in the form of a txt file. Uses Simpson's Rule where applicable, and the Trapezoidal Rule in all other cases.


Simpson's 1/3 Rule:

Approximates a function using 2nd order polynomials.

For n equal segments (i.e., n+1 evenly spaced data points), the definite integral can be approximated as follows. Note that n must be even.

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



Simpson's 3/8 Rule:

Approximates a function using 3rd order polynomials.

For n equal segments (i.e., n+1 evenly spaced data points), the definite integral can be approximated as follows. Note that n must be an integer multiple of 3.

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



Trapezoidal Rule:

Approximates a function using trapezoids.

For n equal segments (i.e., n+1 evenly spaced data points), the definite integral can be approximated as follows.

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



Test code:

for Npts = logspace(1, 4, 4) + 1
    x = pi*linspace(0, 1, Npts);
    y = sin(x);
    [method, result] = Simpson(x, y);
    fprintf('n = %-6d \tTrapz = %.6f \t%s = %.6f\n', Npts-1, trapz(x, y), method, result)
end

n = 10     	Trapz = 1.983524 	Simpson's 1/3 Rule = 2.000110
n = 100    	Trapz = 1.999836 	Simpson's 1/3 Rule = 2.000000
n = 1000   	Trapz = 1.999998 	Simpson's 1/3 Rule = 2.000000
n = 10000  	Trapz = 2.000000 	Simpson's 1/3 Rule = 2.000000
for Npts = logspace(1, 4, 4)
    x = pi*linspace(0, 1, Npts);
    y = sin(x);
    [method, result] = Simpson(x, y);
    fprintf('n = %-6d \tTrapz = %.6f \t%s = %.6f\n', Npts-1, trapz(x, y), method, result)
end

n = 9      	Trapz = 1.979651 	Simpson's 3/8 Rule = 2.000382
n = 99     	Trapz = 1.999832 	Simpson's 3/8 Rule = 2.000000
n = 999    	Trapz = 1.999998 	Simpson's 3/8 Rule = 2.000000
n = 9999   	Trapz = 2.000000 	Simpson's 3/8 Rule = 2.000000

Resources:

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

About

numerical integration using Simpson's Rule


Languages

Language:MATLAB 100.0%