Solving Systems of ODE's With Matlab's Ode45 0.1 Previous Encounters With Ode45
Solving Systems of ODE's With Matlab's Ode45 0.1 Previous Encounters With Ode45
function dydt=yprime(t,y)
dydt(1,1)=y(2);
dydt(2,1)=-0.2*y(2)-sin(y(1));
1
Question: What kind of vector does the function yprime output?
Answer: A column vector! This wasn’t arbitrary: Matlab’s ODE’s solvers,
including ode45, want the function that evaluates the derivative to return a
column vector (this does appear to be arbitrary!). If you start getting red error
messages, this might be one place to check!
EXAMPLE: Suppose we want a solution for (2) with t ∈[0,40] and initial
conditions y1 (0) = 0 and y2 (0) = 3. We’re going to call ode45 in the same way
as in the 1-dimensional case, except that now our y0 is a vector of the form [y1
y2]. The command will look like:
Now you can plot t vs. y to see the solutions, or y1 vs. y2 to see the phase
portrait. Try this with a few different choices of initial conditions.
function dydt=yprime(t,y,a)
dydt(1,1)=y(2);
dydt(2,1)=a*y(2)-sin(y(1));
The seemingly random [] in the list of arguments for yprime is an empty place-
holder where certain options can be input to ode45. If you’re curious about that,
you can query help ode45 to see more information. Otherwise, just remember
to include it when passing in additional arguments to yprime.
Note: There is no limit to how many free parameters ode45 will pass on to
yprime. You can simply redefine yprime to handle more inputs and add those
2
inputs to the end of the list of ode45 arguments to your heart’s desire. Try gen-
erating solutions for a few different values of a to see the effect of the parameter
on the system.
The Matlab contour function will automatically create contour plots for a 3-
dimensional set of data. Recall from calculus that the contour of a multivariable
function, say f (x, y), is a set of pairs (x, y) for which f (x, y) = c, where c is
constant.
[x,y]=meshgrid(0:.1:1,0:.1:1);
contour(x,y,x.^2+y.^2,20)
0.5 Homework
Write an m-file that solves the following two second-order, nonlinear ODE’s:
1. y 00 + sin y = 0
2. y 00 + 1
10 y + sin y = 2 cos t
In order to use the techniques discussed in this worksheet, you’ll have to re-
write both equations as a system of first-order ODE’s: you may want to ask a
neighbor or refer to your textbook if you’ve forgotten how to do this.
What your script should do: Produce two plots per system. One plot
should contain a phase portrait (y1 vs. y2 ) for several different initial condi-
tions. The other should show solutions (y(t) vs. t) corresponding to different
inital conditions. They should be clearly labeled, more or less like the ones
shown below (but don’t worry about having captions).
3
Figure 1: Some solution trajectories Figure 2: A phase portrait for sev-
for the sample system. eral initial conditions.