ml3

4
AMS 20 MATLAB NOTES UCSC Solving systems of first order equations with ode45 c 2015, Yonatan Katznelson The MATLAB numerical solver, ode45 is designed to work with first order dif- ferential equations and systems of first order equations. We have already seen how to find approximate solutions to a single, first-order initial value problem, and the procedure for systems of first order equations is completely analogous. First a brief summary of systems of first order equations and initial value problems. 1. Systems of first order differential equations. A system of first order differential equations looks like this: y 0 1 = f 1 (t, y 1 ,y 2 ,...,y k ) y 0 2 = f 2 (t, y 1 ,y 2 ,...,y k ) . . . . . . y 0 k = f k (t, y 1 ,y 2 ,...,y k ) (1.1) where y 1 ,y 2 ,...,y k are interrelated functions. A first order system like (1.1) may be accompanied by initial values y 1 (t 0 )= η 1 ,y 2 (t 0 )= η 2 , ..., y k (t 0 )= η k , giving a (k-dimensional) initial value problem. A better way to view a system of first order equations like (1.1) is as a single vector-valued equation y 0 = F(t, y), (1.2) where y = y 1 y 2 . . . y k , y 0 = y 0 1 y 0 2 . . . y 0 k and F = f 1 f 2 . . . f k . This is the way that MATLAB views things. In this format, an initial value problem looks like y 0 = F(t, y), y(t 0 )= η, where η = η 1 η 2 . . . η k . 2. Using ode45 to solve vector-valued initial value problems. Recall that to compute an approximate solution to the single (scalar-valued) initial value problem y 0 = f (t, y), y(t 0 )= y 0 on the interval [t 0 ,t 1 ], we use the MATLAB commands 1

Upload: jana-voda

Post on 19-Dec-2015

222 views

Category:

Documents


0 download

DESCRIPTION

Solving systems of first order equations with ode45

TRANSCRIPT

Page 1: ML3

AMS 20 MATLAB NOTES UCSC

Solving systems of first order equations with ode45

c© 2015, Yonatan Katznelson

The MATLAB numerical solver, ode45 is designed to work with first order dif-ferential equations and systems of first order equations. We have already seenhow to find approximate solutions to a single, first-order initial value problem,and the procedure for systems of first order equations is completely analogous.

First a brief summary of systems of first order equations and initial valueproblems.

1. Systems of first order differential equations.

A system of first order differential equations looks like this:

y′1 = f1(t, y1, y2, . . . , yk)y′2 = f2(t, y1, y2, . . . , yk)...

...y′k = fk(t, y1, y2, . . . , yk)

(1.1)

where y1, y2, . . . , yk are interrelated functions. A first order system like (1.1) maybe accompanied by initial values

y1(t0) = η1, y2(t0) = η2, . . . , yk(t0) = ηk,

giving a (k-dimensional) initial value problem.A better way to view a system of first order equations like (1.1) is as a single

vector-valued equationy′ = F(t,y), (1.2)

where

y =

y1y2...yk

, y′ =

y′1y′2...y′k

and F =

f1f2...fk

.This is the way that MATLAB views things. In this format, an initial value problemlooks like

y′ = F(t,y), y(t0) = η,

where

η =

η1η2...ηk

.

2. Using ode45 to solve vector-valued initial value problems.

Recall that to compute an approximate solution to the single (scalar-valued)initial value problem

y′ = f(t, y), y(t0) = y0

on the interval [t0, t1], we use the MATLAB commands

1

Page 2: ML3

>> f=@(t,y) ... (definition of f(t, y));>> [t y] = ode45(f,[t0 t1],y0);

To find the approximate solutions to vector-valued, first order initial valueproblems, the syntax is almost identical, except that we have to remember thatthe variables and functions are now (column) vectors.

Specifically, to compute an approximate solution to the vector-valued initialvalue problem

y′1y′2...y′k

=

f1(t, y1, . . . , yk)f2(t, y1, . . . , yk)

...fk(t, y1, . . . , yk)

;

y1(t0)y2(t0)...

yk(t0)

=

η1η2...ηk

(2.1)

on the interval [t0, t1], we use the MATLAB commands

>> F=@(t,y) [def. of f1(t, y(1), . . . , y(k)); . . .; def. of fk(t, y(1), . . . , y(k))];

>> [t y]=ode45(F, [t0 t1], [η1 η2 . . . ηk]);

where now, y is a k×n matrix, whose jth row y(j) contains the (approximate) valuesof yj(t). If we want to plot the graphs of one (or more) of the (approximate)solutions, we use the command

>> plot(t, y(:,j))

to plot yj.

3. Examples.

Example 1. Use ode45 to plot (approximate) solutions of the system below onthe interval [0, 5].

y′1 =ty1

y21 + y22 + 1y1(0) = 1

y′2 =(y2 − y1)2

y22 + y23 + 1y2(0) = 0

y′3 =t2y23

y21 + y23 + 1y3(0) = −2

(3.1)

The MATLAB commands

>> F=@(t,y) [t.*y(1)./(y(1).∧∧∧2+y(2).∧∧∧2+1);

(y(2)-y(1)).∧∧∧2./(y(2).∧∧∧2+y(3).∧∧∧2+1);

t.∧∧∧2.*y(3).∧∧∧2./(y(1).∧∧∧2+y(3).∧∧∧2+1)];

>> [t y]=ode45(F,[0 2], [1 0 -1]);

>> plot(t, y(:,1), ’r’, t, y(:,2), ’g’, t, y(:,3), ’b’)

2

Page 3: ML3

Figure 1: The output for Example 1.

plot the graph of y1 in red, the graph of y2 in green and the graph of y3 in blue,producing the plot in Figure 1 above.

Example 2. Suppose a pendulum has length ` = 2 feet, and at time t = 0, it isdisplaced from its equilibrium position by the angle ϑ0 = π/6 and released (withinitial velocity 0).† Plot the position (angle) of the pendulum as a function oftime t, assuming that there is no damping, and determine the (approximate) periodof the motion (i.e., determine when the pendulum first returns to its initialposition).

We want to find the solution of the second order initial value problem

ϑ′′ + 16.87 sin(ϑ) = 0, ϑ(0) =π

6, ϑ′(0) = 0,

where ` = 2 (and g ≈ 32.174). We can’t solve this problem explicitly, so instead wewill compute an approximate solution using ode45.

To do this, we first need to transform the second order initial value probleminto a pair of first order initial value problems

ϑ′′ +g

`sin(ϑ) = 0, ϑ(0) =

π

6, ϑ′(0) = 0 =⇒

ϑ′1 = ϑ2 ϑ1(0) =

π

6

ϑ′2 = −16.87 sin(ϑ1) ϑ2(0) = 0

where ϑ1 = ϑ and ϑ2 = ϑ′.

Next we use the MATLAB commands

†See section 1.3 in the book for the derivation of the motion of an undamped pendulum.

3

Page 4: ML3

>> Pen=@(t,Theta) [Theta(2); -16.87*sin(Theta(1))];

>> [t Theta]=ode45(Pen,[0 5],[pi/6 0]);

>> plot(t,Theta(:,1))

>> grid minor

to produce the plot in Figure 2, below. The last command ‘grid minor’, adds a finegrid to the plot which gives a better view of where the maxima are to determinethe period.

Figure 2: Plot of the position of the pendulum in Example 2.

From the plot, it appears that the pendulum first returns to its initial posi-tion about halfway between 1.5 and 1.6, i.e., at about tp = 1.55.

you can study the values stored in T(:,1) to find the index where π/6 ≈ 0.5236appears for the second time. Then, with that index you can check the correspondingentry in the time vector t. If you do this, you will find that the pendulum firstreturns to its initial position after about 1.5505 seconds.

4