next up previous
Next: Conclusion Up: APC591 Tutorial 1: Euler's Previous: Numerically Solving the Example

Conditional Statements

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 $y_0=100, K=1, s=20$ crosses $y=1000$. 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


Text version of this program

It is found that, for this stepsize, $y$ crosses 1000 at $t=2.7$. How does this result change for different stepsizes?



Jeffrey M. Moehlis 2001-09-24