Open d02la_demo.m in the Editor
Run demo
function d02la_demo
% This function demonstrates the use of the d02la, d02lx & do2lz routines
% from the NAG toolbox for integrating a non-stiff system of 2nd order ODEs.
%
% Original version written by David Sayers.
% Modifications by Jeremy Walton (jeremyw@nag.co.uk).  August 2007.
% - added comments
% - tidied graphics
% - removed some unnecessary code (specifically, a loop over d02lz, below).

% Prompt user for input parameters.
prompt = {'Initial value of x [the independent variable]',...
    'Stepsize for x',...
    'Final value for x',...
    'Initial value(s) of y [the dependent variable]',...
    'Initial value(s) of yp [its derivative]' };
dialogtitle = 'Define initial values for d02la';
num_lines = 1;
default = {'0.0','0.2','10.0','[0.5; 0.0]','[0.0; 1.732050807568877]'};
answer = inputdlg(prompt, dialogtitle, num_lines, default, 'on');

% Convert results from strings to numbers.  We use str2double where
% possible because it's faster than str2num, but it doesn't work on vectors.
t=str2double(answer{1});
tinc=str2double(answer{2});
tend=str2double(answer{3});
y=str2num(answer{4}); %#ok<ST2NM>
yp=str2num(answer{5}); %#ok<ST2NM>

% Open the figure, set the properties of the plot, and set the title.
figure('Name', 'Integrating a non-stiff system of 2nd order ODEs using the NAG Toolbox', ...
    'Position', [100, 200, 534, 500],...
    'NumberTitle', 'off');
axes = gca;
set(axes, 'FontSize', 13);
xlabel('x');
ylabel('y_{i}', 'Rotation', 0);
xlim([t tend])
ylim([-1.5 1.5]);
title('Using NAG routines d02la, d02lx and d02lz for solving ODEs', 'FontSize', 14);

% Find out what colours Matlab's going to use for the lines in the plots,
% then use them in the annotation.  We could in principle tell Matlab what
% colours to use here, but that seems more painful.
colors = get(0, 'defaultAxesColorOrder');
text('Interpreter', 'latex',...
    'String', '$y_{1}^{\prime\prime}(x) = -y_{1}(x)/[y_{1}^2(x) + y_{2}^2(x)]^{3/2}$',...
    'Position', [0.5, 1.35], ...
    'Color', [colors(1,1) colors(1,2) colors(1,3)],...
    'FontSize', 16);
text('Interpreter', 'latex',...
    'String', '$y_{2}^{\prime\prime}(x) = -y_{2}(x)/[y_{1}^2(x) + y_{2}^2(x)]^{3/2}$',...
    'Position', [0.5, 1.05],...
    'Color', [colors(2,1) colors(2,2) colors(2,3)],...
    'FontSize', 16);
hold on;

% The number of equations in this demo is hardwired to be 2 (e.g. in the
% form of the function (d02la_fcn) that we're integrating).
neqn = nag_int(2);

% Set other parameters for NAG routine.
ydp = zeros(neqn, 1);
rwork = zeros(16+11*neqn, 1);
start = true;

% Initialization before loop.
tnext = t+tinc;
x(1) = t;
istep = 1;
z(istep, 1) = y(1);
z(istep, 2) = y(2);

% Loop over x values.
while t < tend
    % Call d02lx, which sets up for d02la.
    [start, rwork, ifail] = d02lx(0, 1e-4, zeros(neqn,1), zeros(neqn,1), ...
        nag_int(0), start, false, false, rwork);
    if ifail~=0
        disp([' Non-zero ifail after d02lx call ', num2str(ifail)]);
        return
    end;
    % Call the integrator d02la.
    [t, y, yp, ydp, rwork, ifail] = d02la('d02la_fcn', t, tnext, ...
        y, yp, ydp, rwork);
    if ifail ~= 0
        disp([' Non-zero ifail after d02la call ', num2str(ifail)]);
        return
    end;

    % Now call d02lz to interpolate components of the solution from d02la.
    [ywant, ypwant, ifail] = d02lz(t, y, yp, neqn, tnext, rwork);
    if ifail ~= 0
        disp([' Non-zero Ifail after d02lz call ', num2str(ifail)]);
        return
    end;
    % Add the values from the solution at this step to the plot.
    istep = istep + 1;
    x(istep) = tnext;
    z(istep, 1) = ywant(1);
    z(istep, 2) = ywant(2);
    plot(axes, x, z, 'LineWidth', 2);

    % This pause makes the animation more realistic.
    pause(0.5);

    tnext=t+tinc;

end;
clear z x rwork ydp ywant ypwant ;
end