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

An Example

As an example, suppose we want to solve the one-dimensional ordinary differential equation

\begin{displaymath}
\frac{dy}{dt} = K (y-s),
\end{displaymath} (5)

where $K$ and $s$ are constants. This can be in fact be solved exactly because it is a separable equation, and we find that
\begin{displaymath}
y(t) = y_0 e^{K t} + s (1 - e^{K t}),
\end{displaymath} (6)

where $y_0 = y(0)$. (This can be checked by plugging (6) into (5)).

To view the exact solution in Matlab, we create a file called ``yexact.m'' with the following lines of text:

function r = yexact(t,y0,K,s)
r = y0*exp(K*t) + s*(1 - exp(K*t));

Text version of yexact.m

Note that this function takes four arguments, the time t, the initial condition y0, and the constants K and s from (5). Suppose that we want the solution for y0=100, K=1, and s=20. First, in Matlab we type:

t = 0:0.01:5;

which creates a vector t = (0,0.01,0.02,...,4.98,4.99,5), then

plot(t,yexact(t,100,1,20))

which plots yexact vs. t at the times given in the vector t. This creates a plot as shown in Figure 1 below.

Figure 1: The exact solution for equation (5) with $y_0=100$, $K = 1$, $s=20$.
\begin{figure}\begin{center}
\leavevmode
\epsfbox{exact.eps}\end{center}\end{figure}


next up previous
Next: Numerically Solving the Example Up: APC591 Tutorial 1: Euler's Previous: Euler's Method
Jeffrey M. Moehlis 2001-09-24