Download<< Back
function [tout, yout] = q1_solution(ypfun, t0, tfinal, y0, tol, trace)
% Q1_SOLUTION Modification of ode23_orig to use following low- and high-order methods
%
% 0 | 0 |
% 1 | 1 1/3 | 1/3
% ---+---------- 2/3 | 0 2/3
% | 1/2 1/2 -----+---------------
% | 1/4 0 3/4
% Initialization
pow = 1/3;
if nargin < 5, tol = 1.e-3; end
if nargin < 6, trace = 0; end
t = t0;
hmax = (tfinal - t)/16;
h = hmax/8;
y = y0(:);
chunk = 128;
tout = zeros(chunk,1);
yout = zeros(chunk,length(y));
k = 1;
tout(k) = t;
yout(k,:) = y.';
if trace
clc, t, h, y
end
% The main loop
while (t < tfinal) & (t + h > t)
if t + h > tfinal, h = tfinal - t; end
% Compute the slopes
s1 = feval(ypfun, t, y); s1 = s1(:);
s2 = feval(ypfun, t+h, y+h*s1); s2 = s2(:);
s2h = feval(ypfun, t+h/3, y+h*s1/3); s2h = s2h(:);
s3 = feval(ypfun, t+2*h/3, y+2*h*s2h/3); s3 = s3(:);
% Estimate the error and the acceptable error
delta = norm(h*(s1 + 2*s2 - 3*s3)/4,'inf');
tau = tol*max(norm(y,'inf'),1.0);
% Update the solution only if the error is acceptable
if delta <= tau
t = t + h;
y = y + h*(s1 + 3*s3)/4;
k = k+1;
if k > length(tout)
tout = [tout; zeros(chunk,1)];
yout = [yout; zeros(chunk,length(y))];
end
tout(k) = t;
yout(k,:) = y.';
end
if trace
home, t, h, y
end
% Update the step size
if delta ~= 0.0
h = min(hmax, 0.9*h*(tau/delta)^pow);
end
end
if (t < tfinal)
disp('Singularity likely.')
t
end
tout = tout(1:k);
yout = yout(1:k,:);