MATLAB TUTORIAL for the First Course. Part III: Hamming Method

Prof. Vladimir A. Dobrushkin

This tutorial contains many matlab scripts.
You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. Any comments and/or contributions for this tutorial are welcome; you can send your remarks to

Email Vladimir Dobrushkin

Hamming Method

In 1959, Richard Wesley Hamming from Bell Telephone Laboratories, Murray Hill, New Jersey, proposed a stable predictor-corrector method for ordinary differential equations, which now bears his name. He improved classical Milne--Simpson method by replacing unstable corrector rule by a stable one.
Journal of the ACM, Volume 6, Issue 1, Jan. 1959, pages 37-47.

The Hamming multistep method is used to approximate solution of the initial vaue problem \( y' = f(x,y), \quad y(x_0 ) = y_0 \) over interval [a,b] (where usually x0 = a) by using the predictor
\[ p_{n+1} = y_{n-3} + \frac{4h}{3} \left( 2\,f_{n-2} - f_{n-1} + 2\,f_n \right) , \qquad n=3,4,\ldots . \]
and the corrector
\[ y_{n+1} = \frac{9\, y_n - y_{n-2}}{8} + \frac{3h}{8} \left( f_{n+1} - f_{n-1} + 2\,f_n \right) , \qquad n=3,4,\ldots . \]
As usual, we implement a unifom grid \( x_n = x_0 + n\,h , \) with step length h. Here yn is an approximation to the true value \( \phi (x_n ) \) of the actual solution \( y = \phi (x) . \)

Example. Let us start with the Riccati equation \( y' = x^2 + y^2 \) subject to the initial condition \( y(0) =-1 . \) Its solution is expressed through Bessel functions:

\[ d(x)\, y(x) = J_{-3/4} \left( \frac{x^2}{2} \right) \left( \Gamma^2 \left( \frac{3}{4} \right) + \pi \right) - Y_{-3/4} \left( \frac{x^2}{2} \right) \Gamma^2 \left( \frac{3}{4} \right) . \]
where the denominator
\[ d(x) = Y_{1/4} \left( \frac{x^2}{2} \right) \Gamma^2 \left( \frac{3}{4} \right) - J_{1/4} \left( \frac{x^2}{2} \right) \left( \Gamma^2 \left( \frac{3}{4} \right) +\pi \right) \]
has the first positive null at x = 2.223378383 as the figure shows
d[x_] = BesselY[1/4, x^2 /2]*Gamma[3/4]^2 - BesselJ[1/4, x^2 /2]*(Gamma[3/4]^2 + Pi)
Plot[d[x], {x, 0, 5.5}, PlotStyle -> Thick]
The sollution blows up near the point 2.223378.

function H=hamming(f,t,y)
% Input:    f is the function entered as a string 'f'
%           t is the vector of abscissas
%           y is the vector of ordinates
% Remark:   The first four coordinates of t and y must have 
%           starting values obtained with RK4
% Output:   H=[t',y'] where t is the vector of abscissas
%           y is the vector of ordinates
n=length(t);
if n < 5, break,end;
F=zeros(1,4); 
F=feval(f,t(1:4),y(1:4));
h=t(2)-t(1);
pold=0;
yold=0;
for k=4:n-1
    % Predictor
    pnew=y(k-3)+(4*h/3)*(F(2:4)*[2 -1 2]'); 
    % Modifier
    pmod=pnew+112*(yold-pold)/121;
    t(k+1)=t(1)+h*k;
    F=[F(2) F(3) F(4) feval(f,t(k+1),pmod)];
    % Corrector
    cnew=(9*y(k)-y(k-2)+3*h*(F(2:4)*[-1 2 1]'))/8; 
    y(k+1)=cnew+9*(pnew-cnew)/121;
    pold=pnew;
    yold=cnew;
    F(4)=feval(f,t(k+1),y(k+1));
end
H=[t',y'];

Example. Hamming's solution to \( y' = 30-5y, \quad y(0) =1 \) over interval [0,10] with N = 50 steps produces oscillations. It is stabilized when N = 70.


Complete