Monday, February 25, 2013

MATLAB - Simpson's 3/8 rule

simpson38
% ========================================================================
% ***************** Integration by Simpson's 3/8 method ******************
% *************************** By Mahesha MG ******************************
% Date: 25/02/2013
% ========================================================================
display('Equation is x^2+x-2')
xl=input('Enter lower limit:');
xu=input('Enter upper upper limit: ');
n=input('Enter number of subintervals: ');
% ========================================================================
h=(xu-xl)/n;
x=xl+h;
integ=equan(xl)+equan(xu);
i=1;
while (x<xu)
    if (i<=2)
        integ=integ+3*equan(x);
        i=i+1;
    else
        integ=integ+2*equan(x);
        i=1;
    end
    x=x+h;
end
integ=integ*3*h/8
% ========================================================================
 
% Equation to be solved
function[eqn]=equan(x);
eqn=x^2+x-2;

4 comments:

  1. Can you please give the MATLAB code for n point too??

    ReplyDelete
  2. ??? Undefined function or method 'equan' for input arguments of type 'double'.

    ReplyDelete