Open in the MATLAB editor: d02px_example
function d02px_example fprintf('d02px example results\n\n'); % Set initial conditions and input method = int64(2); tstart = 0; tend = 2*pi; yinit = [0;1]; hstart = 0; thresh = [1e-08; 1e-08]; n = int64(2); task = 'Complex Task'; errass = false; lenwrk = int64(32*n); reqest = 'Both'; nwant = int64(1); tol = 1.0E-2; npts = 32; work = zeros(n+5*nwant, 1); twant = zeros(npts+1, 1); ywant = zeros(npts, nwant); ypwant = zeros(npts, nwant); lenint = n+5*nwant; wrkint = zeros(lenint,1); % Set control for printing solution tinc = (tend-tstart)/npts; % Run through the calculation twice with two tolerance values for i = 1:2 tol = tol*0.1; % Call setup function [work, ifail] = d02pv(tstart, yinit, tend, tol, thresh, method, task, ... errass, lenwrk); % Set up first point at which solution is required twant(1) = tstart; twant(2) = tstart + tinc; ywant(1,1) = yinit(1); ypwant(1,1) = yinit(2); t = tstart; j=1; % Integrate by steps until tend is reached or error is encountered. while t < tend % Integrate one step from t, updating t. [t, y, yp, work, ifail] = d02pd(@f, n, work); % Interpolate at required additional points up to t while twant(j+1) < t j=j+1; % Interpolate and print solution at t = twant. [ywant(j,:), ypwant(j,:), work, wrkint, ifail] = d02px(n, twant(j), ... reqest, nwant, @f, work, wrkint); % Set next required solution point twant(j+1) = twant(j) + tinc; end end % Get integration statistics. [fevals, stepcost, waste, stepsok, hnext, fail] = d02py; fprintf('\nCalculation with TOL = %8.1e\n\n', tol); fprintf(' Number of evaluations of f = %d\n', fevals); end % Plot results fig1 = figure; title('Simple Sine Solution, tol=1e-4'); hold on; xlabel('t'); ylabel('Solution'); plot(twant(1:npts), ywant(:, 1), '-xr'); text(3, 0.25, 'Solution', 'Color', 'r'); plot(twant(1:npts), ypwant(:, 1), '-xg'); text(1.45, 0.25, 'Derivative', 'Color', 'g'); hold off function [yp] = f(t, y) % Evaluate derivative vector. yp = zeros(2, 1); yp(1) = y(2); yp(2) = -y(1);
d02px example results Calculation with TOL = 1.0e-03 Number of evaluations of f = 68 Calculation with TOL = 1.0e-04 Number of evaluations of f = 105