Coder Social home page Coder Social logo

bdubs-astro / numintgrl Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 830 KB

MATLAB function that performs numerical integration using Simpson's Rule where possible, and the Trapezoidal Rule otherwise

MATLAB 100.00%
integration numerical-integration simpson-method simpson-rule trapezoidal-method trapezoidal-rule

numintgrl's Introduction

Numerical Integration

Performs numerical integration on x-y data. Uses Simpson's Rule where applicable, and the Trapezoidal Rule in all other cases.

Returns the result, as well as a string indicating the method used.

    [method, result] = NumIntgrl(x, y);

Methods

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] = NumIntgrl(x, y);
    fprintf('n = %-6d \tTrapz = %.6f \t%s = %.6f\n', Npts-1, trapz(x, y), method, result)
end

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

n = 9      	Trapz = 1.979651 	Simp 3/8 = 2.000382
n = 99     	Trapz = 1.999832 	Simp 3/8 = 2.000000
n = 999    	Trapz = 1.999998 	Simp 3/8 = 2.000000
n = 9999   	Trapz = 2.000000 	Simp 3/8 = 2.000000
for Npts = logspace(1, 4, 4) + 2
    x = pi*linspace(0, 1, Npts);
    y = sin(x);
    [method, result] = NumIntgrl(x, y);
    fprintf('n = %-6d \tTrapz = %.6f \t%s = %.6f\n', Npts-1, trapz(x, y), method, result)
end

n = 11     	Trapz = 1.986387 	Trap     = 1.986387
n = 101    	Trapz = 1.999839 	Trap     = 1.999839
n = 1001   	Trapz = 1.999998 	Trap     = 1.999998
n = 10001  	Trapz = 2.000000 	Trap     = 2.000000

Resources:

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

numintgrl's People

Contributors

bdubs-astro avatar bdwissman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.