20
Ordinary Differential Equa- tions (ODEs) – Examples Daniel J. Bodony Department of Aerospace Engineering University of Illinois at Urbana-Champaign Video 17

Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Embed Size (px)

Citation preview

Page 1: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Ordinary Differential Equa-tions (ODEs) – Examples

Daniel J. BodonyDepartment of Aerospace EngineeringUniversity of Illinois at Urbana-Champaign

Video 17

Page 2: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

In this video you will learn. . .

1 How to integrate ODEs through three examples

2 How to plot the results

D. J. Bodony (UIUC) AE199 IAC Video 17 2 / 11

Page 3: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1a — Using Anonymous Functions

Learning how to integrate ODEs is best done with the simplest ofexamples first. We want to use ode45 to integrate

dy

dt= 1

subject to the initial condition y(0) = 0 from t = 0 to t = 1.

The solution is easy:

y(t) =

∫ t

01 dt = t + C

where C is a constant. To find the constant we use the initial conditiony(0) = 0 =⇒ y(0) = 0 + C = 0 =⇒ C = 0. Thus our solution is

y(t) = t.

D. J. Bodony (UIUC) AE199 IAC Video 17 3 / 11

Page 4: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1a — Using Anonymous Functions

Learning how to integrate ODEs is best done with the simplest ofexamples first. We want to use ode45 to integrate

dy

dt= 1

subject to the initial condition y(0) = 0 from t = 0 to t = 1.

The solution is easy:

y(t) =

∫ t

01 dt = t + C

where C is a constant. To find the constant we use the initial conditiony(0) = 0 =⇒ y(0) = 0 + C = 0 =⇒ C = 0. Thus our solution is

y(t) = t.

D. J. Bodony (UIUC) AE199 IAC Video 17 3 / 11

Page 5: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1a — Using Anonymous Functions

To use ode45, remember what it needs: ode45(@fun, tspan, ic).Let’s work on these one-by-one:

@fun The @fun refers to the right-hand-side function ~f (t, ~y) fromthe previous slides. In this example, we have f (t, y) = 1.Using an anonymous function we would set@fun = @(t,y) 1.

tspan The tspan is the row vector of the initial and final times, t0and tf . For this example, t0 = 0 and tf = 1. Thus tspan =

[0 1].

ic The ic is the row vector of the initial condition. For thisexample, y(0) = 0; thus, ic = [0].

Now go watch the video for the example running. . .

D. J. Bodony (UIUC) AE199 IAC Video 17 4 / 11

Page 6: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1a — Using Anonymous Functions

To use ode45, remember what it needs: ode45(@fun, tspan, ic).Let’s work on these one-by-one:

@fun The @fun refers to the right-hand-side function ~f (t, ~y) fromthe previous slides. In this example, we have f (t, y) = 1.Using an anonymous function we would set@fun = @(t,y) 1.

tspan The tspan is the row vector of the initial and final times, t0and tf . For this example, t0 = 0 and tf = 1. Thus tspan =

[0 1].

ic The ic is the row vector of the initial condition. For thisexample, y(0) = 0; thus, ic = [0].

Now go watch the video for the example running. . .

D. J. Bodony (UIUC) AE199 IAC Video 17 4 / 11

Page 7: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1b — Using Function m-files

Matlab also allows you to specify the right-hand-side function ~f (t, ~y)through function m-files, in addition to anonymous functions. For morecomplicated problems like you’ll see in AE3XX and AE4XX classes, usingthe m-file approach is more useful.

The only thing that changes is the @fun argument inode45(@fun, tspan, ic).

You now need to create a Matlab function file called fun.m that looks likethis:

function ydot = fun(t,y)

ydot = t;

return;

Go back to the video to watch this example.

D. J. Bodony (UIUC) AE199 IAC Video 17 5 / 11

Page 8: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1b — Using Function m-files

Matlab also allows you to specify the right-hand-side function ~f (t, ~y)through function m-files, in addition to anonymous functions. For morecomplicated problems like you’ll see in AE3XX and AE4XX classes, usingthe m-file approach is more useful.

The only thing that changes is the @fun argument inode45(@fun, tspan, ic).

You now need to create a Matlab function file called fun.m that looks likethis:

function ydot = fun(t,y)

ydot = t;

return;

Go back to the video to watch this example.

D. J. Bodony (UIUC) AE199 IAC Video 17 5 / 11

Page 9: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1b — Using Function m-files

Matlab also allows you to specify the right-hand-side function ~f (t, ~y)through function m-files, in addition to anonymous functions. For morecomplicated problems like you’ll see in AE3XX and AE4XX classes, usingthe m-file approach is more useful.

The only thing that changes is the @fun argument inode45(@fun, tspan, ic).

You now need to create a Matlab function file called fun.m that looks likethis:

function ydot = fun(t,y)

ydot = t;

return;

Go back to the video to watch this example.

D. J. Bodony (UIUC) AE199 IAC Video 17 5 / 11

Page 10: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 1b — Using Function m-files

Matlab also allows you to specify the right-hand-side function ~f (t, ~y)through function m-files, in addition to anonymous functions. For morecomplicated problems like you’ll see in AE3XX and AE4XX classes, usingthe m-file approach is more useful.

The only thing that changes is the @fun argument inode45(@fun, tspan, ic).

You now need to create a Matlab function file called fun.m that looks likethis:

function ydot = fun(t,y)

ydot = t;

return;

Go back to the video to watch this example.

D. J. Bodony (UIUC) AE199 IAC Video 17 5 / 11

Page 11: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 2 — Trajectory of a Sphere (Again!)

In our sphere example, the ODEs were (in first order form!)

d~v

dt=

~F

md~x

dt= ~v

for the vector trajectory ~x(t) = (x(t), y(t)). Let’s focus on only they -component:

dvydt

=−mg

m= −g

dy

dt= vy

where we have said that ~v = (vx , vy ) and used ~F = (0,−mg).

D. J. Bodony (UIUC) AE199 IAC Video 17 6 / 11

Page 12: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 2 — Trajectory of a Sphere (Again!)

So our system to be integrated by ode45 is

dvydt

= −g

dy

dt= vy .

How do we make this work? Well, let’s go back to ode45’s needs. The fullcommand is

[time, out] = ode45(@fun, tspan, ic);

The first thing we need is @fun. Let’s use an m-file for this and call ittrajectory.m. The name ‘fun’ is arbitrary; we’ll just call ode45 using@trajectory instead of @fun.

D. J. Bodony (UIUC) AE199 IAC Video 17 7 / 11

Page 13: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 2 — Trajectory of a Sphere (Again!)

So our system to be integrated by ode45 is

dvydt

= −g

dy

dt= vy .

How do we make this work? Well, let’s go back to ode45’s needs. The fullcommand is

[time, out] = ode45(@fun, tspan, ic);

The first thing we need is @fun. Let’s use an m-file for this and call ittrajectory.m. The name ‘fun’ is arbitrary; we’ll just call ode45 using@trajectory instead of @fun.

D. J. Bodony (UIUC) AE199 IAC Video 17 7 / 11

Page 14: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 2 — Trajectory of a Sphere (Again!)

So our system to be integrated by ode45 is

dvydt

= −g

dy

dt= vy .

How do we make this work? Well, let’s go back to ode45’s needs. The fullcommand is

[time, out] = ode45(@fun, tspan, ic);

The first thing we need is @fun. Let’s use an m-file for this and call ittrajectory.m. The name ‘fun’ is arbitrary; we’ll just call ode45 using@trajectory instead of @fun.

D. J. Bodony (UIUC) AE199 IAC Video 17 7 / 11

Page 15: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 2 — Trajectory of a Sphere (Again!)

We know trajectory.m must represent the right-hand-side function of~f (t, ~y), so what is the ~f ? The first thing to note is that the vector ~y refersto the unknowns so, in our case, ~y = (y , vy ). Thus, since

~y ′ =d~y

dt=

d

dt

[yvy

]= ~f (t, ~y),

we must have

~f (t, ~y) =

[vy−g

].

D. J. Bodony (UIUC) AE199 IAC Video 17 8 / 11

Page 16: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 2 — Trajectory of a Sphere (Again!)

Now that we know that

~f (t, ~y) =

[vy−g

],

building the trajectory.m file is easy. Here it is:

function ydot = trajectory(t,y)

% remember, y and ydot are vectors!

g = 9.81;

ydot(1) = y(2);

ydot(2) = -g;

return

Go to the video to watch the integration of[time,out] = ode45(@trajectory, tspan, ic).

D. J. Bodony (UIUC) AE199 IAC Video 17 9 / 11

Page 17: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 3 — Trajectory of a Sphere (Again Again!)

OK, I’m beating the trajectory example to death but bear with me. In ourlast example the Matlab function ode45 happily integrated to what everfinal time tf we gave it. How do we get it to stop when the sphere hits theground?

We use the ‘events’ option of ode45. The ball hitting the ground is anevent and Matlab will do special things at any event we choose. Type doc

odeset and search for the phrase ‘event location property.’

Create a Matlab function m-file called events.m that looks like:

function [value, isterminal, direction] = myevents(t,y)

% we want to stop the calculation when y = 0

% as the ball falls towards the ground

value = y(1); % the event is triggered when value = 0

isterminal = 1; % stop when value = 0

direction = -1; % only count when the solution is decreasing!

return

D. J. Bodony (UIUC) AE199 IAC Video 17 10 / 11

Page 18: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 3 — Trajectory of a Sphere (Again Again!)

OK, I’m beating the trajectory example to death but bear with me. In ourlast example the Matlab function ode45 happily integrated to what everfinal time tf we gave it. How do we get it to stop when the sphere hits theground?

We use the ‘events’ option of ode45. The ball hitting the ground is anevent and Matlab will do special things at any event we choose. Type doc

odeset and search for the phrase ‘event location property.’

Create a Matlab function m-file called events.m that looks like:

function [value, isterminal, direction] = myevents(t,y)

% we want to stop the calculation when y = 0

% as the ball falls towards the ground

value = y(1); % the event is triggered when value = 0

isterminal = 1; % stop when value = 0

direction = -1; % only count when the solution is decreasing!

return

D. J. Bodony (UIUC) AE199 IAC Video 17 10 / 11

Page 19: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 3 — Trajectory of a Sphere (Again Again!)

OK, I’m beating the trajectory example to death but bear with me. In ourlast example the Matlab function ode45 happily integrated to what everfinal time tf we gave it. How do we get it to stop when the sphere hits theground?

We use the ‘events’ option of ode45. The ball hitting the ground is anevent and Matlab will do special things at any event we choose. Type doc

odeset and search for the phrase ‘event location property.’

Create a Matlab function m-file called events.m that looks like:

function [value, isterminal, direction] = myevents(t,y)

% we want to stop the calculation when y = 0

% as the ball falls towards the ground

value = y(1); % the event is triggered when value = 0

isterminal = 1; % stop when value = 0

direction = -1; % only count when the solution is decreasing!

return

D. J. Bodony (UIUC) AE199 IAC Video 17 10 / 11

Page 20: Ordinary Di erential Equa- - Aeroacousticsacoustics.ae.illinois.edu/videos/AE199IAC-F13-Bodony-Video-17-2.pdf · Ordinary Di erential Equa-tions (ODEs) { Examples ... Example 1a |

Example 3 — Trajectory of a Sphere (Again Again!)

Then issue the ode45 command like this:

>> options = odeset(’Events’,@myevents);

>> [t,out] = ode45(@trajectory, [t0 tf], ic, options);

and see what happens!

D. J. Bodony (UIUC) AE199 IAC Video 17 11 / 11