Simpson's 1/3rd Rule in MATLAB

Below is the matlab code for simpson's 1/3rd rule :

x=[0 .2 .4 .6 .8 1]
n=6;
h=(x(6)-x(1))/5
for i=1:n
y(i)=1/(1+x(i)*x(i));
end
sum=h/3*(y(1)+y(6)+4*(y(2)+y(4))+2*(y(3)+y(5)))

Output :

>> simp

x =

         0    0.2000    0.4000    0.6000    0.8000    1.0000


h =

    0.2000


sum =

    0.7487

Top