At Mark 22: | nwkjac was removed from the interface; neq was made optional |
. . . [...] = d02nv(...); [...] = d02ns(...); [..., ifail] = d02nb(...); if (ifail ~= 1 and ifail < 14) [...] = d02ny(...); end . . .The linear algebra setup function nag_ode_ivp_stiff_fulljac_setup (d02ns) and one of the integrator setup functions, nag_ode_ivp_stiff_bdf (d02nv) or nag_ode_ivp_stiff_blend (d02nw), must be called prior to the call of nag_ode_ivp_stiff_exp_fulljac (d02nb). The integrator diagnostic function nag_ode_ivp_stiff_integ_diag (d02ny) may be called after the call to nag_ode_ivp_stiff_exp_fulljac (d02nb). There is also a function, nag_ode_ivp_stiff_contin (d02nz), designed to permit you to change step size on a continuation call to nag_ode_ivp_stiff_exp_fulljac (d02nb) without restarting the integration process.
Input Parameters
Output Parameters
Input Parameters
Output Parameters
Input Parameters
[errloc, ifail] = d02za(acor(1:neq,2), acor(1:neq,1)); % Check ifail before proceeding
Output Parameters
Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.
Open in the MATLAB editor: d02nb_example
function d02nb_example fprintf('d02nb example results\n\n'); % Initialize setup variables and arrays. neq = int64(3); neqmax = int64(neq); nwkjac = int64(neqmax*(neqmax + 1)); maxord = int64(5); sdysav = int64(maxord+1); maxstp = int64(200); mxhnil = int64(5); h0 = 0; hmax = 10; hmin = 1.0e-10; tcrit = 10; petzld = false; const = zeros(6, 1); rwork = zeros(50+4*neqmax, 1); % d02nv is a setup routine to be called prior to d02nb. [const, rwork, ifail] = d02nv(neqmax, sdysav, maxord, 'Newton', petzld, ... const, tcrit, hmin, hmax, h0, maxstp, ... mxhnil, 'Average-L2', rwork); % d02ns determines how d02nb evaluates the Jacobian. [rwork, ifail] = d02ns(neq, neqmax, 'Numerical', nwkjac, rwork); % Initialize integration variables and arrays. inform(1:23) = int64(0); ysave = zeros(neq, sdysav); wkjac = zeros(nwkjac, 1); % First case. Integrate to tout without passing tout (tcrit=tout and itask=4) % use B.D.F formulae with a Newton method. % Evaluate Jacobian numerically (d02nbz), no monitoring (d02nby). t = 0.0; tout = 10.0; itask = int64(4); itrace = int64(0); y = [1; 0; 0]; itol = int64(1); rtol = [0.0001]; atol = [1e-07]; % Output initial condition. fprintf(' Numerical Jacobian\n\n'); fprintf(' x y\n'); fprintf('%8.3f %8.5f%8.5f%8.5f\n', t, y); [t, y, ydot, rwork, inform, ysave, wkjac, ifail] = d02nb(t, tout, y, rwork, ... rtol, atol, itol, inform, @fcn, ysave, 'd02nbz', wkjac, 'd02nby', ... itask, itrace); % Output results at t. fprintf('%8.3f %8.5f%8.5f%8.5f\n', t, y); % d02ny returns integrator diagnostics. [hu, h, tcur, tolsf, nst, nre, nje, nqu, nq, nit, imxer, algequ, ifail] = ... d02ny(neq, neqmax, rwork, inform); % Output diagnostics. fprintf('\nDiagnostic information\n integration status:\n'); fprintf(' last and next step sizes = %8.5f, %8.5f\n',hu, h); fprintf(' integration stopped at x = %8.5f\n',tcur); fprintf(' algorithm statistics:\n'); fprintf(' number of time-steps and Newton iterations = %5d %5d\n',nst,nit); fprintf(' number of residual and jacobian evaluations = %5d %5d\n',nre,nje); fprintf(' order of method last used and next to use = %5d %5d\n',nqu,nq); fprintf(' component with largest error = %5d\n\n',imxer); % Second case. As first case but with Jacobian evaluated using jac. t = 0.0; y = [1; 0; 0]; [const, rwork, ifail] = d02nv(neqmax, sdysav, maxord, 'Newton', petzld, ... const, tcrit, hmin, hmax, h0, maxstp, mxhnil, 'Average-L2', rwork); [rwork, ifail] = d02ns(neq, neqmax, 'Analytical', nwkjac, rwork); fprintf(' Analytic Jacobian\n\n'); fprintf(' x y\n'); fprintf('%8.3f %8.5f%8.5f%8.5f\n', t, y); % jac evaluates Jacobian analytically. [t, y, ydot, rwork, inform, ysave, wkjac, ifail] = ... d02nb(... t, tout, y, rwork, rtol, atol, itol, inform, ... @fcn, ysave, @jac, wkjac, 'd02nby', itask, itrace); fprintf('%8.3f %8.5f%8.5f%8.5f\n', t, y); [hu, h, tcur, tolsf, nst, nre, nje, nqu, nq, nit, imxer, algequ, ifail] = ... d02ny(neq, neqmax, rwork, inform); fprintf('\nDiagnostic information\n integration status:\n'); fprintf(' last and next step sizes = %8.5f, %8.5f\n',hu, h); fprintf(' integration stopped at x = %8.5f\n',tcur); fprintf(' algorithm statistics:\n'); fprintf(' number of time-steps and Newton iterations = %5d %5d\n',nst,nit); fprintf(' number of residual and jacobian evaluations = %5d %5d\n',nre,nje); fprintf(' order of method last used and next to use = %5d %5d\n',nqu,nq); fprintf(' component with largest error = %5d\n\n',imxer); % Now repeat the calculation with a larger number of points, and store % the results for plotting. nstep = 100; tend = 10; t = 0; y = [1; 0; 0]; ncall = 1; ykeep(:,1) = y; tkeep = t; % To allow ifail=13 exits, set nag_issue_warings to true and catch. orig_stat = nag_issue_warnings(); nag_issue_warnings(true); % Setup integration: numerical Jacobian [const, rwork, ifail] = d02nv(... neqmax, sdysav, maxord, 'Newton', petzld, const,... tcrit, hmin, hmax, h0, maxstp, mxhnil, 'Average-L2', rwork); [rwork, ifail] = d02ns(neq, neqmax, 'Numerical', nwkjac, rwork); for j = 1:nstep if (j < 91) tout = exp(-0.26*(90-j))*(90*tend)/(nstep); else tout = (j*tend)/(nstep); end [t, y, ydot, rwork, inform, ysave, wkjac, ifail] = ... d02nb(... t, tout, y, rwork, rtol, atol, itol, inform, @fcn, ... ysave, 'd02nbz', wkjac, 'd02nby', itask, itrace); % continuation [rwork, ifail] = d02nz(... neqmax, tcrit, h, hmin, hmax, maxstp, mxhnil, rwork); ncall = ncall + 1; tkeep(ncall) = t; ykeep(:,ncall) = y; end nag_issue_warnings(orig_stat); % Plot results. fig1 = figure; display_plot(tkeep, ykeep); function [f, ires] = fcn(neq, t, y, ires) % Evaluate derivative vector. f = zeros(3,1); f(1) = -0.04d0*y(1) + 1.0d4*y(2)*y(3); f(2) = 0.04d0*y(1) - 1.0d4*y(2)*y(3) - 3.0d7*y(2)*y(2); f(3) = 3.0d7*y(2)*y(2); function p = jac(neq, t, y, h, d, p) % Evaluate the Jacobian. p = zeros(neq, neq); hxd = h*d; p(1,1) = 1.0d0 - hxd*(-0.04d0); p(1,2) = -hxd*(1.0d4*y(3)); p(1,3) = -hxd*(1.0d4*y(2)); p(2,1) = -hxd*(0.04d0); p(2,2) = 1.0d0 - hxd*(-1.0d4*y(3)-6.0d7*y(2)); p(2,3) = -hxd*(-1.0d4*y(2)); p(3,2) = -hxd*(6.0d7*y(2)); p(3,3) = 1.0d0 - hxd*(0.0d0); function display_plot(tkeep, ykeep) % Two curves with different y scales. [haxes, hline1, hline2] = plotyy(tkeep,ykeep(1,:), ... tkeep,ykeep(2,:), ... 'semilogx', 'semilogx'); hold on; % the third curve hline3 = semilogx(tkeep,ykeep(3,:)); % Set the axis limits and the tick specifications to beautify the plot. set(haxes(1), 'YLim', [-0.05 1.3]); set(haxes(1), 'XMinorTick', 'on', 'YMinorTick', 'on'); set(haxes(1), 'YTick', [0.0:0.2:1.2]); set(haxes(2), 'YLim', [0.0 4e-5]); set(haxes(2), 'YMinorTick', 'on'); set(haxes(2), 'YTick', [5e-6:5e-6:3.5e-5]); for iaxis = 1:2 % These properties must be the same for both sets of axes. set(haxes(iaxis), 'XLim', [0 12]); set(haxes(iaxis), 'XTick', [1e-9 1e-7 1e-5 1e-3 1e-1 10]); end set(gca, 'box', 'off'); % Add title. ht = title({'Stiff Robertson Problem:','BDF with full Jacobian'}); set(ht,'Position',[1e-6,1.15,0.0]); % Label the x axis, and both y axes. xlabel('x'); ylabel(haxes(1),'Solution (a,c)'); ylabel(haxes(2),'Solution (b)'); % Set some features of the lines. set(hline1, 'Color','blue'); set(hline2, 'Color','green'); set(hline3, 'Color','red'); % Add a legend. legend('a','c','b','Location','West'); hold off;
d02nb example results Numerical Jacobian x y 0.000 1.00000 0.00000 0.00000 10.000 0.84136 0.00002 0.15863 Diagnostic information integration status: last and next step sizes = 0.51867, 0.51867 integration stopped at x = 10.00000 algorithm statistics: number of time-steps and Newton iterations = 55 79 number of residual and jacobian evaluations = 132 17 order of method last used and next to use = 3 3 component with largest error = 3 Analytic Jacobian x y 0.000 1.00000 0.00000 0.00000 10.000 0.84136 0.00002 0.15863 Diagnostic information integration status: last and next step sizes = 0.51867, 0.51867 integration stopped at x = 10.00000 algorithm statistics: number of time-steps and Newton iterations = 55 79 number of residual and jacobian evaluations = 81 17 order of method last used and next to use = 3 3 component with largest error = 3