(a) Write and submit a Matlab function implementing Romberg integration to withi
ID: 3737793 • Letter: #
Question
(a) Write and submit a Matlab function implementing Romberg integration to within a specified error tolerance (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 ofi reached? function - romberg(, a. b, tol) %Estimate the integral of a function f between a and b using Romberg integration. Attempt to supply an answer with estimated absolute error no more than tol. For example, romberg ( (x) x .*3, 1, 2, 1E-6) should return 3.75Explanation / Answer
As per your requirement the below one is solution please follow it
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;
% 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=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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.