Finding approximate the integral using the composite trapezoidal rule, of a function f(x) = cos(x):
format long
eps_step = 1e-5;
N = 20;
a = 0;
b = 10;
h = b - a;
ctrap = 0.5*(cos(a) + cos(b))*h;
for i=1:N
h = h/2;
ctrapn = 0.5*(cos(a) + 2*sum( cos( (a + h):h:(b - h) ) ) + cos(b))*h;
if abs( ctrap - ctrapn ) < eps_step
break;
elseif i == N
error( 'The composite trapezoidal rule did not converge' );
end
ctrap = ctrapn;
end
ctrapn
What happens if you remove the conditional statement and simply approximate the integral with larger and larger numbers of sub-intervals? (Try N = 100;)
Copyright ©2005 by Douglas Wilhelm Harder. All rights reserved.


