Numerical Computing with MATLAB

So far, we have been assuming that the tspan interval, t 0 ? t ? t final, is a given part of the problem specification, or we have used an infinite interval and a GUI button to terminate the computation. In many situations, the determination of t final is an important aspect of the problem.
One example is a body falling under the force of gravity and encountering air resistance. When does it hit the ground? Another example is the two-body problem, the orbit of one body under the gravitational attraction of a much heavier body. What is the period of the orbit? The events feature of the MATLAB ordinary differential equation solvers provides answers to such questions.
Events detection in ordinary differential equations involves two functions, f(t, y) and g(t, y), and an initial condition, ( t 0, y 0). The problem is to find a function y(t) and a final value t * so that
and
A simple model for the falling body is
with initial conditions y(0) = 1,
. The question is, for what t does y(t) = 0? The code for the function f(t, y) is
function ydot = f(t,y) ydot = [y(2); -1+y(2)^2];
With the differential equation written as a first-order system, y becomes a vector with two components and so g(t, y) = y 1. The code...