Wednesday, May 9, 2012

Simpsons 1/3rd Rule Using Matlab



function[y]=f(x)
y=1/(1+(x*x));

Save the above function as f.m.


function[sum]=simpson(a,b,n)
sum=0;
h=0;
h=(b-a)/n;
for i=1:n+1
   x(i)=a;
   a=a+h;
end
sum=sum+f(x(1))+f(x(n+1));
for i=2:2:n
   sum=sum+4*f(x(i));
end
for i=3:2:n
   sum=sum+2*f(x(i));
end
sum=sum*(h/3);

Arguments sent to the function are:- simpson(lower bound,upper bound,number of sub intervals)

Reactions:

0 comments:

Post a Comment