Saturday, February 23, 2013

MATLAB - Trapezoidal rule

trapezoid
% ========================================================================
% ****************** Integration by trapezoidal method *******************
% *************************** By Mahesha MG ******************************
% Date: 23/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);
while (x<xu)
    integ=integ+2*equan(x);
    x=x+h;
end
integ=integ*h/2
% ========================================================================
 
% Equation to be solved
function[eqn]=equan(x);
eqn=x^2+x-2;

No comments:

Post a Comment