echo off;
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% This free software is complements of the author.

% Algorithm 7.4 (Romberg Integration).
% Section 7.3, Recursive Rules and Romberg Integration, Page 379
echo on; clc; format long; hold off; clear

% This program implements Romberg integration.

% Define and store the function f(x) in the M-file f.m
% function y = f(x)
% y = 13.*(x - x.^2).*exp(-3.*x./2);

delete f.m
diary f.m; disp('function y = f(x)');...
disp('y = 13.*(x - x.^2).*exp(-3.*x./2);');...
diary off;

f(0); % Remark. f.m romber.m are used for Algorithm 7.4

pause % Press any key to continue.

clc;, clg;
% Plot f(x) over the interval [a,b].

a = 0;
b = 4;
h = (b-a)/200;
X = a:h:b;
Y = f(X);

pause % Press any key to see the graph y = f(x).

clc; clg;
a = 0;
b = 4;
c = -1.5;
d = 2;
axis([a b c d]);...
plot(X,Y,'g');...
hold on;...
plot([a b],[0 0],'b',[0 0],[c d],'b');...
xlabel('x');...
ylabel('y');...
title('The curve y = f(x) over [a,b].');...
grid;...
axis;...
hold off;...
shg; pause % Press any key to continue.

clc;

% Romberg integration is applied over the interval [a,b].

% Place the endpoints of [a,b] in a and b.

% Place the number of rows for the Romberg table in n.

% Place the tolerance in tol.

a = 0;
b = 4;
n = 8;
toler = 0.00001;

[R,quad,err,h] = romber('f',a,b,n,toler);

pause % Press any key to continue.

clc;
Mx1 = 'The Romberg integration result is: ';
clc,echo off,diary output,...
disp(''),disp(Mx1),disp(R),diary off, echo on
pause % Press any key to continue.

X0 = a:h:b;
Y0 = f(X0);
Z0 = zeros(1,length(X0));
c = -1.5;
d = 2;
axis([a b c d]);...
plot(X,Y,'g',X0,Y0,'or',X0,Z0,'+r');...
hold on;...
plot([a b],[0 0],'b',[0 0],[c d],'b');...
xlabel('x');...
ylabel('y');...
title('Romberg integration.');...
grid;...
axis;...
hold off;...
shg; pause % Press any key to continue.

Mx2 = 'The approximate value of the integral is:';
Mx3 = 'The error bound for the quadrature is:';
Mx4 = ['+- ',num2str(err)];
clc,echo off,disp(''),disp(Mx2),diary on,...
disp(quad),disp(''),disp(Mx3),disp(Mx4),diary off,echo on