1. ## Some MatLab help

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

2. Originally Posted by ussvoyager
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
1. Your function trapezium does not return anything (it just prints it as there is no semi colon after the last line.

2. The error formula for the trapezium rule depends on the function (or rather its second derivative), so if you want the error estimate to be independedent of the function you will either need to obtain the second derivative some how or call the function twice with n and 2*n steps.

3. trapezium(0,1,10) then trapezium(0,1,20)

4. It is always a good idea to just run your code and see what happens (you won't break anything)

CB