You are on page 1of 4

clear

syms x
Z=[ 5 10 15 20 25]

Z=

5 10 15 20 25

simpson6(inline('cos(5*x)+(sqrt(2*x))'),1,8,Z)

s=

19.1845

s=

20.7586

s=

20.7355

s=

20.7325

s=

20.7318

ans =

5.0000 0.7000 19.1845


10.0000 0.3500 20.7586
15.0000 0.2333 20.7355
20.0000 0.1750 20.7325
25.0000 0.1400 20.7318

ans =

20.7318

s=[19.1845 20.7586 20.7355 20.7325 20.7318]

s=

19.1845 20.7586 20.7355 20.7325 20.7318

plot(Z,s)

m-file
function s=simpson (f,a,b,Z)
T=Z;
h=Z;
for i=1:length(Z)
h(i)=(b-a)/(2*Z(i));
s1=0;
s2=0;
s=0;
for k=1:(Z(i))
x=a+h(i)*(2*k-1);
s1=s1+feval(f,x);
end
for k =1:(Z(i)-1)
x=a+h(i)*(2*k);
s2=s2+feval(f,x);
end
s=h(i)*(feval(f,a)+feval(f,b)+4*s1+2*s2)/3
T(i)=s;
end
[Z'h'T']

You might also like