Scilab Program - trapezoidal rule - IProgramX

Program 

function []=tra(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;
n=xn;
h=x(2)-x(1);
I=f(1,1)+f(1,n);
for j=2:n-1
        I=I+2*f(1,j);
end;
I=(h/2.0)*I
printf('Integration by Trapezoidal rule =%f',I);
endfunction

Output:

-->exec('C:\Users\acer\Documents\sp5(1).sce', -1)

-->x=(0:0.1:1.6);

-->tra(x,sin(x))
Integration by Trapezoidal rule =1.028342

Post a Comment

0 Comments