Create a matlab program using the composite trapezoidal rule
Create a matlab program using the composite trapezoidal rule to compute an approximate value for the integral (sinx/x) dx from x=0 to 0.8. Try 92 iterations.
Create a matlab program using the composite trapezoidal rule to compute an approximate value for the integral (sinx/x) dx from x=0 to 0.8. Try 92 iterations.
Solution
% The matlab code for composite trapezoidal rule is given below...
function integral = cmptrap(a,b,n,f)
h = (b-a)/n;
x = [a+h:h:b-h];
integral = h/2*(2*sum(feval(\'f\',x))+feval(\'f\',a)+feval(\'f\',b));
Run with
>> cmptrap(0.000001,0.8,92,\'f\')
ans =
0.7721 (approx)
where ’f’ is the name of the function definition file
function y = f(t)
y = sin(t)./t; % pay attention to the dot
