I'm not very good with matlab and have been trying to get some practice in over the summer. Ive just about managed to write an M-file for the trapezium rule and was just wondering how I could modify it and set a tolerance level.

This is what ive got so far:

Code:

function trapezium(a,b,n)
h=(b-a)/n;
sum = 0;
for i=1:n-1;
x(i)=a + i*h;
sum = sum + f(x(i));
end
I = h*(f(a)+f(b)+2*sum)/2
function y=f(x)
y=sin(x)

So i can use it for n strips but im not sure how to make it run for increasing strips :S

Can anyone help me out at all? Thanks