Results 1 to 2 of 2

Math Help - Matlab - Simpson's rule

  1. #1
    Member
    Joined
    May 2008
    Posts
    171

    Matlab - Simpson's rule

    Hey guys.
    I have the following matlab code

    function y = simpson(f,a,b,n)
    %SIMPSON Simpson's rule integration with equally spaced points
    %
    % y=SIMPSON(f,a,b,n) returns the Simpson's rule approximation to
    % the integral of f(x) over the interval [a,b] using n+1 equally
    % spaced points. The input variable f is a string containing the
    % name of a function of one variable. The function f(x) must accept
    % a vector argument and return the vector of values of the function.
    %
    % NOTE: n must be even.

    h=(b-a)/n;
    x=linspace(a,b,n+1);
    fx=feval(f,x);

    y=h/3*(fx(1)+4*sum(fx(2:2:n))+2*sum(fx(3:2:n-1))+fx(n+1));

    It's the Simpson's rule in matlab.
    I want to run the following code with the function y = 1/(1+x^4), how can I do it from the command line?

    Thanks.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by asi123 View Post
    Hey guys.
    I have the following matlab code

    function y = simpson(f,a,b,n)
    %SIMPSON Simpson's rule integration with equally spaced points
    %
    % y=SIMPSON(f,a,b,n) returns the Simpson's rule approximation to
    % the integral of f(x) over the interval [a,b] using n+1 equally
    % spaced points. The input variable f is a string containing the
    % name of a function of one variable. The function f(x) must accept
    % a vector argument and return the vector of values of the function.
    %
    % NOTE: n must be even.

    h=(b-a)/n;
    x=linspace(a,b,n+1);
    fx=feval(f,x);

    y=h/3*(fx(1)+4*sum(fx(2:2:n))+2*sum(fx(3:2:n-1))+fx(n+1));

    It's the Simpson's rule in matlab.
    I want to run the following code with the function y = 1/(1+x^4), how can I do it from the command line?

    Thanks.
    simpson(@(x) 1./(1+x.^4),0,1,4)

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. plz help me with SIMPSON rule in Matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: April 18th 2010, 04:42 PM
  2. Using Simpson's Rule For sin x / x
    Posted in the Calculus Forum
    Replies: 10
    Last Post: September 17th 2009, 10:19 PM
  3. Replies: 0
    Last Post: October 20th 2008, 07:12 PM
  4. Trapezoid Rule/Simpson's Rule
    Posted in the Calculus Forum
    Replies: 1
    Last Post: May 16th 2007, 06:52 PM
  5. Replies: 3
    Last Post: August 31st 2006, 09:08 AM

Search Tags


/mathhelpforum @mathhelpforum