y^{′} = f(t,y) given y(t_{0}) = y_{0}
$${y}^{\prime}=f(t,y)\text{\hspace{1em} given \hspace{1em}}y\left({t}_{0}\right)={y}_{0}$$ |
Input Parameters
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: nag_ode_ivp_rkts_range_example
function nag_ode_ivp_rkts_range_example % Set initial conditions and input method = int64(1); tstart = 0; tend = 2*pi; yinit = [0;1]; hstart = 0; thresh = [1e-08; 1e-08]; npts = 40; tol0 = 1.0E-3; ygot = zeros(npts+1, 2); tgot = zeros(npts+1, 1); err1 = zeros(npts+1, 2); err2 = zeros(npts+1, 2); ymax = zeros(1, 2); % Set control for output tinc = (tend-tstart)/npts; tol = 10.0*tol0; % We run through the calculation twice with two tolerance values for i = 1:2 tol = tol*0.1; % Call setup function [iwsav, rwsav, ifail] = ... nag_ode_ivp_rkts_setup(tstart, tend, yinit, tol, thresh, method); fprintf('\nCalculation with TOL = %8.1e\n\n', tol); fprintf(' t y1 y2 err1 err2\n'); fprintf(' %6.3f %7.3f %7.3f %7.3f %7.3f\n', tstart, yinit, 0, 0); tgot(1) = tstart; ygot(1,:) = yinit; twant = tstart; for j=1:npts twant = twant + tinc; [tgot(j+1), ygot(j+1,:), ypgot, ymax, user, iwsav, rwsav, ifail] = ... nag_ode_ivp_rkts_range(@f, twant, ygot(j, :), ymax, iwsav, rwsav); err1(j+1, i) = ygot(j+1, 1)-sin(tgot(j+1)); err2(j+1, i) = ygot(j+1, 2)-cos(tgot(j+1)); if rem(j, 5) == 0 fprintf(' %6.3f %7.3f %7.3f %7.3f %7.3f\n', tgot(j+1), ygot(j+1, :), err1(j+1, i), err2(j+1)); end end [fevals, stepcost, waste, stepsok, hnext, iwsav, ifail] = ... nag_ode_ivp_rkts_diag(iwsav, rwsav); fprintf('Cost of the integration in evaluations of f is %d\n', fevals); end % Plot results fig = figure('Number', 'off'); title('First-order ODEs using Runge-Kutta Low-order Method using Two Tolerances'); hold on; axis([0 10 -1.2 1.2]); xlabel('t'); ylabel('Solution (y, y'')'); plot(tgot, ygot(:, 1), '-xr'); text(ceil(tgot(npts+1)), ygot(npts+1, 1)-0.2, 'y', 'Color', 'r'); plot(tgot, ygot(:, 2), '-xg'); text(ceil(tgot(npts+1)), ygot(npts+1, 2), 'y''', 'Color', 'g'); % Plot errors with a different (log) scale ax1 = gca; ax2 = axes('Position',get(ax1,'Position'),... 'XAxisLocation','bottom',... 'YAxisLocation','right',... 'YScale', 'log', ... 'Color','none',... 'XColor','k','YColor','k'); hold on; axis([0 10 1e-7 0.01]); ylabel('abs(Error)'); plot(ax2, tgot, abs(err1(:, 1)), '-*b'); text(ceil(tgot(npts+1)), err1(npts+1, 1), 'y-error (tol=0.001)', 'Color', 'b'); plot(ax2, tgot, abs(err1(:, 2)), '-sm'); text(ceil(tgot(npts+1)), err1(npts+1, 2), 'y-error (tol=0.0001)', 'Color', 'm'); function [yp, user] = f(t, n, y, user) yp = [y(2); -y(1)];
Calculation with TOL = 1.0e-03 t y1 y2 err1 err2 0.000 0.000 1.000 0.000 0.000 0.785 0.707 0.707 -0.000 -0.000 1.571 0.999 -0.000 -0.001 -0.000 2.356 0.706 -0.706 -0.001 0.001 3.142 -0.000 -0.998 -0.000 0.002 3.927 -0.706 -0.705 0.001 0.002 4.712 -0.998 0.001 0.002 0.001 5.498 -0.705 0.706 0.002 -0.002 6.283 0.001 0.997 0.001 -0.003 Cost of the integration in evaluations of f is 421 Calculation with TOL = 1.0e-04 t y1 y2 err1 err2 0.000 0.000 1.000 0.000 0.000 0.785 0.707 0.707 -0.000 -0.000 1.571 1.000 -0.000 -0.000 -0.000 2.356 0.707 -0.707 -0.000 0.001 3.142 -0.000 -1.000 -0.000 0.002 3.927 -0.707 -0.707 0.000 0.002 4.712 -1.000 0.000 0.000 0.001 5.498 -0.707 0.707 0.000 -0.002 6.283 0.000 1.000 0.000 -0.003 Cost of the integration in evaluations of f is 871
Open in the MATLAB editor: d02pe_example
function d02pe_example % Set initial conditions and input method = int64(1); tstart = 0; tend = 2*pi; yinit = [0;1]; hstart = 0; thresh = [1e-08; 1e-08]; npts = 40; tol0 = 1.0E-3; ygot = zeros(npts+1, 2); tgot = zeros(npts+1, 1); err1 = zeros(npts+1, 2); err2 = zeros(npts+1, 2); ymax = zeros(1, 2); % Set control for output tinc = (tend-tstart)/npts; tol = 10.0*tol0; % We run through the calculation twice with two tolerance values for i = 1:2 tol = tol*0.1; % Call setup function [iwsav, rwsav, ifail] = d02pq(tstart, tend, yinit, tol, thresh, method); fprintf('\nCalculation with TOL = %8.1e\n\n', tol); fprintf(' t y1 y2 err1 err2\n'); fprintf(' %6.3f %7.3f %7.3f %7.3f %7.3f\n', tstart, yinit, 0, 0); tgot(1) = tstart; ygot(1,:) = yinit; twant = tstart; for j=1:npts twant = twant + tinc; [tgot(j+1), ygot(j+1,:), ypgot, ymax, user, iwsav, rwsav, ifail] = ... d02pe(@f, twant, ygot(j, :), ymax, iwsav, rwsav); err1(j+1, i) = ygot(j+1, 1)-sin(tgot(j+1)); err2(j+1, i) = ygot(j+1, 2)-cos(tgot(j+1)); if rem(j, 5) == 0 fprintf(' %6.3f %7.3f %7.3f %7.3f %7.3f\n', tgot(j+1), ygot(j+1, :), err1(j+1, i), err2(j+1)); end end [fevals, stepcost, waste, stepsok, hnext, iwsav, ifail] = d02pt(iwsav, rwsav); fprintf('Cost of the integration in evaluations of f is %d\n', fevals); end % Plot results fig = figure('Number', 'off'); title('First-order ODEs using Runge-Kutta Low-order Method using Two Tolerances'); hold on; axis([0 10 -1.2 1.2]); xlabel('t'); ylabel('Solution (y, y'')'); plot(tgot, ygot(:, 1), '-xr'); text(ceil(tgot(npts+1)), ygot(npts+1, 1)-0.2, 'y', 'Color', 'r'); plot(tgot, ygot(:, 2), '-xg'); text(ceil(tgot(npts+1)), ygot(npts+1, 2), 'y''', 'Color', 'g'); % Plot errors with a different (log) scale ax1 = gca; ax2 = axes('Position',get(ax1,'Position'),... 'XAxisLocation','bottom',... 'YAxisLocation','right',... 'YScale', 'log', ... 'Color','none',... 'XColor','k','YColor','k'); hold on; axis([0 10 1e-7 0.01]); ylabel('abs(Error)'); plot(ax2, tgot, abs(err1(:, 1)), '-*b'); text(ceil(tgot(npts+1)), err1(npts+1, 1), 'y-error (tol=0.001)', 'Color', 'b'); plot(ax2, tgot, abs(err1(:, 2)), '-sm'); text(ceil(tgot(npts+1)), err1(npts+1, 2), 'y-error (tol=0.0001)', 'Color', 'm'); function [yp, user] = f(t, n, y, user) yp = [y(2); -y(1)];
Calculation with TOL = 1.0e-03 t y1 y2 err1 err2 0.000 0.000 1.000 0.000 0.000 0.785 0.707 0.707 -0.000 -0.000 1.571 0.999 -0.000 -0.001 -0.000 2.356 0.706 -0.706 -0.001 0.001 3.142 -0.000 -0.998 -0.000 0.002 3.927 -0.706 -0.705 0.001 0.002 4.712 -0.998 0.001 0.002 0.001 5.498 -0.705 0.706 0.002 -0.002 6.283 0.001 0.997 0.001 -0.003 Cost of the integration in evaluations of f is 421 Calculation with TOL = 1.0e-04 t y1 y2 err1 err2 0.000 0.000 1.000 0.000 0.000 0.785 0.707 0.707 -0.000 -0.000 1.571 1.000 -0.000 -0.000 -0.000 2.356 0.707 -0.707 -0.000 0.001 3.142 -0.000 -1.000 -0.000 0.002 3.927 -0.707 -0.707 0.000 0.002 4.712 -1.000 0.000 0.000 0.001 5.498 -0.707 0.707 0.000 -0.002 6.283 0.000 1.000 0.000 -0.003 Cost of the integration in evaluations of f is 871