You are on page 1of 1

function [ M ] = Romberg( f,a,b,n )

x = a;
fa = eval(f);
x = b;
fb = eval(f);
for i = 1:n
h(i) = (b-a)/2^(i-1);
end
for i = 1:n
s = 0;
for j = 1:2^(i-1)-1
x = (a + j*h(i));
fjh = eval(f);
s = s + fjh;
end
M(i,1) = (h(i)/2) *(fa + 2*s +fb);
end
for i = 2:n
for j = i:n
M(j,i) = (4^(i-1) * M(j,i-1) - M(j-1,i-1))/(4^(i-1)-1);
end
end
end
function [ M ] = Romberg(x.^2,0,1,3)
x = a;
fa = eval(f);
x = b;
fb = eval(f);
for i = 1:n
h(i) = (b-a)/2^(i-1);
end
for i = 1:n
s = 0;
for j = 1:2^(i-1)-1
x = (a + j*h(i));
fjh = eval(f);
s = s + fjh;
end
M(i,1) = (h(i)/2) *(fa + 2*s +fb);
end
for i = 2:n
for j = i:n
M(j,i) = (4^(i-1) * M(j,i-1) - M(j-1,i-1))/(4^(i-1)-1);
end
end
end

You might also like