Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I keep getting an error when I try to run the function. (not enough input argume

ID: 3282101 • Letter: I

Question

I keep getting an error when I try to run the function. (not enough input arguments.)

function [r i]=ramboringra(f,a,b,eps_step)
h = b - a;
R(0 + 1, 0 + 1) = 0.5*(f(a) + f(b))*h;

%for i = 1:N
err=0.1;
i=0;
while(err>eps_step)
i=i+1;
h = h/2;

R( i + 1, 1 ) = 0.5*(f(a) + 2*sum( f( (a + h):h:(b - h) ) ) + f(b))*h;

for j = 1:i
R(i + 1, j + 1) = (4^j*R(i + 1, j) - R(i, j))/(4^j - 1);
end
err=abs( R(i + 1, i + 1) - R(i, i) );


end

(a) Write and submit a Matlab function implementing Romberg integration to within a specified error tolerance at* html (b) Use the function you wrote to estimate the integral of f(x)-e from a 1 to b 2, with an absolute error tolerance of 10-9. Is your answer accurate to within this tolerance? What was the maximum value of i reached?

Explanation / Answer

Without function

clc;
clear all;
format long
f=@(x) x.^x;
eps_step = 1e-6;

R = zeros( N + 1, N + 1 );
a = 1;
b = 2;
h = b - a;
R(0 + 1, 0 + 1) = 0.5*(f(a) + f(b))*h;

%for i = 1:N
err=0.1;
i=0;
while(err>eps_step )
i=i+1;
h = h/2;
% This calculates the trapezoidal rule with intervals of width h
R( i + 1, 1 ) = 0.5*(f(a) + 2*sum( f( (a + h):h:(b - h) ) ) + f(b))*h;

for j = 1:i
R(i + 1, j + 1) = (4^j*R(i + 1, j) - R(i, j))/(4^j - 1);
end
err=abs( R(i + 1, i + 1) - R(i, i) );


end

R( i + 1, i + 1 )
i

R=2.050446234658004


i =

4

OR

with function

function [r i]=ramboringra(f,a,b,eps_step)
h = b - a;
R(0 + 1, 0 + 1) = 0.5*(f(a) + f(b))*h;

%for i = 1:N
err=0.1;
i=0;
while(err>eps_step)
i=i+1;
h = h/2;

R( i + 1, 1 ) = 0.5*(f(a) + 2*sum( f( (a + h):h:(b - h) ) ) + f(b))*h;

for j = 1:i
R(i + 1, j + 1) = (4^j*R(i + 1, j) - R(i, j))/(4^j - 1);
end
err=abs( R(i + 1, i + 1) - R(i, i) );


end

r=R( i + 1, i + 1 );
i;

end

f=@(x)x^x;
a=1;
b=2;
eps_step=1e-6;
[r i]=ramboringra(f,a,b,eps_step)

r =

2.050446234658004


i =4