Algorithm FUNCTION quadapta b tol 0000001 c a b2 fa fa f
Algorithm:
FUNCTION quadapt(a, b)
tol = 0.000001
c = (a + b)/2
fa = f(a)
fc = f(c)
fb = f(b)
quadapt = qstep(a, b, tol, fa, fc, fb)
END quadapt
FUNCTION qstep(a, b, tol, fa, fc, fb)
h1 = b - a
h2 = h1/2
c = (a + b)/2
fd = f((a + c)/2)
fe = f((c + b)/2)
I1 = h1/6 * (fa + 4 * fc + fb)
I2 = h2/6 * (fa + 4 * fd + 2 * fc + 4 * fe + fb)
IF |I2 - I1| tol THEN
I = I2 + (I2 - I1)/15
ELSE
Ia = qstep(a, c, tol, fa, fd, fc)
Ib = qstep(c, b, tol, fc, fe, fb)
I = Ia + Ib
END IF
qstep = I
END qstep
MATLAB Program:
function [Q,fcount] = quadadapt(F, a, b, tol, varargin)
if nargin < 4 | isempty(tol)
tol = 1.e–6;
end
c = (a + b)/2;
fa = feval(F,a,varargin{:});
fc = feval(F,c,varargin{:});
fb = feval(F,b,varargin{:});
[Q,k] = quadstep(F, a, b, tol, fa, fc, fb, varargin{:});
fcount = k + 3;
function [Q,fcount] = quadstep(F, a, b, tol, fa, fc, fb, varargin)
_____
fd = feval(F,(a+c)/2,varargin{:});
fe = feval(F,(c+b)/2,varargin{:});
Q1 = h/6 * (fa + 4*fc + fb);
Q2 = h/12 * (fa + 4*fd + 2*fc + 4*fe + fb);
_____
_____
fcount = 2;
else
_____
Q = Qa + Qb;
fcount = ka + kb + 2;
end
every _____ is a missing step.
The options are:
[Q = Q2 + (Q2 – Q1)/15;]
[if abs(Q2 – Q1) <= tol]
[h = b – a;
c = (a + b)/2;]
[[Qa,ka] = quadstep(F, a, c, tol, fa, fd, fc, varargin{:});
[Qb,kb] = quadstep(F, c, b, tol, fc, fe, fb, varargin{:});]
Solution
Missing steps of MATLAB code is arranged in the code below. Missing steps arranged are highlighted in Bold & Underlined and explanations are mentioned in between two % symbol like % explanation %..
MATLAB Program:
function [Q,fcount] = quadadapt(F, a, b, tol, varargin)
if nargin < 4 | isempty(tol)
tol = 1.e–6;
end
c = (a + b)/2;
fa = feval(F,a,varargin{:});
fc = feval(F,c,varargin{:});
fb = feval(F,b,varargin{:});
[Q,k] = quadstep(F, a, b, tol, fa, fc, fb, varargin{:});
fcount = k + 3;
function [Q,fcount] = quadstep(F, a, b, tol, fa, fc, fb, varargin)
h = b – a;
c = (a + b)/2;
% As per algorithm, after declaration of function qstep step size i.e. h1=b-a and average c=(a+b)/2 has been calculated. Similarly, in MATLAB program function quadstep has been declared however step size calculation and average calculation is missing. Above statement h=b-a calculaes the step size and c = (a+b)/2 calculates average.%
fd = feval(F,(a+c)/2,varargin{:});
fe = feval(F,(c+b)/2,varargin{:});
Q1 = h/6 * (fa + 4*fc + fb);
Q2 = h/12 * (fa + 4*fd + 2*fc + 4*fe + fb);
if abs(Q2 – Q1) <= tol
% As per algorithm, after calculation of two integrals I1 & I2, differernce of both are compared with tolerance by the statement IF |I2 - I1| tol THEN. Similarly after calculation of two intergrals in MATLAB program, difference of both the integrals are to be compared with tollerance. Thus the missing step is as mentioned above which compares the difference with tollerance.%
Q = Q2 + (Q2 – Q1)/15;
% After the if statement in the algorithm, I = I2 + (I2 - I1)/15 statement is executed. Thus the similar statement in MATLAB program is given as mentioned above. %
fcount = 2;
else
[Qa,ka] = quadstep(F, a, c, tol, fa, fd, fc, varargin{:});
[Qb,kb] = quadstep(F, c, b, tol, fc, fe, fb, varargin{:});
% For the else condition in algorithm, qstep funtion is called twice using the following steps Ia = qstep(a, c, tol, fa, fd, fc) & Ib = qstep(c, b, tol, fc, fe, fb). Similary in MATLAB program after else condition quadstep function has been called twice for Qa,ka and Qb,kb by the above mentioned steps. %
Q = Qa + Qb;
fcount = ka + kb + 2;
end

