%MATLAB version of FORTRAN program for Example 8.7 on page 387 of conte/deboor
%
%  Reference:
%
%    Samuel Conte, Carl de Boor,
%    Elementary Numerical Analysis,
%    Third Edition,
%    SIAM, 2017,
%    ISBN: 978-1-611975-19-2.
%
SOLN = @(X) exp(X)-1-X;
F = @(x,y) x+y;

disp('Adams-Moulton Method')
disp('   N    XN              YN              DN              ERROR')
NSTEPS = 32; H = 1/NSTEPS; YBEGIN = 0; XBEGIN = 0;
f = zeros(1,NSTEPS);

   % compute the first four points using the exact solution

f(1) = F(XBEGIN,YBEGIN);
N = 0; 
fprintf('  %2d   %15.8e %15.8e %15.8e %15.8e\n', ...
            [N,XBEGIN,YBEGIN,0,0])
for N=1:3
   XN = XBEGIN + N*H; YN = SOLN(XN); f(N+1) = F(XN,YN);
   fprintf('  %2d   %15.8e %15.8e %15.8e %15.8e\n', [N,XN,YN,0,0])
end
   % begin iteration
for N=4:NSTEPS
   YNPRED = YN +(H/24)*f(N-3:N)*[-9;37;-59;55]; XN = XBEGIN + N*H; 
   FNPRED = F(XN,YNPRED); YN = YN +(H/24)*[f(N-2:N),FNPRED]*[1;-5;19;9];
   f(N+1) = F(XN,YN);
   YOFXN = SOLN(XN); 
   fprintf('  %2d   %15.8e %15.8e %15.8e %15.8e\n', ...
               [N,XN,YN,(YN-YNPRED)/14,YOFXN-YN])
end
