Program
function[]=simp3(x,f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1))then
error('x of f,or both ,not coloumn vector (s)')
abort;
end;
if((xn<>yn))
then
error ('x of f,or both,not of the same length')
abort;
end;
if(modulo(xn-1,3)<>0)then
disp(xn-1,"list size=");
error("list size must be of the from 3*i+1, where i=integer");
abort;
end;
n=xn;
h=x(2)-x(1);
i=f(1,1)+f(1,n);
for j=2:n-1
if(modulo(j-1,3)==0) then
i=i+2*f(1,j)
else
i=i+3*f(1,j)
end;
end;
i=(3/8)*h*i;
printf('integration by simpson 3/8 rd rule = %f',i);
endfunction
Output:
x=(4:0.2:5.2);
simp(x,log10(x));
integration by simpson 3/8 rd rule = 1.827847
function[]=simp3(x,f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1))then
error('x of f,or both ,not coloumn vector (s)')
abort;
end;
if((xn<>yn))
then
error ('x of f,or both,not of the same length')
abort;
end;
if(modulo(xn-1,3)<>0)then
disp(xn-1,"list size=");
error("list size must be of the from 3*i+1, where i=integer");
abort;
end;
n=xn;
h=x(2)-x(1);
i=f(1,1)+f(1,n);
for j=2:n-1
if(modulo(j-1,3)==0) then
i=i+2*f(1,j)
else
i=i+3*f(1,j)
end;
end;
i=(3/8)*h*i;
printf('integration by simpson 3/8 rd rule = %f',i);
endfunction
Output:
x=(4:0.2:5.2);
simp(x,log10(x));
integration by simpson 3/8 rd rule = 1.827847
0 Comments