MATH 267 How to solve ODE systems in MATLAB

advertisement
MATH 267
How to solve ODE systems in MATLAB
MATLAB contains very easy to use commands for numerical solutions of first
order ODE systems.
>> [t,y]=ode45(F,tspan,y0) returns the solution of the initial value problem
for the system defined by F on the interval defined in tspan with initial conditions
defined by y0. If the system has n unknown component functions then the output
array y contains n columns, one for each component.
The inline command can be used to define F as in the case of the single ODE.
(Those of you who know how to use m-files to define functions in MATLAB may do
that instead. See Chapter 8 of the supplementary MATLAB text for more details.)
Suppose for example that the problem of interest is
x01 = tx2 + sin (x1 x2 )
x1 (1) = 3
x02 = x1 e−tx2
x2 (1) = −2
This means that the system may be written in vector form as x0 = F(t, x) where
tx2 + sin (x1 x2 )
F(t, x) =
x1 e−tx2
A MATLAB command which will define this vector function is
>> F=inline(’[t*x(2)+sin(x(1)*x(2));x(1)*exp(-t*x(2))]’,’t’,’x’)
The input tspan defines the interval of t values on which the solution is to be
found, as in the single equation case, e.g.
>> tspan=[1 4]
will cause the solution to be computed for 1 ≤ t ≤ 4, with initial conditions at t = 1
given by y0, which in this case should be
>> y0=[3;-2]
(Why the semicolon in y0? It forces y0 to be a column vector. Without the semicolon, as in the definition of tspan, you are defining a row vector. The semicolon
also occurs in the definition of F because it needs to be a column vector, but tspan
can be either a row or column vector.)
To plot the solution, give the command
>> plot(t,y(:,1));shg
which will create a plot of the first component of the solution. Replace 1 by 2 to get
the second component. (shg shows the graph window, if it isn’t already visible).
You can get both curves on the same plot with the command
>> plot(t,y(:,1),t,y(:,2));shg
To display the solution in the phase plane
>> plot(y(:,1),y(:,2));shg
Homework
1. Obtain the numerical solution of the problem stated on the reverse side for
1 ≤ t ≤ 4 by entering all of the commands there in an appropriate order. Plot both
components of the solution on the same axes.
2. Transform the problem
t
y 00 = 3y − sin (y 0 − )
2
0 < t < 2 y(0) = 1
y 0 (0) = −2
into an equivalent problem for a first order system and solve numerically using
ode45. Plot the solution. (Remember the solution is a single function in this case,
not two functions.)
3. Use ode45 to solve
−1 −4
x =
x
5 −1
0
2
x(0) =
1
Compute the solution for 0 < t < 10 and plot the result in the phase plane.
You do not need to print out columns of numbers in any of these problems. Turn
in plot only for 1 and 3, and for problem 2 turn in the plot and write out the first
order system you used. Refer to the first MATLAB assignment if needed to remind
yourself how to get a plot printed.
4. Find the exact solution of problem 3 by elimination as follows: Solve the first
equation for x2 in terms of x01 , x1 , differentiate to obtain x02 in terms of x001 , x01 , and
then substitute for both x02 , x2 in the second equation. You should end up with a
single second order constant coefficient equation for x1 , which may be solved by
the method of Chapter 4. Use the first equation again to obtain x2 and then fit the
initial conditions.
Download