Program
function []=simpson13(x, f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1)) then
error('x of f,or both , not column vector(s)')
abort;
end;
if((xn<>yn)) then
error('x and f are not of the same length');
abort;
end;
if(modulo(xn-1,2)<>0) then
disp(xn-1,"list size=");
error('list size must be an even number');
abort;
end;
n=xn;
h=x(2)-x(1);
I=f(1,1)+f(1,n);
for j=2:n-1
if(modulo(j,2)==0) then
I=I+4*f(1,j);
else
I=I+2*f(1,j);
end;
end;
I=(h/3.0)*I
printf('Integration by simpson 1/3 rd rule =%f',I);
endfunction
Output:
-->x=(0:0.1:1.6);
-->simpson13(x,sin(x))
Integration by simpson 1/3 rd rule =1.029200
function []=simpson13(x, f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1)) then
error('x of f,or both , not column vector(s)')
abort;
end;
if((xn<>yn)) then
error('x and f are not of the same length');
abort;
end;
if(modulo(xn-1,2)<>0) then
disp(xn-1,"list size=");
error('list size must be an even number');
abort;
end;
n=xn;
h=x(2)-x(1);
I=f(1,1)+f(1,n);
for j=2:n-1
if(modulo(j,2)==0) then
I=I+4*f(1,j);
else
I=I+2*f(1,j);
end;
end;
I=(h/3.0)*I
printf('Integration by simpson 1/3 rd rule =%f',I);
endfunction
Output:
-->x=(0:0.1:1.6);
-->simpson13(x,sin(x))
Integration by simpson 1/3 rd rule =1.029200
0 Comments