Results 1 to 1 of 1

Math Help - plz help me with SIMPSON rule in Matlab

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    1

    plz help me with SIMPSON rule in Matlab

    Hi guys, i got a simpson's rule code, but i dont know how to use it.

    Code:
    function [integral,difference,ratio]=simpson(a,b,n0,index_f)
    %
    %  function [integral,difference,ratio]=simpson(a,b,n0,index_f)
    %
    %  This uses Simpson's rule with n subdivisions to integrate the function
    %  f over the interval [a,b].  The values of n used are
    %        n = n0,2*n0,4*n0,...,256*n0
    %  The value of n0 MUST be nonzero. 
    %  The corresponding numerical integrals are returned in the vector 
    %  integral.  The differences of successive numerical integrals are
    %  returned in the vector difference:
    %        difference(i) = integral(i)-integral(i-1),  i=2,...,9
    %  The entries in ratio give the rate of decrease in these
    %  differences.
    %
    %  In using this program, define the integrand using the 
    %  function given below.  The parameter index_f allows the user 
    %  to do calculations with multiple integrands.
    % Initialize output vectors.
    integral = zeros(9,1);
    difference = zeros(9,1);
    ratio = zeros(9,1);
    % Initialize for Simpson integration.
    sumend = f(a,index_f) + f(b,index_f);
    sumodd = 0;
    sumeven = 0;
    % Initialize for case of n0 > 2.
    if(n0 > 2)
        h = (b-a)/n0;
        for i=2:2:n0-2
            sumeven = sumeven + f(a+i*h,index_f);
        end
    end
    %  Calculate the numerical integrals, doing each
    %  by appropriately modifying the preceding case.
    for i=1:9
        n = (n0)*(2^(i-1));
        h = (b-a)/n;
        sumeven = sumeven + sumodd;
        sumodd = 0;
        for k=1:2:n-1
            sumodd = sumodd + f(a+k*h,index_f);
        end
        integral(i) = h*(sumend + 4*sumodd + 2*sumeven)/3;
    end
    %  Calculate the differences of the successive 
    %  Simpson rule integrals and the ratio
    %  of decrease in these differences.
    difference(2:9)=integral(2:9)-integral(1:8);
    ratio(3:9)=difference(2:8)./difference(3:9);
    
    
    function f_value = f(x,index)
    %
    % This defines the integrand.
    switch index
        case 1
            f_value = exp(-x.^20;
        case 2
            f_value=1./(1+x.^2)
        case 3
            f_value=1./(2+cos(x));
            
    end

    IF I want to use it to integrate f(x)= x.^(1/2) * exp(x) , how can i write in the command window?

    Thx
    Last edited by CaptainBlack; April 18th 2010 at 08:58 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. simpson's 1/3rd rule
    Posted in the Calculus Forum
    Replies: 1
    Last Post: March 21st 2010, 10:20 AM
  2. Matlab - Simpson's rule
    Posted in the Math Software Forum
    Replies: 1
    Last Post: June 8th 2009, 12:57 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