It is often of interest to determine when the solution satisfies a certain property. For example, suppose that we want to know when the solution obtained with Euler's method to equation (5) with crosses . This can be accomplished with the following Matlab program:
K = 1;
s = 20;
y0 = 100;
npoints = 50;
dt = 0.1;
y = zeros(npoints,1);
t = zeros(npoints,1);
y(1) = y0;
t(1) = 0.0;
for step=1:npoints-1
y(step+1) = y(step) + dt*K*(y(step)-s);
t(step+1) = t(step) + dt;
if ((y(step) < 1000) & (y(step+1) > 1000)) % conditional statement
fprintf('y crosses 1000 at t= %14.7f',t(step+1)); % output result to the screen
end
end
It is found that, for this stepsize, crosses 1000 at . How does this result change for different stepsizes?