MATH 267 Use of MATLAB to solve ODEs

advertisement

MATH 267 Use of MATLAB to solve ODEs

MATLAB contain very easy to use commands for solving initial value problems for ordinary differential equations. Suppose we wish to obtain a numerical solution of y

0

= f ( t, y ) y ( t

0

) = y

0

This can be done with the following command:

>>[t y] = ode45(f,tspan,y0) where the inputs f,tspan,y0 and outputs t,y are as follows.

f : defines the function in the ODE. For example to get f ( t, y ) = t

2 command

1

− y

3

, give the MATLAB

>>f=inline(’t ˆ 2-y ˆ 3’,’t’,’y’) tspan : defines the endpoints of the interval on which you want the solution, e.g.

>> tspan=[1 3] to get the solution for 1 ≤ t ≤ 3. The first entry should be t

0

.

y0 : the initial value, e.g, for y

0

= 4 give the MATLAB command

>> y0=4 t : a vector of t values t

1

, t

2

. . . t n at which the approximate solution is given.

y : a vector of y values y

1

, y

2

. . . y n where y j is the approximate solution at t j

.

The command

>>plot(t,y) tells MATLAB to plot the solution, and you can save the figure in a convenient format for printing by pulling down the file menu in the figure window.

HOMEWORK

1. Use the above procedure to numerically solve y

0

= t

2

− y

3 y (1) = 4 on the interval 1 ≤ t ≤ 3 and plot the result. (Hand in the plot only, you should not print out a listing of all of the y n values!)

2. Numerically solve y

0

= y + t y (0) = 1 on the interval 0 ≤ t ≤ 1 and plot the result. Compare the numerical solution to the exact solution at t =

1

2 and t = 1.

1

Alternatively, you may define f by means of an m-file, if you know how to do this, in which case the calling command should be >>[t y] = ode45(’f’,tspan,y0) or >>[t y] = ode45(@f,tspan,y0)

1

MATLAB also contains easy to use commands for both the Laplace transform and the inverse Laplace transform. These require that the Symbolic Toolbox be installed

2

.

Enter the following commands for a simple demonstration:

>> syms t s

>> f=t*exp(-2*t)

>> laplace(f)

The answer will be the Laplace transform of f ( t ) = te

− 2 t

, expressed in the MATLAB syntax.

>> F=1/(s ˆ 3-s ˆ 2+s-1)

>> ilaplace(F)

The answer will be the inverse Laplace transform of F ( s ) = 1 / ( s

3 − s

2

+ s − 1).

The first MATLAB command above establishes t,s as symbolic expressions. Other expressions defined in terms of t,s , such as f, F are then automatically symbolic expressions also.

Complicated symbolic expressions may be constructed from simpler ones, for example

>> F1=3+exp(-s)

>> F2=s ˆ 2-4

>> F=F1/F2

If F is any symbolic expression the command

>> pretty(F) may be used to display an expression for F in a more easily readable format.

HOMEWORK

Use MATLAB as much as possible to do the following:

3) Find the Laplace transform of f ( t ) = 2

4) Find the inverse Laplace transform of t

F

2

( e s

− t cos (4 t ) +

) =

√ t .

e

− 4 s

(2 s +1) s 3 ( s 2 +2 s +5)

.

5) Solve the initial value problem y

000

+ 6 y

00

+ 15 y

0

+ 50 y = te

− 5 t

+ t

3 y (0) = 2 y

0

(0) = 1 y

00

(0) = 0

In each case your final answer should be written out and expressed in ordinary mathematical notation, not the MATLAB syntax. Note that Matlab uses the word ’Heaviside’ to denote the unit step function.

2 See me if you are not sure about this

2

Download