differential equations and computer methods written by ...math237/maplenotescesar.pdf · left-hand...

100
MAPLE TM LAB MANUAL FOR MATH 237 Differential Equations and Computer Methods Written by CESAR O. AGUILAR QUEEN’S UNIVERSITY KINGSTON, ONTARIO, CANADA

Upload: others

Post on 13-Jul-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

MAPLETM LAB MANUAL FOR MATH 237

Differential Equations and Computer Methods

Written by

CESAR O. AGUILAR

QUEEN’S UNIVERSITYKINGSTON, ONTARIO, CANADA

Page 2: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side
Page 3: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Preface

This manual introduces the basic Maple tools needed to solve and interpretdifferential equations. It includes many examples relevant to the materialcovered in the course MATH 237 taken by the Mathematics and PhysicsEngineering students. It is recommended, although not required, that stu-dents read the material and complete the examples in the manual beforeattempting the weekly Maple lab assignments.

I would like to thank professor Ole A. Nielsen for his overall supervisionin the writing of this document. Your help and suggestions were greatlyappreciated.

Cesar AguilarAugust 2001

Page 4: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side
Page 5: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Contents

1 Functions, Expressions and Plotting 1

1.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Functions and Expressions . . . . . . . . . . . . . . . . . . . . 2

1.3 Sequences and Plotting . . . . . . . . . . . . . . . . . . . . . . 4

1.4 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.5 Getting Help in Maple . . . . . . . . . . . . . . . . . . . . . . 10

2 Plotting Structures and Direction Fields 11

2.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.2 Plot Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.3 Direction Fields . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3 Solving Differential Equations; dsolve 19

3.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.2 Solving ODE’s . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4 Picard Iterations 25

4.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

4.2 The map and nops Commands . . . . . . . . . . . . . . . . . . 25

4.3 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

4.4 The if Command . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.5 The Picard Scheme . . . . . . . . . . . . . . . . . . . . . . . . 28

4.6 Vector Version of the Picard Scheme . . . . . . . . . . . . . . 32

5 Numerical Solutions Part I 39

5.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

5.2 The numeric Option . . . . . . . . . . . . . . . . . . . . . . . 39

Page 6: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

vi CONTENTS

6 Numerical Solutions Part II 47

6.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476.2 Comparing Numerical Methods . . . . . . . . . . . . . . . . . 476.3 Choosing the Right Step Size . . . . . . . . . . . . . . . . . . 50

7 The Laplace Transform 53

7.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537.2 The laplace Command . . . . . . . . . . . . . . . . . . . . . 537.3 The invlaplace Command . . . . . . . . . . . . . . . . . . . 567.4 Piecewise Functions; Heaviside . . . . . . . . . . . . . . . . . . 577.5 Solving ODE’s with the Laplace Transform . . . . . . . . . . . 60

8 Systems of Ordinary Differential Equations 65

8.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658.2 Elementary Matrix Operations . . . . . . . . . . . . . . . . . . 658.3 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . 698.4 Solving Systems of Differential Equations . . . . . . . . . . . . 71

8.4.1 Eigenvector Method . . . . . . . . . . . . . . . . . . . 718.4.2 Using dsolve . . . . . . . . . . . . . . . . . . . . . . . 768.4.3 Laplace Transform . . . . . . . . . . . . . . . . . . . . 77

8.5 The Case of Repeated Eigenvalues . . . . . . . . . . . . . . . . 79

9 Phase Portraits 83

9.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839.2 Parametric Plots . . . . . . . . . . . . . . . . . . . . . . . . . 839.3 Plotting a Single Solution . . . . . . . . . . . . . . . . . . . . 859.4 Phase Portraits . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Page 7: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 1

Functions, Expressions and

Plotting

1.1 Purpose

The purpose of this lab is to review some of the basic syntax in Maple. Thetopics covered in this lab are:

1. functions and expressions

2. sequences and lists

3. basic plotting commands

4. Maple procedures and reading text files

5. getting help in Maple

The Maple commands used in this lab are:

simplify subs rhs diff

unapply proc lhs plot

int read seq

Page 8: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

2 Functions, Expressions and Plotting

1.2 Functions and Expressions

It is important to understand the difference between how Maple deals withfunctions and expressions. One of the main differences is that an expressioncannot be directly evaluated at a particular point, whereas a function can.To see this, first define the function

f(x) =

e2x + sin(x)

π2 + x

in Maple:

> f:=x->sqrt(exp(2*x)+sin(x))/(Pi^2+x);

f := x→√

e(2 x) + sin(x)

π2 + x

To define a function in Maple, we must use the “arrow” notation. You canthink of the symbol → as representing a transformation, that is the variablex is transformed into

e2x + sin(x)

π2 + x.

To evaluate f at π, we simply type:

> f(Pi);√e(2 π)

π2 + π

When possible, Maple can simplify expressions using the simplify com-mand:

> simplify(%);

π (π + 1)

The symbol % is used to recall the previous output.

Now assign the name h to the expression

ex sin(x2)− 1

x4 + 1

Page 9: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

1.2 Functions and Expressions 3

> h:=exp(x)*sin(x^2)-1/(x^4+1);

h := ex sin(x2)− 1

x4 + 1

The symbol := is called the assignment operator. In the above Maple com-mand, h has been assigned the expression

ex sin(x2)− 1

x4 + 1.

Now, h is an expression and not a function, so we cannot evaluate it for avalue x as we did with f . For example, try to evaluate h(0), the value of hat x = 0:

> h(0);

(ex)(0) sin(x2)(0)− 1

x(0)4 + 1

To evaluate h at 0 (or any point), we must use the substitute command subs:

> subs(x=0,h);

e0 sin(0)− 1

> simplify(%);

−1

We can avoid using the subs command in this case by converting the expres-sion h into a function with the unapply command. This command is veryuseful because the answers Maple gives to problems are often expressions.The basic calling command of unapply is

> func_name:=unapply(expr, x);

Thus with the above command, Maple will convert expr into a function (as-signing it the name func name) in the variable x.

Example 1. Use the unapply command to convert the expression h intothe function g and evaluate g at x = 0.

> g:=unapply(h,x);

Page 10: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4 Functions, Expressions and Plotting

g := x→ ex sin(x2)− 1

x4 + 1

> g(0);

−1

You can also assign a name to an equation. For example, assign the name rto the equation y sin(x)− 1 = ln(x2 + 1)− nx2:

> r:=y*sin(x)-1=ln(x^2+1)-n*x^2;

r := y sin(x)− 1 = ln(x2 + 1)− nx2

You can use the commands rhs and lhs to obtain the right-hand side andleft-hand side of this equation, respectively. Assign the name right to theright-hand side of equation r:

> right:=rhs(r);

right := ln(x2 + 1)− nx2

1.3 Sequences and Plotting

In Maple, the command seq is used to generate a sequence. The basic callingcommand of seq is

> seq(f, i=m..n)

The argument f (an expression) should depend on i. The arguments m andn are numerical values. The above command will produce the sequence

f(m), f(m+ 1), f(m+ 2), ..., f(n− 1), f(n).

Example 2. Generate a sequence of the first 10 odd numbers.

> seq(2*n+1,n=0..9);

Page 11: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

1.3 Sequences and Plotting 5

1, 3, 5, 7, 9, 11, 13, 15, 17, 19

You can even generate a sequence of expressions:

> exp_seq:=seq(x^2/n,n=1..5);

exp seq := x2,1

2x2,

1

3x2,

1

4x2,

1

5x2

Use square brackets to convert a sequence into a list:

> exp_list:=[exp_seq];

exp list :=

[

x2,1

2x2,

1

3x2,

1

4x2,

1

5x2]

The plot command is used to plot objects in Maple, where an ‘object’ cannot only be a function, but an expression, list, set, etc. The basic callingcommand of plot is

> plot(f, h, v, options);

The argument f to be plotted may be a function, an expression, a list or aset of functions, etc. The arguments h and v are the horizontal and vertical(optional) ranges, respectively.

Example 3. Plot the expression h from x = −1 to x = 3. Then plot thefunction g from x = −1 to x = 3.

To plot the expression h we type:

> plot(h,x=-1..3);

–5

0

5

10

15

–1 1 2 3x

Page 12: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

6 Functions, Expressions and Plotting

There are two ways to plot the function g from x = −1 to x = 3: either

> plot(g,-1..3);

–5

0

5

10

15

–1 1 2 3

or

> plot(g(x),x=-1..3);

–5

0

5

10

15

–1 1 2 3x

Notice that in plotting an expression it is necessary to specify the independentvariable in the range of the command plot whereas in plotting a function itis optional.

To plot the list of expressions exp list from x = −5 to x = 5, we type:

> plot(exp_list,x=-5..5);

Page 13: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

1.3 Sequences and Plotting 7

0

5

10

15

20

25

–4 –2 2 4x

It is also possible to generate a list of points and to plot them with connectinglines. (In fact, this is what Maple really does when asked to plot a functionor an expression.)

Example 4. Generate a list of five (x, y) values on the interval [−1, 3] usingthe function g and then use plot to plot them.

> gpoints:=[seq([i,g(i)],i=-1..3)];

gpoints :=

[[

− 1, e−1 sin(1)− 1

2

]

, [0,−1],

[

1, e1 sin(1)− 1

2

]

,

[

2, e2 sin(4)− 1

17

]

,

[

3, e3 sin(9)− 1

82

]]

> plot(gpoints,x=-1..3);

–4

–2

2

4

6

8

–1 1 2 3x

Page 14: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8 Functions, Expressions and Plotting

The reason the graph of gpoints looks jagged is because gpoints itself containsonly five points and its graph consists of those points joined by four straightlines. The graph could be made to look more like those of the function gand the expression h by using more points. Now generate a list of 401 (x, y)values using the function g and plot it:

> newgpoints:=[seq([i*0.01,g(i*0.01)],i=-100..300)]:

Notice the use of a colon to end this line in order to suppress the output.

> plot(newgpoints,x=-1..3);

–5

0

5

10

15

–1 1 2 3x

1.4 Procedures

The standard form of a procedure in Maple is:

> procedure_name:= proc(arguments)

> local variable1, variable2, ...;

> ....

> BODY OF PROCEDURE

> ....

> RETURN(some object);

> end;

There is no semi-colon at the end of the first line. It is not necessary toinclude a RETURN statement, but if it is not included then the procedurereturns the output of the last command executed. Here is an example of aprocedure that returns the sum of two numbers:

Page 15: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

1.4 Procedures 9

> add2numbers:=proc(x,y)

> RETURN(x+y);

> end;

add2numbers := proc(x, y) RETURN(x+ y) end

> add2numbers(237,732);

969

It is much more convenient and efficient to write a Maple procedure in a texteditor (such as Notepad) rather than directly in the Maple worksheet. Youcan then import the file with the command read.

Example 5. Start up a text editor and write a procedure that takes theintegral of an expression of x and then plots the result on the interval [a, b].

We can use the following (simplified) code for the procedure:

intplot:=proc(f,a,b)

local integral;

integral:=int(f,x);

RETURN(plot(integral, x=a..b));

end;

The name of the procedure is intplot and it accepts three arguments. Thefirst argument f is an expression, and the arguments a and b are the lowerand upper ranges used in the plot command. There is one local variablein the procedure, namely integral, and it is assigned the result of the int

command. The int command produces the integral of the expression f .

If your file is named ‘myprocedure.txt’ and is saved in the directory ‘mydir’in the ‘D:’ drive, then the file can be imported into Maple by the command

> read ‘‘D:\\mydir\\myprocedure.txt’’;

Note the double quotes and the double back-slashes in the read command.Now run the procedure intplot, supplying a simple expression and values fora and b:

> intplot(x^2,-3,3);

Page 16: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

10 Functions, Expressions and Plotting

–8

–6

–4

–2

2

4

6

8

–3 –2 –1 1 2 3x

1.5 Getting Help in Maple

There is a large amount of documentation in the Maple on-line help files.All of the Maple help files include examples on how to use the command inquestion and links to related procedures. To read a help file on a particulartopic or command type

> ?the_topic;

in Maple and if such a topic or command exists, Maple will display it’s helpfile. Open the help file for the command that differentiates an expression:

> ?diff;

The help file contains a description of the required and optimal arguments(or parameters) the procedure accepts as inputs and a description of theprocedure itself. The help file also contains examples of how the procedurecan be used.

Example 6. Differentiate the expression right defined earlier.

> diff(right,x);

2x

x2 + 1− 2nx

Page 17: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 2

Plotting Structures and

Direction Fields

2.1 Purpose

The purpose of this lab is to introduce further plotting capabilities of Maple.

The new Maple commands used in this lab are:

with(plots) with(DEtools)

display dfielplot

2.2 Plot Structures

When the plot command is executed in Maple, a PLOT data structureis produced that contains information (plot style, plot ranges, colour, etc.)about how to display the desired image. The PLOT structure is a regularexpression, and hence can be manipulated and displayed like any other Mapleexpression. For example, we can assign the name F to the plot of sin(x) fromx = 0 to x = 2π:

> F:=plot(sin(x),x=0..2*Pi);

Page 18: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

12 Plotting Structures and Direction Fields

F :=PLOT(CURVES([[0, 0],

[.1369555585245651, .1365278179668514],

[.2561198576993970, .2533288954318780],

[.3901325751264872, .3803110317172750],

[.5250336158355156, .5012420929778901],

[.6592934954461389, .6125585658925692],

[.7837695903060641, .7059542689071320],

[.9126573765967170, .7911319037633898],

...

[5.369630104513557, −.7916807361706871],[5.501787834528122, −.7042722143764111],[5.623227063040633, −.6130838646330965],[5.762410123325877, −.4975527079022725],[5.886897111658307, −.3859968692352789],[6.019626205745633, −.2605183963436125],[6.146664884637999, −.1360967435821896],[6.283185294620000, −.1255958650482642 10−7]],

COLOUR(RGB , 1.0, 0, 0)),AXESLABELS(“x”, ),

VIEW(0..6.283185308, DEFAULT ))

Notice that one of the components of F is a list of coordinate pairs. The othercomponents of F describe how the PLOT structure should be displayed. Nowwe can use the display command to display the PLOT structure F :

> display(F);

Why didn’t that work? The reason why display did not produce the imageis because the display procedure has not been loaded. This procedure ispart of the plots package. The plots package in Maple contains many usefulplotting commands; display is one of them and we cannot gain direct accessto it until we load it. To load the package type

> with(plots);

Page 19: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

2.2 Plot Structures 13

[animate, animate3d , animatecurve, changecoords, complexplot, complexplot3d ,

conformal , contourplot , contourplot3d , coordplot , coordplot3d , cylinderplot ,

densityplot , display, display3d , fieldplot , fieldplot3d , gradplot , gradplot3d ,

implicitplot , implicitplot3d , inequal , listcontplot , listcontplot3d , listdensityplot ,

listplot , listplot3d , loglogplot , logplot , matrixplot , odeplot, pareto, pointplot ,

pointplot3d , polarplot , polygonplot , polygonplot3d , polyhedra supported ,

polyhedraplot , replot , rootlocus , semilogplot, setoptions, setoptions3d ,

spacecurve, sparsematrixplot, sphereplot, surfdata, textplot , textplot3d , tubeplot ]

The output of with(plots) is a list of all the commands that are part of theplots package. Notice that display is one of them.

Now use the display command to produce the graph of sin(x):

> display(F);

–1

–0.5

0

0.5

1

1 2 3 4 5 6x

Example 1. Assign the name G the plot of cos(x) from x = 0 to x = 2πand use display to display G and F together.

> G:=plot(cos(x),x=0..2*Pi,colour=blue):

A colon is used to suppress the Maple output. Now use the display com-mand to display F and G together:

> display([F,G]);

Page 20: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

14 Plotting Structures and Direction Fields

–1

–0.5

0

0.5

1

1 2 3 4 5 6x

2.3 Direction Fields

The dfieldplot command in Maple can be used to produce the directionfield associated with a first order differential equation. You need to first loadthe DEtools package in Maple to directly use dfieldplot:

> with(DEtools);

[DEnormal , DEplot , DEplot3d , DEplot polygon, DFactor , Dchangevar ,

GCRD , LCLM ,PDEchangecoords, RiemannPsols, abelsol , adjoint ,

autonomous , bernoullisol , buildsol , buildsym, canoni , chinisol , clairautsol ,

constcoeffsols, convertAlg , convertsys , dalembertsol , de2diffop, dfieldplot,

diffop2de, eigenring, endomorphism charpoly , equinv , eta k , eulersols,

exactsol , expsols, exterior power , formal sol , gen exp, generate ic,

genhomosol , hamilton eqs , indicialeq, infgen, integrate sols , intfactor ,

kovacicsols, leftdivision, liesol , line int , linearsol , matrixDE , matrix riccati ,

moser reduce , mult , newton polygon, odeadvisor , odepde, parametricsol ,

phaseportrait , poincare, polysols, ratsols , reduceOrder , regular parts ,

regularsp, riccati system , riccatisol , rightdivision, separablesol ,

super reduce, symgen, symmetric power , symmetric product , symtest ,

transinv , translate, untranslate , varparam, zoom]

The basic calling command of dfieldplot is

Page 21: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

2.3 Direction Fields 15

> dfieldplot(ode,variable,xrange,yrange);

1. ode: an ordinary first order differential equation

2. var : dependent variable

3. xrange: range of the independent variable

4. yrange: range of the dependent variable

Example 2. Use dfieldplot to graph the direction field associated withthe differential equation dy/dx = e−x − 2y.

> dfieldplot(diff(y(x),x)=exp(-x)-2*y(x),y(x),x=-1..1,y=-1..1);

–1

–0.5

0

0.5

1

y(x)

–1 –0.5 0.5 1x

Alternatively, you can first assign a name to the differential equation:

> diff_eqn:=diff(y(x),x)=exp(-x)-2*y(x);

diff eqn :=∂

∂xy(x) = e(−x) − 2 y(x)

Then it is possible to graph the direction field associated with it by typing:

> dfieldplot(diff_eqn,y(x),x=-1..1,y=-1..1);

Page 22: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

16 Plotting Structures and Direction Fields

–1

–0.5

0

0.5

1

y(x)

–1 –0.5 0.5 1x

Notice that in either case you must write y(x) and not y for the dependentvariable, and it is necessary to include the ranges for both the dependent andindependent variables.

Example 3. A particular solution to the above differential equation is y(x) =e−x + 2e−2x. Graph this solution together with the direction field usingdisplay.

First we need to assign a name to the direction field plot:

> diff_field:=dfieldplot(diff_eqn,y(x),x=-1/2..3,y=-1..1,

colour=gray):

Second, assign a name to the plot of the solution y(x):

> diff_sol:=plot(exp(-x)+2*exp(-2*x),x=-1/2..3,colour=blue,

thickness=2):

Now use the display command to display the solution and the direction fieldof the differential equation:

> display([diff_sol,diff_field]);

Page 23: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

2.3 Direction Fields 17

–1

–0.5

0

0.5

1

–0.6 0.20.40.60.8 1 1.21.41.61.8 2 2.22.42.62.8 3x

Page 24: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

18 Plotting Structures and Direction Fields

Page 25: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 3

Solving Differential Equations;

dsolve

3.1 Purpose

The purpose of this lab is to introduce the dsolve command. This built inMaple command is the most commonly used to solve differential equations.

The new Maple commands used in this lab are:

dsolve contourplot

evalf solve

3.2 Solving ODE’s

To begin using the built-in differential equation commands, you must firstload the DEtools package:

> with(DEtools):

The most commonly used command to solve differential equations in Mapleis the dsolve command. The basic calling command of the dsolve commandis

> dsolve(some_diff_eqn,variable,options);

Using this format of dsolve, Maple will attempt to produce a formula forthe general solution of the ODE. To solve initial-value problems you type

Page 26: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

20 Solving Differential Equations; dsolve

> dsolve(some_diff_eq,initial_cond,variable,options);

If possible, Maple will return the solution in explicit form. Otherwise, it willreturn it implicitly. Occasionally the implicit solution is less complicatedthan the explicit one and the implicit option forces Maple to return thesolution implicitly.

Example 1. Solve the initial-value problem

dy

dx=x

y, y(0) = 0.

First you can assign a name to the differential equation:

> de1:=diff(y(x),x)=x/y(x);

de1 :=∂

∂xy(x) =

x

y(x)

Now use the dsolve command:

> de1sol_explicit:=dsolve(de1,y(0)=0,y(x));

de1sol explicit := y(x) =√x2, y(x) = −

√x2

Maple returns two solutions to the initial-value problem. If we include theimplicit option in the dsolve command, Maple returns the solution implic-itly:

> de1sol_implicit:=dsolve(de1,y(0)=0,y(x),implicit);

de1sol implicit := y(x)2 − x2 − C1 = 0

Constants supplied by Maple always begin with the underscore character.In this case, Maple seems to ignore the initial-condition when using the im-

plicit option. Applying the initial condition we obtain C1 = 0. Hence thesolutions to the differential equation that pass through the origin are y = xand y = −x. To plot the solution with the direction field first assign a nameto the direction field:

> with(plots):

> de1_field:=dfieldplot(de1,y(x),x=-3..3,y=-3..3,colour=gray):

Page 27: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

3.2 Solving ODE’s 21

Now assign a name to the plot of the solutions:

> de1sol_plot:=plot([x,-x],x=-3..3,colour=blue,thickness=2):

Now use display to produce a graph of both plots together:

> display(de1_field,de1sol_plot);

–3

–2

–1

1

2

3

–3 –2 –1 1 2 3x

Example 2. Solve 2x sin y dx + (x2 cos y − 1)dy = 0 subject to the initialcondition y(0) = 1/2.

This differential equation is exact because

∂y(2x sin y) =

∂x(x2 cos y − 1)

> de2:=2*x*sin(y(x))+(x^2*cos(y(x))-1)*diff(y(x),x)=0;

de2 := 2 x sin(y(x)) + (x2 cos(y(x))− 1)

(

∂xy(x)

)

= 0

We can obtain the general solution to the ODE using dsolve:

> de2_gensol2:=dsolve(de2,y(x));

de2 gensol2 := C1 + x2 sin(y(x))− y(x) = 0

The graph of x2 sin y−y = k for various values of k is just the level curves ofthe function f(x, y) = x2 sin y − y. Therefore, we can use the contourplot

(part of the plots package) command using the function f(x, y) to graphsome of the solutions of the ODE:

Page 28: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

22 Solving Differential Equations; dsolve

> f:=subs(_C1=0,y(x)=y,lhs(de2_gensol2));

f := x2 sin(y)− y

> contourplot(f,x=-5..5,y=-5..5);

–4

–2

2

4

y

–4 –2 2 4x

To solve the initial-value problem, we use dsolve:

> de2_sol3:=dsolve(de2,y(0)=1/2,y(x));

de2 sol3 := y(x) = RootOf

(

1

2+ x2 sin( Z )− Z

)

Alternatively, we can substitute y(x) = 1/2 and x = 0 into the the generalsolution, and then solve for C1:

> C:=solve(subs(y(x)=1/2,x=0,lhs(de2_gensol2)));

C :=1

2

Now substitute C into the the general solution to obtain the solution of theIVP (also substitute y(x) = y):

> ivp_sol:=subs(_C1=C,y(x)=y,lhs(de2_gensol2));

ivp sol :=1

2+ x2 sin(y)− y

To plot the solution, we use the implicitplot (part of the plots package)command:

Page 29: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

3.2 Solving ODE’s 23

> implicitplot(ivp_sol,x=-10..10,y=-10..10);

–8

–6

–4

–2

2

4

6

8

y

–10 –5 5 10x

Example 3. Plot the solution of the initial-value problem

y′ − sin(2πx)y = 1, y(0) = 1

together with the associated direction field on the interval [0, 2π].

For convenience, we assign an expression name to the ODE, and then solveit using dsolve:

> de3:=diff(y(x),x)-sin(2*Pi*x)*y(x)=1;

de3 :=

(

∂xy(x)

)

− sin(2 π x) y(x) = 1

> de3_sol:=dsolve(de3,y(0)=1,y(x));

de3 sol := y(x) =e

−1

2

−1 + cos(2 π x)

π

∫ x

0

e

1

2

−1 + cos(2 π u)

π

du+

e

−1

2

−1 + cos(2 π x)

π

Now define the solution as a function:

Page 30: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

24 Solving Differential Equations; dsolve

> g:=unapply(rhs(de3_sol),x);

g :=x → e

−1

2

−1 + cos(2 π x)

π

∫ x

0

e

1

2

−1 + cos(2 π u)

π

du+

e

−1

2

−1 + cos(2 π x)

π

Evaluate g at x = 1:

> g(1);

∫ 1

0

e

1

2

−1 + cos(2 π u)

π

du+ 1

> evalf(g(1));

1.858273585

The command evalf is used to obtain a floating point approximation of g(1).Now we plot the direction field together with the solution:

> de3_field:=dfieldplot(de3,y(x),x=0..2*Pi,y=0..8):

> de3_plot:=plot(g,0..2*Pi,colour=blue,thickness=2):

> display(de3_field,de3_plot);

2

4

6

8

1 2 3 4 5 6

Page 31: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 4

Picard Iterations

4.1 Purpose

The purpose of this lab is to use a Maple procedure that implements thePicard iteration scheme for a differential equation. Several examples willbe used to illustrate how the approximate solutions produced by the Picardscheme become better and better approximations to the exact solution asthe number of iterations become larger and larger.

The new Maple commands used in this lab are:

map nops for nargs

if op do ERROR

4.2 The map and nops Commands

The map command is most commonly used to apply a function (or procedure)to the individual components of an expression, such as a matrix, a list, or anarray. The basic calling command for map is

> map(some_function, some_expr, arguments);

For example, suppose that you are given the array

v = [ln(x2), x2 − cos(2πx3), ex2

]

Page 32: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

26 Picard Iterations

and wish to calculate the derivatives of each element. To do this in Maple,you can first assign a name to the array and then use the map command tofind the derivative of each element:

> v:=[ln(x^2),x^2-cos(2*Pi*x^3),exp(x^2)];

v :=[

ln(x2), x2 − cos(2 π x3), e(x2)]

> dv:=map(diff, v, x);

dv :=

[

21

x, 2 x+ 6 sin(2 π x3) π x2, 2 x e(x

2)

]

You can view the help file of map to see the different ways it can be used.

The nops command returns the number of components (operands) of anobject. For example, the array v contains three components, and so typing

> nops(v);

3

returns the number three. Quickly browse the help file of nops.

4.3 The for Loop

By now, you have probably encountered loop commands in a computer pro-gramming course. Loops are used to “execute a statement sequence repeat-edly, either for a counted number of times or until a condition is satisfied”.The loop command we will use in this lab is the for loop. The standardformat of the for loop in Maple is

for variable from start to finish

do

...

statement

...

od;

Page 33: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4.4 The if Command 27

The od statement is used to end the do statement. There are several optionsthat can be used with the for loop, such as the while and if statements.Here’s a simple example of how the for loop can be used:

> for i from 1 to 10 do print(i) od;

1

2

3

4

5

6

7

8

9

10

You can view the help file of the for loop to view additional examples.

4.4 The if Command

The if command is used to execute a statement provided a given conditionis satisfied. The standard format of the if command is the following:

if condition then

...

statement

...

fi;

The command fi is used to end the if command. The condition to besatisfied can include the relational operators <, >, =, and <> (not equal),the logical operators and, or and not, and the logical names true and false.More complicated if statements can be written by using the expressions elseand elif. You can view the help file of if to view some examples of howthe command can be used.

Page 34: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

28 Picard Iterations

4.5 The Picard Scheme

There are several methods that can be used to write the code of a procedurethat implements the Picard scheme. In this lab, we will use a for loop to doso.

If you feel confident enough and wish to experiment, you can write yourown procedure that implements the Picard scheme in a text editor and thenimport it into Maple. If not, you can simply use the one given below:

picards:=proc(f,t0,x0,n)

local k, x;

if nargs<>4 then ERROR(‘FOUR ARGUMENTS REQUIRED‘); fi;

if type(f,procedure)=false then

ERROR(‘FIRST ARGUMENT MUST BE A FUNCTION‘);

fi;

if n<0 then

ERROR(‘FOURTH ARGUMENT MUST BE A NON-NEGATIVE INTEGER‘,n);

fi;

x:=x0;

if n=0 then RETURN(x0); fi;

for k from 1 to n do

x:=x0 + int(f(_a,subs(t=_a,x)),_a=t0..t);

od;

RETURN(x);

end;

This procedure is used to find the Picard iterates of the solution of the initialvalue problem

x′ = f(t, x), x(t0) = x0.

The picards procedure contains the arguments f , t0, x0 and n, the desirednumber of iterations.

The first if statement in the procedure tests whether the number of argu-ments entered by the user is equal to 4 (the number of required argumentsneeded to execute the procedure). This is done using nargs. Within a proce-dure, Maple automatically assigns the name nargs the number of arguments

Page 35: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4.5 The Picard Scheme 29

the user entered when calling the procedure. Hence, if nargs is not equal to4, the procedure will be terminated and an error message will be displayed(this is done using the ERROR command).

After defining the local variables, the procedure tests whether the argumentf is a function (or, more precisely, a procedure) and if n is a non-negativeinteger. If either of these conditions fails, an error message will be dis-played.The procedure then assigns the quantity x0 to the name x (x0 is thefirst approximation to the solution). If the user inputs 0 as the number ofiterations desired, the procedure will return the initial condition x0. Theprocedure uses a for loop to perform the iterations. The int command andits arguments represent the integral

∫ t

t0

f(a, x(a)) da.

The subs command must be used because, after iterating, x is dependenton t, and the dummy variable in the integral is a. Write out the first fewiterations on paper so that you can clearly understand what the procedureis doing.

Example 1. Let’s try our procedure using the initial-value problem

dy

dx= sin(x), y(0) = 3.

The solution to this problem is y(x) = 4 − cos(x). Import the file whichcontains the picards procedure into Maple using the read command and usepicards to obtain the first three iterations of the Picard scheme:

> f1:=(x,y)->sin(x);

f1 := (x, y) → sin(x)

> picards(f1,0,3,1);

4− cos(t)

> picards(f1,0,3,2);

Page 36: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

30 Picard Iterations

4− cos(t)

> picards(f1,0,3,3);

4− cos(t)

Or just simply

> seq(picards(f1,0,3,i),i=1..3);

4− cos(t), 4− cos(t), 4− cos(t)

The procedure returns the exact solution after only the first iteration. Tryperforming the first iteration by hand to see that this is in fact the case.

Example 2. Use Picard’s iteration method to approximate the solution ofthe initial value problem

y′ = 2t(1 + y), y(0) = 0.

We can obtain the first six approximations to the solution using the picards

procedure:

> f2:=(t,y)->2*t*(1+y);

f2 := (t, y) → 2 t (1 + y)

> de1_app:=[seq(picards(f2,0,0,i),i=1..6)];

de1 app :=

[

t2,1

2t4 + t2,

1

6t6 +

1

2t4 + t2,

1

24t8 +

1

6t6 +

1

2t4 + t2,

1

120t10 +

1

24t8 +

1

6t6 +

1

2t4 + t2,

1

720t12 +

1

120t10 +

1

24t8 +

1

6t6 +

1

2t4 + t2

]

We can graph the approximations to see how they behave as more iterationsare performed:

> plot(de1_app,t=-2..2);

Page 37: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4.5 The Picard Scheme 31

0

10

20

30

40

–2 –1 1 2t

From the graph we can observe that the approximations seem to be converg-ing to a particular function. Are they converging to the solution of the IVP?To answer this we can use dsolve to obtain the exact solution of the IVPand compare its graph to the graph of the approximations:

> de1:=diff(y(t),t)=2*t*(1+y(t));

de1 :=∂

∂ty(t) = 2 t (1 + y(t))

> exactsol:=rhs(dsolve(de1,y(0)=0,y(t)));

exactsol := −1 + e(t2)

Now assign names to the plots of the solution and the approximations anduse display to display them in a single graph:

> exact_plot:=plot(exactsol,t=-2..2,colour=black,thickness=2):

> approx_plot:=plot(de1_app,t=-2..2):

> display(approx_plot,exact_plot);

0

10

20

30

40

50

–2 –1 1 2t

Page 38: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

32 Picard Iterations

From the graph we can conclude that the approximations do indeed convergeto the solution of the initial value problem.

Observations: You may have noticed that the approximations to the solu-tion are the partial sums of the infinite series

∞∑

n=1

t2n

n!.

We can use the ratio test on the above infinite series to conclude that itconverges for all t. Therefore the limit of the sequence of functions

y1, y2, y3, y4, . . .

where yi+1 =∫ t

02s(1 + yi(s)) ds, exists and

limn→∞

yn(t) =∞∑

n=1

t2n

n!.

In other words, the solution y(t) can be expressed as an infinite series, namely∞∑

n=1

t2n

n!. Can you write this infinite series as an elementary function?

(Hint: ex =

∞∑

n=0

xn

n!).

4.6 Vector Version of the Picard Scheme

The Picard scheme can also be used for vector differential equations. Theprocedure for the vector version of the Picard scheme is very similar to thatof the scalar one. Here it is:

picardv:=proc(f,t0,x0,n)

local k, x;

if nargs<>4 then ERROR(‘FOUR ARGUMENTS REQUIRED‘); fi;

if type(f,procedure)=false then

Page 39: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4.6 Vector Version of the Picard Scheme 33

ERROR(‘FIRST ARGUMENT MUST BE A FUNCTION‘);

fi;

if type(x0,list)=false then

ERROR(‘THIRD ARGUMENT MUST BE A LIST‘);

fi;

if n<0 then

ERROR(‘FOURTH ARGUMENT MUST BE A NON-NEGATIVE INTEGER‘,n);

fi;

if nops(f(t,x)) <> nops(x0) then

ERROR(‘DIMENSIONS OF FIRST AND THIRD ARGUMENTS DO NOT

MATCH‘);

fi;

x:=x0;

if n=0 then RETURN(x0); fi;

for k from 1 to n do

x:=x0 + map(int, f(_a,subs(t=_a,x)), _a=t0..t);

od;

RETURN(x);

end;

After defining the local variables and testing whether f is a procedure, x0 alist, and n a non-negative integer, the procedure tests whether the numberof components of the vector function f and the number of components ofthe array x0 match. The rest of the procedure is basically the same as thescalar version, with the exception that the integration is performed on thecomponents of f . This is accomplished using the map command. Return toyour text editor and edit your picards procedure so that it is the same as theone given above. Make sure to name the procedure picardv and save the filewith a different name. Thus, you should have a file containing the picards

procedure and one containing the picardv procedure.

Example 3. Let’s test picardv by solving the initial value problem

y′′ − y′ − 2y = t,

y(0) = 1/4, y′(0) = −1/2.

Page 40: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

34 Picard Iterations

First we will use dsolve to obtain the solution of the the IVP so that we canlater compare the approximations to it:

> de2:=diff(y(t),t$2)-diff(y(t),t)-2*y(t)=t;

de2 :=

(

∂2

∂t2y(t)

)

−(

∂ty(t)

)

− 2 y(t) = t

> sol2:=rhs(dsolve(de2,y(0)=1/4,D(y)(0)=-1/2,y(t)));

sol2 :=1

4− 1

2t

Now we write this second-order linear IVP in matrix form:

d

dt

[

yy′

]

=

[

0 12 1

] [

yy′

]

+

[

0t

]

=

[

y′

2y + y′ + t

]

,

[

y(0)y′(0)

]

=

[

1/4−1/2

]

Or if we let z = y′, then

d

dt

[

yz

]

=

[

z2y + z + t

]

,

[

y(0)z(0)

]

=

[

1/4−1/2

]

You can think of this last expression as a system of two unknowns, wherethe unknowns are the functions y and z. We can now define the necessaryexpressions that will be used in the picardv procedure:

> t0:=0;

t0 := 0

> y0:=[1/4,-1/2];

y0 :=

[

1

4,−1

2

]

> f3:=(t,u)->[u[2],2*u[1]+u[2]+t];

Page 41: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4.6 Vector Version of the Picard Scheme 35

f3 := (t, u) → [u2, 2 u1 + u2 + t]

The variable u is a list, where u = [y, z]. By typing u[i], Maple will returnthe ith component of u.

Now use five iterations to obtain an approximation to the solution of theIVP:

> Y:=picardv(f3,t0,y0,5);

Y :=

[

1

144t6 − 1

2t +

1

4,11

720t6 − 1

2

]

What picardv returns is a list, where the first component is an approxima-tion to y and the second component is an approximation to z (= y′). Toextract the first component of the list Y , simply type Y[1] (you can also usethe op command). Now we can plot the approximation to the IVP togetherwith the exact solution:

> plot(Y[1],sol2,t=-3..3,y=-3..3);

–3

–2

–1

1

2

3

y

–3 –2 –1 1 2 3t

From the graph we can observe that the approximation follows the solutionvery closely near t = 0. Return to the line in your Maple worksheet whereyou defined Y and change the number of iterations to ten. Now re-plotthe solution and the new approximation on the intervals −4 ≤ t ≤ 4 and−4 ≤ y ≤ 4. Notice that the approximation now follows the solution fartherfrom t = 0 than the previous one. Repeat the same procedure for higheriterations.

Page 42: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

36 Picard Iterations

The advantage of the procedure picardv is that it can also be used to solvescalar differential equations. When used to solve scalar equations, its outputis a list with a single component, which is the approximation to the solution.For the procedure to work with scalar equations, the initial condition y0 mustbe defined as a one component list. Let’s run the picardv and picards

procedures on the IVP given below.

Example 4.dx

dt= −x/2 + t, x(0) = 0.

First define the functions for the scalar and vector version, and then theinitial conditions:

> scalar_fn:=(t,x)->-x/2+t;

scalar fn := (t, x) → −1

2x+ t

> vector_fn:=(t,u)->[-u[1]/2+t];

vector fn := (t, u) →[

−1

2u1 + t

]

> t0:=0;

t0 := 0

> x0:=0;

x0 := 0

Now try the procedures using two iterations:

> picards(scalar_fn,t0,x0,2);

− 1

12t3 +

1

2t2

> picardv(vector_fn,t0,[x0],2);

[

− 1

12t3 +

1

2t2]

Try the procedures with five iterations:

Page 43: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

4.6 Vector Version of the Picard Scheme 37

> picards(scalar_fn,t0,x0,5);

1

11520t6 − 1

960t5 +

1

96t4 − 1

12t3 +

1

2t2

> picardv(vector_fn,t0,[x0],5);

[

1

11520t6 − 1

960t5 +

1

96t4 − 1

12t3 +

1

2t2]

Both procedures return the same approximations for the same number ofiterations. Hence the advantage lies on the side of the vector procedure.

Page 44: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

38 Picard Iterations

Page 45: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 5

Numerical Solutions Part I

5.1 Purpose

The purpose of this lab is to introduce the built in Maple commands thatcan be used to solve differential equations numerically.

The new Maple commands used in this lab are:

dsolve(..., numeric)

odeplot

5.2 The numeric Option

To use the Maple built in commands to obtain numerical solutions to differ-ential equations, you include the numeric option in the dsolve command.The default numerical method used by dsolve is a Fehlberg fourth-fifth orderRunge-Kutta method (RKF45). Type

> ?dsolve/numeric

to view the help file for the numeric option. Notice the different methodsthat can be used when using the numeric option in dsolve.

Example 1. Lets investigate the use of the numeric option by numericallysolving the initial value problem

dx

dt=t

x, x(0) = 1.

Page 46: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

40 Numerical Solutions Part I

The exact solution to this problem is

> X:=t->sqrt(t^2+1);

X := t→√t2 + 1

Once we obtain an approximate solution to the initial value value problemusing dsolve with the numeric option, we will be able to compare it to X(t).

Now let’s obtain an approximate solution to the initial value problem:

> de1:=diff(x(t),t)=t/x(t),x(0)=1;

de1 := ∂∂t

x(t) =t

x(t), x(0) = 1

> appsol:=dsolve(de1,x(t),numeric);

appsol := proc(rkf45 x ) . . . end

The output is a Maple procedure, which we have named appsol. If we type

> appsol(2);

[t = 2, x(t) = 2.236067989635276]

Maple returns a list of two elements. The first element is the value of t we wishto evaluate the approximate solution at, and the second is the approximatevalue toX(t). We can define a function Xa that only returns the approximatevalue by typing

> Xa:=u->subs(appsol(u),x(t));

Xa := u→ subs(appsol(u), x(t))

So typing

> Xa(2);

2.236067989635276

will just return the approximate value of X(2) =√5.

Now we can compare the approximate solution Xa to the exact solution X .To do so, we can generate a table containing rows of the form [t, Xa(t), X(t)]:

Page 47: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

5.2 The numeric Option 41

> for i from 0 to 10 do print([i,Xa(i),evalf(X(i))]) od;

[0, 1., 1.]

[1, 1.414213570626583, 1.414213562]

[2, 2.236067988096750, 2.236067978]

[3, 3.162277670410246, 3.162277660]

[4, 4.123105634716530, 4.123105626]

[5, 5.099019521108344, 5.099019514]

[6, 6.082762537292338, 6.082762530]

[7, 7.071067818062475, 7.071067810]

[8, 8.062257753788231, 8.062257748]

[9, 9.055385143043786, 9.055385138]

[10, 10.04987562554892, 10.04987562]

From the table, we can observe that the approximate solution returns valuesthat are eight digits correct. You can also plot Xa and X to see that thegraphs of them are indistinguishable:

> plot(Xa,X, 0..10);

2

4

6

8

10

0 2 4 6 8 10

We can limit the number of function evaluations that Maple performs whencomputing numerical solutions by including the maxfun option in thedsolve(..., numeric) command. The default number of evaluations al-lowed is 50,000. Unfortunately, performing such a large number of computa-tions can be limited by computer power and time. In most cases, a value of1000 is appropriate for maxfun.

Page 48: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

42 Numerical Solutions Part I

Add the option maxfun=1000 in the definition of appsol and re-executethe command defining Xa. Now re-generate the table of the approximatevalues and re-plot Xa and X . Notice that the approximate values are stillvery close to the true values, and that the graphs of Xa and X are stillindistinguishable.

An important option that should (almost always) be included in thedsolve(..., numeric) command is the startinit=TRUE option. Thedefault value for startinit is FALSE. By setting startinit=TRUE, youinstruct Maple to use the original initial condition when computing an ap-proximate value of the exact solution, instead of using the closer of the lastinitial condition (i.e. the closer of the last computed value).

Example 2. Consider the initial value problem

dy

dx= 1− 2xy, y(0) = 0.

This initial value problem has an exact solution:

> de2:=diff(y(x),x)=1-2*x*y(x),y(0)=0;

de2 := ∂∂x

y(x) = 1− 2 x y(x), y(0) = 0

> sol2:=rhs(dsolve(de2,y(x)));

sol2 := −1

2I e(−x2)

√π erf(I x)

> exactsol:=unapply(sol2,x);

exactsol := x→ −1

2I e(−x2)

√π erf(I x)

Here is the graph of the exact solution:

> plot(exactsol, -5..5);

Page 49: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

5.2 The numeric Option 43

–0.4

–0.2

0.2

0.4

–4 –2 2 4

Now let’s obtain a numerical approximation to the solution without settingstartinit=TRUE and then graphing it:

> appsol:=dsolve(de2,y(x),numeric,maxfun=1000);

appsol := proc(rkf45 x ) . . . end

> numericsol:=u->subs(appsol(u),y(x));

numericsol := u→ subs(appsol(u), y(x))

> plot(numericsol, -5..5);

–0.4

–0.2

0

0.2

0.4

–4 –2 2 4

Notice the error in the graph of the approximation. This error can be avoidedby setting startinit=TRUE. Set startinit=TRUE in the definition ofappsol and re-define appsol. Re-execute the command defining numericsoland obtain its new graph.

Page 50: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

44 Numerical Solutions Part I

As previously mentioned, by setting startinit=TRUE, Maple will alwaysuse the original initial condition to calculate numericsol(x). See page 79 ofthe Maple companion for another illustration of this important option.

Another method for plotting numerical solutions obtained by callingdsolve(..., numeric) is to use the odeplot command. This commandis part of the plots package. The basic calling command for odeplot is:

> odeplot(s, vars, range);

The argument s is the output from a call to dsolve(..., numeric). Thevars argument must be a list of the variables.

Example 3. Use odeplot to plot the numerical solution of the initial valueproblem

dx

dt= t + x2, x(0) = 1.

> de3:=diff(x(t),t)=t+x(t)^2,x(0)=1;

de3 := x(0) = 1,∂

∂tx(t) = t+ x(t)2

> s1:=dsolve(de3,x(t),numeric,maxfun=1000,startinit=TRUE);

s1 := proc(rkf45 x ) . . . end

> odeplot(s1, [t,x(t)], -2..2);

2

4

6

8

10

12

–2 –1.5 –1 –0.5 0 0.5

Now try using the classical rk3 method with a step size of h = 0.001.

Page 51: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

5.2 The numeric Option 45

> s2:=dsolve(de3,x(t),numeric,method=classical[rk3],

maxfun=1000,startinit=TRUE,stepsize=0.001);

s2 := proc(x classical) . . . end

> odeplot(s2, [t,x(t)], -2..2);

0.8

0.9

1

1.1

1.2

1.3

1.4

–1.6 –1.4 –1.2 –1 –0.8 –0.6 –0.4 –0.2 0 0.2

It seems that the rk3 method ceases to produce values at around t = 0.26.The reason for this is because we have set maxfun to small. In this example,it is wise to accept the default value for maxfun. Re-define s2 without themaxfun option and re-plot s2.

From the the graph of the numerical solution produced by both numeri-cal methods, can you determine the interval in which the solution exists?(Hint: What happens to the numerical solution when x → 1.)

Page 52: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

46 Numerical Solutions Part I

Page 53: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 6

Numerical Solutions Part II

6.1 Purpose

The purpose of this lab is to explore, in further detail, the built in numericalmethods in Maple. An illustration of how the step size affects the accuracyof a numerical method is included.

The new Maple commands used in this lab are:

array

6.2 Comparing Numerical Methods

In this section we will use three classical numerical methods to approximatethe exact solution of the differential equation

y′ = 1− t+ 4y,

subject to the initial condition y(0) = 1. The three methods that we willuse are the forward Euler method, the improved Euler method (also knownas Heun’s method) and the fourth order Runge-Kutta method. Since thereexist a formula for the exact solution of the initial value problem we will beable to compare the accuracy of the approximations at different values to theexact values.

Page 54: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

48 Numerical Solutions Part II

Example 1. Use the Euler, improved Euler and Runge-Kutta methods toapproximate the exact solution to the initial value problem

y′ = 1− t + 4y, y(0) = 1,

on the interval t ∈ [0, 2]. Compare the approximations to the exact solution.

We can use dsolve to obtain the exact solution of the initial value problem:

> de1:=diff(y(t),t)=1-t+4*y(t),y(0)=1;

de1 := ∂∂t

y(t) = 1− t + 4 y(t), y(0) = 1

> exact:=rhs(dsolve(de1,y(t)));

exact := − 3

16+

1

4t+

19

16e(4 t)

For convenience, we convert the expression exact to a function:

> exactf:=unapply(exact,t);

exactf := t→ − 3

16+

1

4t+

19

16e(4 t)

Now we obtain the approximations to the exact solution using the threenumerical methods previously mentioned. We will use a step size of h1 = 0.05for the forward Euler method, h2 = 0.075 for the improved Euler method andh3 = 0.1 for the Runge-Kutta method:

> a1:=dsolve(de1,y(t),numeric,method=classical[foreuler],

stepsize=0.05);

a1 := proc(x classical) . . . end

> a2:=dsolve(de1,y(t),numeric,method=classical[heunform],

stepsize=0.075);

a2 := proc(x classical) . . . end

> a3:=dsolve(de1,y(t),numeric,method=classical[rk4],

stepsize=0.1);

Page 55: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

6.2 Comparing Numerical Methods 49

a3 := proc(x classical) . . . end

As in the previous lab, we can define a procedure that only returns theapproximate y value for each of the numerical methods:

> foreuler:=u->subs(a1(u),y(t));

foreuler := u→ subs(a1(u), y(t))

> heun:=u->subs(a2(u),y(t));

heun := u→ subs(a2(u), y(t))

> rk4:=u->subs(a3(u),y(t));

rk4 := u→ subs(a3(u), y(t))

Now we can investigate how accurately (or inaccurately) the numerical meth-ods approximate the exact solution. We can generate a list of the form[n, foreuler(n), heun(n), rk4(n), exactf(n) ], for different values of n, andthen use array to view the list as a table:

> h1:=[seq([evalf(i/5),foreuler(i/5),heun(i/5),rk4(i/5),

evalf(exactf(i/5))],i=0..10)];

h1 :=[[0, 1., 1., 1., 1.], [.2000000000, 2.324900000000000, 2.483324968750000,

2.505006151111111, 2.505329852], [.4000000000, 5.018532640000001,

5.696688224693420, 5.792785270450575, 5.794226004], [.6000000000,

10.55036928230400, 12.72826414587474, 13.04771262943470,

13.05252195], [1.600000000, 406.1259965976057, 668.6623237429197,

...

[1.800000000, 841.9647265447948, 1475.538279723773,

1589.084028369686, 1590.836532], [2., 1745.666236963285,

3256.261636246054, 3535.866741460758, 3540.200110]]

> array(h1);

ARRAY TOO LARGE

Page 56: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

50 Numerical Solutions Part II

View the help file of array to view examples of how else it can be used.

Clearly, the Runge-Kutta method approximates the solution more accuratelythan the two Euler methods. This is because the algorithm for the Runge-Kutta method produces a better approximation to the average slope of theexact solution on the interval (t, t+ h).

There is a clear difference in the approximations to the exact solution att = 2. The percentage error in using the Euler method, the improved Eulermethod and the Runge-Kutta method, to approximate the exact solution att = 2 are approximately 51%, 8% and 0.12%, respectively. To approximatethe exact solution at t = 2, the forward Euler method performs 40 evaluationsof y′ = f(t, y(t)), the improved Euler method performs (approximately) 53evaluations and the Runge-Kutta method performs 80 evaluations. However,to obtain similar results to those obtain by the Runge-Kutta method at t = 2(although still not as accurate), the Euler method and the improved Eulermethod must use a step size of h = 0.0001 and h = 0.01, respectively. Inother words, to obtain similar results as the Runge-Kutta method, the Eulermethod must perform 20,000 evaluations and the improved Euler methodmust perform 400 evaluations. As previously mentioned, the Runge-Kuttamethod only performs 80 evaluations. Thus in general, the Runge-Kuttamethod is a more accurate and efficient algorithm, producing similar resultswith considerably less computations.

Change the step sizes in the procedures a1 and a2 to h = 0.0001 and h = 0.01,respectively, and re-generate the table to investigate how the changes to theforward Euler and improved Euler methods affect the approximations to theexact solution. You will notice that Maple needs more time to perform therequired calculations.

Try using a smaller step size in the Runge-Kutta method to investigate theaccuracy of this method.

6.3 Choosing the Right Step Size

Example 2. Consider the initial value problem

y′ = −100y + 100t+ 1, y(0) = 1.

Page 57: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

6.3 Choosing the Right Step Size 51

Use the same numerical methods as in Example 1 to approximate the exactsolution to the problem for t ∈ [0, 1].

The exact solution to the problem can easily be found with dsolve:

> de2:=diff(y(t),t)=-100*y(t)+100*t+1,y(0)=1;

de2 := y(0) = 1,∂

∂ty(t) = −100 y(t) + 100 t+ 1

> exact2:=rhs(dsolve(de2,y(t)));

exact2 := t + e(−100 t)

> exact2f:=unapply(exact2,t);

exact2f := t→ t + e(−100 t)

We will use the same step sizes as in the previous example for each of thenumerical methods:

> b1:=dsolve(de2,y(t),numeric,method=classical[foreuler],

stepsize=0.05):

> b2:=dsolve(de2,y(t),numeric,method=classical[heunform],

stepsize=0.075):

> b3:=dsolve(de2,y(t),numeric,method=classical[rk4],

stepsize=0.1):

Again, we define a function for each method that only returns the approxi-mate y value:

> foreuler_2:=u->subs(b1(u),y(t));

foreuler 2 := u→ subs(b1(u), y(t))

> heun_2:=u->subs(b2(u),y(t));

heun 2 := u→ subs(b2(u), y(t))

> rk4_2:=u->subs(b3(u),y(t));

Page 58: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

52 Numerical Solutions Part II

rk4 2 := u→ subs(b3(u), y(t))

As in the previous example, we will generate a table consisting of the ap-proximate values obtained by each numerical method:

> h2:=[seq([evalf(i/10),foreuler_2(i/10),heun_2(i/10),

rk4_2(i/10),evalf(exact2f(i/10))],i=0..10)]:

> array(h2);

ARRAY TOO LARGE

From the table, we can observe that all the methods perform horribly. What’smore surprising is that the Runge-Kutta method is the worst of all. Let’s trychanging the step sizes to half of their current values. Hence change the stepsize used in the Euler method to h1 = 0.0255, the improved Euler method toh2 = 0.0375 and the Runge-Kutta method to h3 = 0.5. Now re-generate thetable h2.

The new step sizes do not seem to produce better approximations to the exactsolution. Try the following values for the step sizes: h1 = 0.01, h2 = 0.02and h3 = 0.025. With these step sizes, the Euler method seems to producebetter approximations than the Runge-Kutta method, even though the lattermethod is assumed to have a better algorithm. The improved Euler method isthe worst of the three, it seems to be off by a value of 1 in each approximation.Investigate this problem further by using different step sizes for each of themethods.

This example illustrate the kind of things that can go wrong when usingnumerical methods to approximate the exact solution of an initial valueproblem. Some methods produce better results than others, and hence itis important to explore and compare the performance of several methods.Choosing a proper step size is also very important. Using a large step sizemay lead to inaccurate results, or using one too small may lead to unneces-sary calculations that become time consuming.

Page 59: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 7

The Laplace Transform

7.1 Purpose

In this lab we solve differential equations using the Laplace transform.

The new Maple commands used in this lab are:

laplace convert piecewise

invlaplace D dsolve(...., method=laplace)

Heaviside with(inttrans) @ (atsign)

7.2 The laplace Command

The Maple command that returns the Laplace transform of an expressionis the laplace command. The laplace command is part of the inttrans

package. The inttrans package is a collection of functions designed for com-puting various integral transforms. The basic calling command of laplaceis:

> laplace(expr, var1, var2);

The argument expr is the expression to be transformed (it can also be a set oran equation). The second argument, var1, is the variable expr is transformedwith respect to, and the third argument, var2, is the variable of the resultingtransformation.

Page 60: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

54 The Laplace Transform

Example 1. Use Maple to obtain the Laplace transforms of sin(at), cos(at),eat, t and t3e3t.

> with(inttrans):

> laplace(sin(a*t),t,s);

a

s2 + a2

We can form a set with the remaining expressions and then just applylaplace to the set:

> expressions:=cos(a*t),exp(a*t),t,t^3*exp(3*t);

expressions := t3 e(3 t), t, cos(a t), e(a t)

> laplace(expressions,t,s);

s

s2 + a2,

1

s− a, 6

1

(s− 3)4,1

s2

As previously mentioned, we can obtain the Laplace transform of an equationusing the laplace command:

> eqn:=y(x)=sin(3*x)-exp(-4*x)+x^2;

eqn := y(x) = sin(3 x)− e(−4 x) + x2

> laplace(eqn,x,lambda);

laplace(y(x), x, λ) = 31

λ2 + 9− 1

λ+ 4+ 2

1

λ3

Example 2. Compute the Laplace transform of h(t), h′(t) and h′′(t) ifh(t) = (5t− 2)3.

We start by assigning a name to h(t) and then obtaining its Laplace trans-form:

> h:=t->(5*t-2)^3;

h := t→ (5 t− 2)3

Page 61: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

7.2 The laplace Command 55

> lap_1:=laplace(h(t),t,s);

lap 1 := 7501

s4− 300

1

s3+ 60

1

s2− 8

1

s

Notice that we had to evaluate h at t to obtain the Laplace transform of thefunction h. Remember, laplace returns the Laplace transform of expressionsonly and not functions.

According to Theorem 6.2.1 and its Corollary 6.2.2, we anticipate that theLaplace transform of h′(t) and h′′(t) will be

sLh(t) − h(0) and s2Lh(t) − sh(0)− h′(0)

respectively:

> lap_dh:=laplace(D(h)(t),t,s);

lap dh := 7501

s3− 300

1

s2+ 60

1

s

> lap_ddh:=laplace((D@@2)(h)(t),t,s);

lap ddh := 7501

s2− 300

1

s

We can verify our results by using Theorem 6.2.1 and its Corollary 6.2.2:

> lap_dh2:=s*laplace(h(t),t,s)-h(0);

lap dh2 := s

(

7501

s4− 300

1

s3+ 60

1

s2− 8

1

s

)

+ 8

> lap_ddh2:=s^2*laplace(h(t),t,s)-s*h(0)-D(h)(0);

lap ddh2 := s2(

7501

s4− 300

1

s3+ 60

1

s2− 8

1

s

)

+ 8 s− 60

> simplify(lap_dh - lap_dh2);

0

> simplify(lap_ddh - lap_ddh2);

0

Note that we have used the operator D, which returns the derivative of afunction, and the operator @ (atsign) . View the help file of the operators Dand @.

Page 62: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

56 The Laplace Transform

7.3 The invlaplace Command

The Maple command that returns the inverse Laplace transform of an ex-pression is the invlaplace command (part of the inttrans package). Thebasic calling command of invlaplace is:

> invlaplace(expr, var1, var2);

The description of the arguments of invlaplace are the same as the argu-ments of the laplace command.

Example 3. Find the inverse Laplace transforms of 1/(s− 4),√3/(s2 + 3),

24/s5 and 6/(s+ 2)4.

We can create a list containing the above expressions and then use map toapply invlaplace to each expression in the list.

> a:=[1/(s-4),sqrt(3)/(s^2+3),24/s^5,6/(s+2)^4];

a :=

[

1

s− 4,

√3

s2 + 3, 24

1

s5, 6

1

(s+ 2)4

]

> invlap_a:=map(invlaplace, a, s, t);

invlap a :=[

e(4 t), sin(√3 t), t4, t3 e(−2 t)

]

Example 4. Calculate the inverse Laplace transform of

F (s) =5s2 + 20s+ 6

s3 + 2s2 + s.

Let’s first obtain a partial fraction decomposition of F (s) by using the convertcommand with the option parfrac:

> F:=(5*s^2+20*s+6)/(s^3+2*s^2+s);

F :=5 s2 + 20 s+ 6

s3 + 2 s2 + s

> Fp:=convert(F,parfrac,s);

Page 63: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

7.4 Piecewise Functions; Heaviside 57

Fp := 61

s+ 9

1

(s+ 1)2− 1

s+ 1

You can view the help file of convert to see additional examples of how itcan be used. Once in this form, the Laplace transform of F (s) can be easilycomputed to be

6 + 9te−t − e−t.

We can verify this result by using invlaplace:

> invlaplace(F,s,t);

6 + 9 t e(−t) − e(−t)

> invlaplace(Fp,s,t);

6 + 9 t e(−t) − e(−t)

7.4 Piecewise Functions; Heaviside

You can define piecewise functions in Maple using the piecewise command.The basic calling command of piecewise is:

> piecewise(con_1,f1,cond_2,f2,...,f_otherwise);

Example 5. Using the piecewise command, define the function

f(t) =

−1 0 ≤ t < 31 t ≥ 3

Then plot it and find its Laplace transform.

> f:=t->piecewise(0<=t and t<3,-1,t>=3,1);

f := t→ piecewise(0 ≤ t and t < 3, −1, 3 ≤ t, 1)

> plot(f,0..6);

Page 64: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

58 The Laplace Transform

–1

–0.5

0

0.5

1

1 2 3 4 5 6

> plot(f,0..6, discont=true);

–1

–0.5

0

0.5

1

1 2 3 4 5 6

By setting discont=true, the plot command searches for discontinuitiesand then splits the input axis into intervals where the function is continuous.

Now we compute the Laplace transform of f(t):

> laplace(f(t),t,s);

laplace

−1 −t ≤ 0 and t− 3 < 0 , t, s

1 3 ≤ t

Maple fails! We can get around this by re-defining f in terms of the Heav-iside function. The calling command for the Heaviside function in Maple isappropriately Heaviside . Therefore we can re-define f as

> f1:=t->-(Heaviside(t)-Heaviside(t-3))+Heaviside(t-3);

Page 65: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

7.4 Piecewise Functions; Heaviside 59

f1 := t→ −Heaviside(t) + 2Heaviside(t− 3)

We can also use convert to accomplish the same result:

> f2:=t->convert(f(t),Heaviside);

f2 := t→ convert(f(t), Heaviside)

> f2(t);

−Heaviside(t) + 2Heaviside(t− 3)

We can verify that f , f1 and f2 are equal by evaluating them at arbitrarypoints or by graphing them.

Hence, the Laplace transform of f(t) is

> laplace(f1(t),t,s);

−1

s+ 2

e(−3 s)

s

Important: In Maple, the Heaviside function at t = 0 is undefined. How-ever, according to the definition of the Heaviside function, Heaviside(0) =1. You can get around this by assigning the expression Heaviside(0) thevalue of 1. For example, consider the function g(t) = 2 cos(t)u(t), whereu(t) = Heaviside(t). Evaluate g at t = 0. Maple returns 2 Heaviside(0),instead of returning 2. Now type

> Heaviside(0):=1;

Heaviside(0) := 1

That is we assign the name Heaviside(0) the value of 1. Now re-evaluateg(0).

Page 66: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

60 The Laplace Transform

7.5 Solving ODE’s with the Laplace Trans-

form

The basic steps in solving a differential equation, in the variable y, with theLaplace transform can be summarized as follows:

1. Apply the Laplace transform to the entire differential equation.

2. Substitute the initial conditions.

3. Solve the resulting equation for Y (s) = Ly(t).

4. Then compute the L−1Y (s) to find y(t).

We can carry out these same steps in Maple using the commands we havelearned in this lab.

Example 6. Solve the initial value problem

x′′ + x = sin(2t), x(0) = 2, x′(0) = 1.

Step 1: Apply the Laplace transform to the ODE:

> s1:=laplace(diff(x(t),t$2)+x(t)=sin(2*t),t,s);

s1 := s (s laplace(x(t), t, s)− x(0))−D(x)(0)+ laplace(x(t), t, s) = 21

s2 + 4

Step 2: Apply the initial conditions:

> s2:=subs(x(0)=2,D(x)(0)=1,s1);

s2 := s (s laplace(x(t), t, s)− 2)− 1 + laplace(x(t), t, s) = 21

s2 + 4

Step 3: Solve the resultant equation for X(s) (that is laplace(x(t),t,s)):

> s3:=solve(s2,laplace(x(t),t,s));

s3 :=2 s3 + 8 s+ s2 + 6

s4 + 5 s2 + 4

Step 4: Now use invlaplace to obtain x(t):

Page 67: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

7.5 Solving ODE’s with the Laplace Transform 61

> s4:=invlaplace(s3,s,t);

s4 := −1

3sin(2 t) + 2 cos(t) +

5

3sin(t)

Example 7. Solve the initial value problem

y(iv) − 4y′′′ + 6y′′ − 4y′ + y = 0,

y(0) = 0, y′(0) = 1, y′′(0) = 0, y′′′(0) = 1.

Let’s first assign a name to the differential equation:

> de1:=diff(y(t),t$4)-4*diff(y(t),t$3)+6*diff(y(t),t$2)

-4*diff(y(t),t)+y(t)=0;

de1 :=

(

∂4

∂t4y(t)

)

− 4

(

∂3

∂t3y(t)

)

+ 6

(

∂2

∂t2y(t)

)

− 4

(

∂ty(t)

)

+ y(t) = 0

Now we apply the Laplace transform and substitute the initial conditions inone step:

> q1:=subs(y(0)=0,D(y)(0)=1,(D@@2)(y)(0)=1,(D@@3)(y)(0)=0,

laplace(de1,t,s));

q1 :=s (s (s2 laplace(y(t), t, s)− 1)− 1)− 2− 4 s (s2 laplace(y(t), t, s)− 1)

+ 6 s2 laplace(y(t), t, s)− 4 s laplace(y(t), t, s) + laplace(y(t), t, s) = 0

Notice the use of the @ sign. Similarly, we will combine the two remainingsteps into one:

> q2:=invlaplace(solve(q1,laplace(y(t),t,s)),s,t);

q2 := −1

2t2 et + t et

We can instruct Maple to use the Laplace transform to solve differentialequations by setting the method option equal to laplace in the dsolve

command.

Page 68: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

62 The Laplace Transform

The following example illustrates an advantage in using the Laplace trans-form to solve an initial value problem.

Example 8. Find the solution of the differential equation

y′′ + 4y = e−t cos 2t,

subject to y(0) = 0 and y′(0) = 1 by first omitting the optionmethod=laplace

in dsolve command and then by including it.

> with(DEtools):

> de2:=diff(y(t),t$2)+4*y(t)=exp(-t)*cos(2*t),y(0)=0,D(y)(0)=1:

> sol_1:=rhs(dsolve(de2,y(t)));

sol 1 :=(2

17(−cos(t) + 4 sin(t)) e(−t) cos(t)3 − 2

17(−cos(t) + 2 sin(t)) e(−t) cos(t)

− 9

34

1

et) sin(2 t) + (

1

17e(−t) cos(4 t) +

1

68e(−t) sin(4 t) +

1

5e(−t) cos(2 t)

+1

10sin(2 t) e(−t) +

1

10e(−t) (−sin(2 t)− 2 cos(2 t)))cos(2 t) +

13

17sin(2 t)

− 1

17cos(2 t)

Maple returns a fairly long expression for y(t).

Now let’s add the method=laplace option:

> sol_2:=rhs(dsolve(de2,y(t),method=laplace));

sol 2 := − 1

17cos(2 t) +

13

17sin(2 t) +

1

17e(−t) cos(2 t)− 4

17sin(2 t) e(−t)

A lot better! You can verify that these solutions are in fact the same bygraphing them or evaluating them at arbitrary points. Alternatively, you canuse the following sequence of commands to convert the expression obtainedby using the first method into the less complicated expression for the solutionobtained by using the second method:

> combine(sol_1,trig);

Page 69: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

7.5 Solving ODE’s with the Laplace Transform 63

1

34

sin(2 t) e(−t) et + 2 e(−t) et cos(2 t)− 9 sin(2 t) + 26 sin(2 t) et − 2 cos(2 t) et

et

> simplify(%);

− 1

17cos(2 t) +

13

17sin(2 t) +

1

17e(−t) cos(2 t)− 4

17sin(2 t) e(−t)

You may wish to read the help file of combine to view additional exampleson how it can be used.

Page 70: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

64 The Laplace Transform

Page 71: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 8

Systems of Ordinary

Differential Equations

8.1 Purpose

The purpose of this lab is to use Maple to solve systems of ordinary differ-ential equations. A review of elementary matrix operations is included.

The new Maple commands used in this lab are:

with(linalg) alias charpoly matrixDE

matrix array factor linsolve

evalm inverse multiply solve

&* eigenvals det

&*() eigenvects transpose

8.2 Elementary Matrix Operations

There are various ways to define a matrix in Maple. For convenience andsimplicity, we will use the matrix command rather than the array command.The matrix command and many of the commands used in this lab are part ofthe linalg package (linear algebra). Let’s load the package before beginning:

> with(linalg):

Page 72: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

66 Systems of Ordinary Differential Equations

To define the matrices

A =

1 −2 10 2 −12 1 1

and B =

2 1 −11 −1 02 −1 1

using the matrix command in Maple, type

> A:=matrix([[1,-2,1],[0,2,-1],[2,1,1]]);

A :=

1 −2 10 2 −12 1 1

Notice the use of the comma separating the rows of the matrix and the extrabrackets enclosing the rows. We can also use the following, simpler format,to define the matrix B with the matrix command:

> B:=matrix(3,3,[2,1,-1,1,-1,0,2,-1,1]);

B :=

2 1 −11 −1 02 −1 1

With this format, we first give the size of the matrix and then enter theentries in row order.

The evalm command can be used to perform elementary operations with ma-trices. Theses operations include addition, multiplication and integer powers.It can also be used to map functions to the entries of a matrix. The operator&* can be used to indicate matrix multiplication.

Example 1. Using the matrices A and B, calculate C1 = AB and C2 =3A2 − 7B3.

> C1:=evalm(A&*B);

C1 :=

2 2 00 −1 −17 0 −1

To calculate C2, we type

Page 73: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.2 Elementary Matrix Operations 67

> C2:=evalm(3*A^2-7*B^3);

C2 :=

−5 −43 54−20 9 5−58 −31 48

Note: The evalm command is not part of the linalg package.

You can also multiply matrices using the multiply command contained inthe linalg package. The basic calling command of multiply is

> multiply(M1,M2,...,Mn);

Hence to obtain C1 using the multiply command we simply type

> multiply(A,B);

2 2 00 −1 −17 0 −1

Un-evaluated matrix expressions, such as 3A − 4B2, can also be entered inmultiply:

> multiply(3*A-4*B^2,B+11*A^3);

1461 −1220 1223−228 283 −270−657 831 −848

The operator &*() can be used to denote the identity matrix. For example,to calculate (A− λI), we type

> evalm(A-lambda*&*());

1− λ −2 10 2− λ −12 1 1− λ

When using the &*() operator, Maple will determine the appropriate sizeof the identity matrix that must be used, we do not have to specify it. Forconvenience, we can use the alias command to assign an alias to the operator&*(). The purpose of the alias command is to define “abbreviations for thelonger unique names that Maple uses”.

Page 74: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

68 Systems of Ordinary Differential Equations

> alias(Id=&*());

I, Id

> evalm(A-lambda*Id);

1− λ −2 10 2− λ −12 1 1− λ

A quick method for entering the n × n identity matrix is to use the array

command with the identity option. Hence to produce the 5 × 5 identitymatrix, type

> array(identity,1..5,1..5);

1 0 0 0 00 1 0 0 00 0 1 0 00 0 0 1 00 0 0 0 1

The inverse command contained in the linalg package can be used to obtainthe inverse of a matrix. Thus to obtain the inverse of the matrix A, we simplytype

> inverse(A);

1 1 0

−2

3

−1

3

1

3

−4

3

−5

3

2

3

We can also use the command evalm to obtain the same result:

> evalm(A^(-1));

Page 75: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.3 Eigenvalues and Eigenvectors 69

1 1 0

−2

3

−1

3

1

3

−4

3

−5

3

2

3

The det command (part of the linalg package) is used to obtain the de-terminant of a matrix. The transpose command (also part of the linalg

package) is used to obtain the transpose of a matrix. Both commands use asingle argument:

> det(C2);

29923

> transpose(C1);

2 0 72 −1 00 −1 −1

8.3 Eigenvalues and Eigenvectors

To obtain the eigenvalues of a square matrix, we use the eigenvals com-mand. The output of eigenvals(X) is a sequence consisting of the eigenval-ues of the matrix X.

The eigenvects command returns the eigenvalues and eigenvectors of amatrix (it also returns the multiplicity of the eigenvalues). The output ofeigenvects is a sequence of lists of the form

[ei,mi, v[1, i], ..., v[ni, i]],

where ei is the eigenvalue, mi is the multiplicity of the eigenvalue (thenumber of times it occurs as a root of the characteristic polynomial) andv[1, i], ..., v[ni, i] is a basis for the eigenspace corresponding to the eigen-value ei.

Page 76: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

70 Systems of Ordinary Differential Equations

Example 2. Calculate the eigenvectors and eigenvalues of the matrix

P =

−3 5 −5−7 9 −5−7 7 −3

First we define the matrix P:

> P:=matrix(3,3,[-3,5,-5,-7,9,-5,-7,7,-3]);

P :=

−3 5 −5−7 9 −5−7 7 −3

If we only want the eigenvalues of P, we use the eigenvals command:

> evals_P:=eigenvals(P);

evals P := −3, 2, 4

If we want the eigenvalues and eigenvectors of P, we use the eigenvects

command:

> evecs_P:=eigenvects(P);

evecs P := [−3, 1, [1, 1, 1]], [2, 1, [1, 1, 0]], [4, 1, [0, 1, 1]]The third component of each list is a set of one element (singleton set)containing the eigenvector of the corresponding eigenvalue. Hence we canextract the eigenvectors in the following way:

> ev1:=evecs_P[1][3][1];

ev1 := [1, 1, 1]

> ev2:=evecs_P[2][3][1];

ev2 := [1, 1, 0]

> ev3:=evecs_P[3][3][1];

ev3 := [0, 1, 1]

Page 77: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.4 Solving Systems of Differential Equations 71

> ev1:=convert(ev1,matrix);

ev1 :=

111

Another method of finding the eigenvalues of a matrix is to use the charpolycommand. The output of charpoly(P,lambda) is the characteristic polyno-mial (in the variable λ) of the matrix P. That is charpoly(P) = det(P−λI).> cp:=charpoly(P,lambda);

cp := (λ+ 3) (λ2 − 6 λ+ 8)

> factor(cp);

(λ+ 3) (λ− 2) (λ− 4)

Therefore, as previously found before, the eigenvalues of P are −3, 2, 4. Youmay wish to read the help file of factor, although its use is self-explanatory.

8.4 Solving Systems of Differential Equations

8.4.1 Eigenvector Method

Example 3. Solve the initial value problem

x′ =

−3 5 −5−7 9 −5−7 7 −3

x, x(0) =

4−5−3

If the coefficient matrix of the differential equation has 3 distinct eigenvalues,then the general solution of the initial value problem is

x(t) = c1ξ1eλ1t + c2ξ2e

λ2t + c3ξ3eλ3t, (8.1)

where λ1, λ2 and λ3 are the eigenvalues of the coefficient matrix and ξ1,ξ2 and ξ3 are corresponding eigenvectors. Hence the problem of finding thesolution of the system of DE’s reduces to a linear algebra problem. First wedefine the matrix A to be the coefficient matrix of the differential equation,and then use eigenvects to determine the unknowns in equation (8.1). Notethat the matrix A has already been given the name P:

Page 78: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

72 Systems of Ordinary Differential Equations

> A:=evalm(P);

A :=

−3 5 −5−7 9 −5−7 7 −3

> eigenvects(A);

[4, 1, [0, 1, 1]], [−3, 1, [1, 1, 1]], [2, 1, [1, 1, 0]]

Hence there exist three linearly independent eigenvectors, therefore A is di-agonalizable and the general solution is simply

x(t) = c1

110

e2t + c2

011

e4t + c3

111

e−3t

To help us determine the constants ci, we can rewrite x(t) in the followingway:

x(t) =

e2t 0 e−3t

e2t e4t e−3t

0 e4t e−3t

c1c2c3

We enter the above 3× 3 matrix in Maple with the name Ψ:

> psi:=matrix(3,3,[exp(2*t),0,exp(-3*t),exp(2*t),exp(4*t),

exp(-3*t),0,exp(4*t),exp(-3*t)]);

ψ :=

e(2 t) 0 e(−3 t)

e(2 t) e(4 t) e(−3 t)

0 e(4 t) e(−3 t)

Instead of manually defining the matrix Ψ(t), as was just done, we can usethe matrixDE command. View the help file of matrixDE. Hence to produceΨ(t), we simply type

> alt:=matrixDE(A,t);

alt :=

−5

7e(2 t) −5

2e(−3 t) 0

−5

7e(2 t) −5

2e(−3 t) e(4 t)

0 −5

2e(−3 t) e(4 t)

, [0, 0, 0]

Page 79: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.4 Solving Systems of Differential Equations 73

> alt[1];

−5

7e(2 t) −5

2e(−3 t) 0

−5

7e(2 t) −5

2e(−3 t) e(4 t)

0 −5

2e(−3 t) e(4 t)

In this case we can ignore the second object of the output from matrixDE

(the vector [0, 0, 0]) because it is only used when solving nonhomogeneoussystems.

The matrix Ψ(t) obtained by using matrixDE may differ slightly than theprevious expression for Ψ(t), but really they are the same. Maple is justusing different eigenvectors.

To determine the constants ci, we must solve the system of linear equations

Ψ(0)

c1c2c3

=

4−5−3

To accomplish this, we can use the linsolve command (part of the linalg

package). The output of linsolve(A,b) is the vector x that satisfies thematrix equation Ax = b. Hence, in our problem A = Ψ(0) and b =[4, −5, −3]. Now we use linsolve to find c = [c1, c2, c3]:

> b:=matrix(3,1,[4,-5,-3]);

b :=

4−5−3

> c:=linsolve(simplify(subs(t=0,evalm(psi))),b);

c :=

−2−96

The command simplify(subs(t=0,evalm(psi))) produces Ψ(0).

Page 80: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

74 Systems of Ordinary Differential Equations

Note: If the matrix obtained with matrixDE is used for Ψ, then the vectorc determined with linsolve will be different.

Therefore the solution of the initial value problem is

x(t) = −2

110

e2t − 9

011

e4t + 6

111

e−3t =

−2e2t + 6e−3t

−2e2t − 9e4t + 6e−3t

−9e4t + 6e−3t

We can obtain the previous expression for x(t) with the following Maplecommand:

> solivp3:=multiply(psi,c);

solivp3 :=

−2 e(2 t) + 6 e(−3 t)

−2 e(2 t) − 9 e(4 t) + 6 e(−3 t)

−9 e(4 t) + 6 e(−3 t)

Example 4. Solve the initial value problem

x′ =

0 1 0 00 0 1 00 0 0 142 55 7 −7

x , x(0) =

1010

If we are not concerned about the eigenvalues and eigenvectors, we can usematrixDE to solve the problem:

> R:=matrix(4,4,[0,1,0,0,0,0,1,0,0,0,0,1,42,55,7,-7]);

R :=

0 1 0 00 0 1 00 0 0 142 55 7 −7

> alt2:=matrixDE(R,t);

alt2 :=

e(3 t) e(−7 t) e(−2 t) e(−t)

3 e(3 t) −7 e(−7 t) −2 e(−2 t) −e(−t)

9 e(3 t) 49 e(−7 t) 4 e(−2 t) e(−t)

27 e(3 t) −343 e(−7 t) −8 e(−2 t) −e(−t)

, [0, 0, 0, 0]

Page 81: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.4 Solving Systems of Differential Equations 75

> Omega:=alt2[1];

Ω :=

e(3 t) e(−7 t) e(−2 t) e(−t)

3 e(3 t) −7 e(−7 t) −2 e(−2 t) −e(−t)

9 e(3 t) 49 e(−7 t) 4 e(−2 t) e(−t)

27 e(3 t) −343 e(−7 t) −8 e(−2 t) −e(−t)

Therefore the solution to the initial value problem is

x(t) = Ω(t)d

where d satisfies

Ω(0)d =

1010

To find d, we use linsolve as in the previous example:

> b2:=matrix(4,1,[1,0,1,0]);

b2 :=

1010

> d:=linsolve(simplify(subs(t=0,evalm(Omega))),b2);

d :=

3

251

50−16

253

2

Therefore the solution to the initial value problem is

> solivp4:=multiply(Omega,d);

Page 82: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

76 Systems of Ordinary Differential Equations

solivp4 :=

3

25e(3 t) +

1

50e(−7 t) − 16

25e(−2 t) +

3

2e(−t)

9

25e(3 t) − 7

50e(−7 t) +

32

25e(−2 t) − 3

2e(−t)

27

25e(3 t) +

49

50e(−7 t) − 64

25e(−2 t) +

3

2e(−t)

81

25e(3 t) − 343

50e(−7 t) +

128

25e(−2 t) − 3

2e(−t)

8.4.2 Using dsolve

The calling command to solve a system of differential equations using dsolveis

> dsolve(sysODE,ICs, var1,var2,...,varn, options);

The argument sysODE is a system of ordinary differential equations and ICsare the initial conditions, if applicable. The argument var1, var2, ..., varn isa set of the unknown functions to sysODE. The argument options areoptional arguments that depend on the type of problem being solve.

Example 5. Solve the system of differential equations

x′ = −3x+ 5y − 5z

y′ = −7x+ 9y − 5z

z′ = −7x+ 7y − 3z

subject to the initial conditions x(0) = 4, y(0) = −5 and z(0) = −3.

Note that this problem was already solved in Example 3, therefore we will beable to compare the output of dsolve to the solution previously obtained.Let’s see how dsolve performs:

> de:=diff(x(t),t)=-3*x(t)+5*y(t)-5*z(t),diff(y(t),t)=-7*x(t)

+9*y(t)-5*z(t),diff(z(t),t)=-7*x(t)+7*y(t)-3*z(t),x(0)=4,

y(0)=-5,z(0)=-3;

Page 83: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.4 Solving Systems of Differential Equations 77

de := ∂∂t

x(t) = −3 x(t) + 5 y(t)− 5 z(t),∂

∂ty(t) = −7 x(t) + 9 y(t)− 5 z(t),

∂tz(t) = −7 x(t) + 7 y(t)− 3 z(t), x(0) = 4, y(0) = −5, z(0) = −3

> solivp5:=dsolve(de,x(t),y(t),z(t));

solivp5 :=y(t) = −2 e(2 t) − 9 e(4 t) + 6 e(−3 t), z(t) = −9 e(4 t) + 6 e(−3 t),

x(t) = −2 e(2 t) + 6 e(−3 t)

We can display the solution of Example 3 by typing

> matrix(3,1,[x(t),y(t),z(t)])=evalm(solivp3);

x(t)y(t)z(t)

=

−2 e(2 t) + 6 e(−3 t)

−2 e(2 t) − 9 e(4 t) + 6 e(−3 t)

−9 e(4 t) + 6 e(−3 t)

The expressions for the solutions using dsolve are identical to those obtainedin Example 3, and hence both methods for solving linear systems are use-ful. So why use the eigenvector method? It is evident that the eigenvectormethod is more time consuming, but this method reveals important informa-tion about the solution to the linear system. For example, by knowing theeigenvalues and eigenvectors of the coefficient matrix, one can easily deter-mine how the solutions behave when t → ∞. The solutions depend highlyon the signs of the eigenvalues and hence it is important to know them ifqualitative knowledge of solutions is desired. This will be covered in the nextlab. For most cases, dsolve does the job.

8.4.3 Laplace Transform

The Laplace transform can be used to solve systems of differential equationsin the same way that it was used to solve higher order differential equations.The beauty of using the Laplace transform to solve systems of ODE’s is thatafter applying it, the differential equations system is transformed into analgebraic one, where the variables are the Laplace transforms of the unknownfunctions. Hence we can solve the algebraic system for the unknowns, andthen use the inverse Laplace transform to obtain the solutions of the originalsystem of ODE’s.

Page 84: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

78 Systems of Ordinary Differential Equations

Example 6. Solve the initial value problem

x′ =

[

0 11 0

]

x +

[

sin t2 cos t

]

, x(0) =

[

20

]

.

The following system is equivalent to

x′ = y + sin t (8.2)

y′ = x+ 2 cos t (8.3)

> sys:=diff(x(t),t)=y(t)+sin(t),diff(y(t),t)=x(t)+2*cos(t);

sys := ∂∂t

x(t) = y(t) + sin(t),∂

∂ty(t) = x(t) + 2 cos(t)

We apply the Laplace transform to both sides of equations (8.2) and (8.3) toobtain the simple algebraic system with the variables X(s) and Y (s):

sX(s)− x(0) = Y (s) + 1s2+1

sY (s)− y(0) = X(s) + 2ss2+1

> s1:=laplace(sys,t,s);

s1 :=s laplace(x(t), t, s)− x(0) = laplace(y(t), t, s) +1

s2 + 1,

s laplace(y(t), t, s)− y(0) = laplace(x(t), t, s) + 2s

s2 + 1

Now we apply the initial conditions:

> s2:=subs(x(0)=2,y(0)=0,s1);

s2 :=s laplace(x(t), t, s)− 2 = laplace(y(t), t, s) +1

s2 + 1,

s laplace(y(t), t, s) = laplace(x(t), t, s) + 2s

s2 + 1

Next we use solve to solve for the variables X(s) and Y (s):

Page 85: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.5 The Case of Repeated Eigenvalues 79

> s3:=solve(s2,laplace(x(t),t,s),laplace(y(t),t,s));

s3 :=laplace(x(t), t, s) = (2 s2 + 5) s

(s2 + 1) (s2 − 1),

laplace(y(t), t, s) =4 s2 + 3

s4 − 1

Thus, we have that

X(s) =s(2s2 + 5)

s4 − 1and Y (s) =

4s2 + 3

s4 − 1

Finally we apply the inverse Laplace transform to X(s) and Y (s) to obtainthe functions x(t) and y(t):

> s4:=invlaplace(s3,s,t);

s4 := x(t) = 7

4et +

7

4e(−t) − 3

2cos(t), y(t) =

7

4et − 7

4e(−t) +

1

2sin(t)

This problem can also be solved with dsolve:

> dsolve(sys[1],sys[2],x(0)=2,y(0)=0,x(t),y(t));

x(t) = 7

4et +

7

4e(−t) − 3

2cos(t), y(t) =

7

4et − 7

4e(−t) +

1

2sin(t)

8.5 The Case of Repeated Eigenvalues

In this section we solve a system of differential equations whose coefficientmatrix has a repeated eigenvalue. Remember that even though some eigen-values may be repeated, you may still be able to find n linearly independenteigenvectors for an n× n coefficient matrix.

Example 7. Find the general solution of the given system of differentialequations.

x′ =

2 −4 2 2−2 0 1 3−2 −2 3 3−2 −6 3 7

x

Page 86: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

80 Systems of Ordinary Differential Equations

> Q:=matrix(4,4,[2,-4,2,2,-2,0,1,3,-2,-2,3,3,-2,-6,3,7]);

Q :=

2 −4 2 2−2 0 1 3−2 −2 3 3−2 −6 3 7

> factor(charpoly(Q,lambda));

(λ− 2)2 (λ− 4)2

> eigenvects(Q);

[4, 2, [0, 1, 1, 1]], [2, 2, [1, 0, −1, 1], [0, 1, 2, 0]]

The eigenvalues of Q are λ1 = 2 and λ2 = 4. The corresponding eigenvectorsfor λ1 are

v1 =

0120

and v2 =

10−11

,

and for λ2 = 4

v3 =

0111

.

Therefore three linearly independent solutions to the system are

x1(t) = v1e2t

x2(t) = v2e2t

x3(t) = v3e4t

Since there is only one linearly independent eigenvector for λ2 = 4, we mustfind a vector η satisfying

(Q− 4I)2η = 0.

In other words, η is in the null space of (Q− 4I)2. This can easily be donein Maple using the nullspace command. Typing nullspace(X) will returna basis for the null space of the matrix X.

Page 87: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

8.5 The Case of Repeated Eigenvalues 81

> nullspace(evalm(Q-4*Id)^2);

[−1, 1, 1, 0], [0, 1, 1, 1]

Hence we must choose

η =

−1110

,

and

(Q− 4I)η =

0−1−1−1

Therefore the fourth linearly independent solution of the system is

x4(t) = e4t (η + t(Q− 4I)η)

=

−1110

+

0−1−1−1

t

e4t

Thus the general solution to the system of DE’s is

X(t) = c1

0120

e2t + c2

10−11

e2t + c3

0111

e4t + c4

−1110

+

0−1−1−1

t

e4t

Page 88: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

82 Systems of Ordinary Differential Equations

Page 89: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

Lab No. 9

Phase Portraits

9.1 Purpose

In this lab we will use Maple to generate phase portraits for a given systemof differential equations.

The new Maple commands used in this lab are:

augment

9.2 Parametric Plots

Before generating phase portraits in Maple, we need to be able to plot para-metric curves. In Maple, to plot the image of the parameterization

c(t) = (x(t), y(t)),

we use the plot command with the following format:

> plot([x(t),y(t),t=range of t],h,v,options);

The options h and v are the horizontal and vertical ranges to be viewed,respectively.

Example 1. Plot the curves c1(t) = (t cos t, t sin t) and c2(t) = (3 cos3 t, 2 sin3 t)for t ∈ [0, 2π].

Page 90: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

84 Phase Portraits

> plot([t*cos(t),t*sin(t),t=0..8*Pi]);

–20

–10

0

10

20

–20 –10 10 20

> plot([3*cos(t)^3,2*sin(t)^3,t=0..2*Pi],-3.5..3.5,-2.5..2.5,

colour=blue);

–2

–1

1

2

–3 –2 –1 1 2 3

You can plot both curves on the same graph by typing

> plot([t*cos(t),t*sin(t),t=0..2*Pi],[3*cos(t)^3,2*sin(t)^3,

t=0..2*Pi]);

Page 91: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

9.3 Plotting a Single Solution 85

–4

–3

–2

–1

1

2

–2 2 4 6

9.3 Plotting a Single Solution

In this section we will plot the trajectory of a solution to a system of differen-tial equations. In the next section, we will use the same example to generatea phase portrait.

Example 2. Solve the initial value problem

x′ =

[

−1 −71 −9

]

x, x(0) =

[

12

]

and then plot the solution on the phase plane.

First we define the matrix A to be the coefficient matrix of the system andthen use eigenvects to find the eigenvalues and eigenvectors of A:

> with(linalg):

> A:=matrix(2,2,[-1,-7,1,-9]);

A :=

[

−1 −71 −9

]

> E:=eigenvects(A);

E := [−8, 1, [1, 1]], [−2, 1, [7, 1]]

Page 92: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

86 Phase Portraits

Therefore the general solution of the system is

x(t) = c1

[

71

]

e−2t + c2

[

11

]

e−8t

=

[

7e−2t e−8t

e−2t e−8t

] [

c1c2

]

= Ψ(t)c (9.1)

We can easily define Ψ(t) in Maple using the augment command. Theaugment command joins two or more matrices or vectors. For example, touse the vectors

v1 =

[

12

]

and v2 =

[

34

]

as columns in a 2× 2 matrix we type

> augment([1,2],[3,4]);[

1 32 4

]

Hence, to define Ψ(t) we type

> psi:=augment(E[1][3][1]*exp(E[1][1]*t),E[2][3][1]*

exp(E[2][1]*t));

ψ :=

[

e(−8 t) 7 e(−2 t)

e(−8 t) e(−2 t)

]

The command E[1][3][1] extracts the first eigenvector in the list E andE[1][1] extracts the corresponding eigenvalue.

To find c we can use linsolve to solve the system

Ψ(0)c =

[

12

]

. (9.2)

Alternatively, since Ψ(0) is invertible, we can find c by multiplying equation(9.2) on both sides by Ψ−1(0). Therefore, c can be expressed as

c = Ψ−1(0)

[

12

]

. (9.3)

Page 93: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

9.3 Plotting a Single Solution 87

In Maple, we can first define the matrix Ψ(0), and then use inverse togetherwith multiply to find c:

> psi_0:=subs(t=0,evalm(psi));

psi 0 :=

[

e0 7 e0

e0 e0

]

> c:=multiply(inverse(psi_0),[1,2]);

c :=

[

13

6,−1

6

]

Thus the solution to the initial value problem is

> solivp:=multiply(psi,c);

solivp :=

[

13

6e(−8 t) − 7

6e(−2 t),

13

6e(−8 t) − 1

6e(−2 t)

]

Before we plot the solution x(t) = (x(t), y(t)) on the phase plane, we canobserve from the above expression that as t → ∞, x(t) and y(t) both ap-proach 0. Hence we can visualize the graph of x(t) as a curve beginning atthe point (1, 2) (at t = 0) and approaching the origin as t increases.

We can confirm our observations by plotting the solution of the initial valueproblem on the phase plane:

> plot([solivp[1],solivp[2],t=0..5]);

0

0.5

1

1.5

2

–0.4 –0.2 0.2 0.4 0.6 0.8 1

Page 94: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

88 Phase Portraits

9.4 Phase Portraits

To generate a phase portrait of the system of differential equations in theprevious example, we can first combine equations (9.1) and (9.3) to obtainthe following formula for the solution to the system with an arbitrary initialcondition [ x0

y0 ]:

x(t) = Ψ(t)Ψ−1(0)

[

x0y0

]

.

Using this equation, we can generate a sequence of solutions to the sys-tem of DE’s with arbitrary initial conditions. Let’s first define the matrixΨ(t)Ψ−1(0) in Maple:

> Phi:=multiply(psi,inverse(psi_0));

Φ :=

−1

6e(−8 t) +

7

6e(−2 t) 7

6e(−8 t) − 7

6e(−2 t)

−1

6e(−8 t) +

1

6e(−2 t) 7

6e(−8 t) − 1

6e(−2 t)

Next we define a function that takes an initial condition [x0, y0] and outputsthe corresponding solution to the system having that initial condition:

> Sol:=(x0,y0)->multiply(Phi,[x0,y0]);

Sol := (x0 , y0 ) → multiply(Φ, [x0 , y0 ])

Hence if we type Sol(1,2), we will obtain the solution to the initial valueproblem in the previous example:

> Sol(1,2);

[

13

6e(−8 t) − 7

6e(−2 t),

13

6e(−8 t) − 1

6e(−2 t)

]

We can plot the solution on the phase plane by typing the following:

> plot([Sol(1,2)[1],Sol(1,2)[2],t=0..5]);

Page 95: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

9.4 Phase Portraits 89

0

0.5

1

1.5

2

–0.4 –0.2 0.2 0.4 0.6 0.8 1

Now let’s generate a list of parametric plot structures using Sol that can beplotted with plot:

> curves:=seq(seq([Sol(i,j)[1],Sol(i,j)[2],t=0..20],j=-4..4),

i=-1..1);

curves :=

[

−9

2e(−8 t) +

7

2e(−2 t), −9

2e(−8 t) +

1

2e(−2 t), t = 0..20

]

,

[

−10

3e(−8 t) +

7

3e(−2 t), −10

3e(−8 t) +

1

3e(−2 t), t = 0..20

]

,

[

−13

6e(−8 t) +

7

6e(−2 t), −13

6e(−8 t) +

1

6e(−2 t), t = 0..20

]

,

[

−e(−8 t), −e(−8 t), t = 0..20]

,

...[

13

6e(−8 t) − 7

6e(−2 t),

13

6e(−8 t) − 1

6e(−2 t), t = 0..20

]

,

[

10

3e(−8 t) − 7

3e(−2 t),

10

3e(−8 t) − 1

3e(−2 t), t = 0..20

]

,

[

9

2e(−8 t) − 7

2e(−2 t),

9

2e(−8 t) − 1

2e(−2 t), t = 0..20

]

> plot(curves);

Page 96: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

90 Phase Portraits

–4

–2

2

4

–3 –2 –1 1 2 3

For more qualitative results, we can use display to produce a graph of thedirection field associated with the system of DE’s, together with the phaseportrait:

> de:=diff(x(t),t)=-x(t)-7*y(t),diff(y(t),t)=x(t)-9*y(t);

de := ∂∂t

x(t) = −x(t)− 7 y(t),∂

∂ty(t) = x(t)− 9 y(t)

> dfplot:=dfieldplot(de,[x(t),y(t)],t=0..20,x=-3..3,y=-3..3,

colour=gray):

> phplot:=plot(curves,thickness=2):

> display(dfplot,phplot);

–3

–2

–1

1

2

3

–3 –2 –1 1 2 3

Page 97: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

9.4 Phase Portraits 91

In this case, all solutions to the system of DE’s approach the origin ast → ∞. This is due to the signs of the eigenvalues. Since both eigenval-ues are negative, the solutions will decay exponentially. Notice also that thesolutions approach the origin in the direction of the eigenvectors. This is be-cause as t increases, the exponential terms in the vector [x(t), y(t)] becomecloser and closer to 1. As a result, this will cause the vector [x(t), y(t)] tolook more like the eigenvectors.

Example 3. Use Maple to plot the phase portrait of the system

x′ =

[

−1/2 1−1 −1/2

]

x.

In this example, we will use dsolve to solve the system. We will includeinitial conditions, but will enter them as parameters so that we later createa sequence of solutions for various initial conditions:

> ivp:=diff(x(t),t)=-x(t)/2+y(t),diff(y(t),t)=-x(t)-y(t)/2,

x(0)=a,y(0)=b;

ivp := ∂∂t

x(t) = −1

2x(t) + y(t), x(0) = a, y(0) = b,

∂ty(t) = −x(t)− 1

2y(t)

> solivp2:=dsolve(ivp,x(t),y(t));

solivp2 :=

x(t) = e

1

2t

(cos(t) a+ sin(t) b),

y(t) = e

1

2t

(−sin(t) a+ cos(t) b)

Next we will define a function that converts the solutions into parametricplot structures that can be plotted with plot:

> parasol:=unapply(subs(solivp2,[x(t),y(t),t=trange]),a,b,

trange);

Page 98: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

92 Phase Portraits

parasol :=(a, b, trange) →[

e

1

2t

(cos(t) a+ sin(t) b),

e

1

2t

(−sin(t) a+ cos(t) b), t = trange

]

The function parasol is dependent on the initial conditions a and b, and ofthe t range. For example, if we want to plot the solution to the system subjectto the initial conditions x(0) = 1 and y(0) = 1, on the interval t ∈ [0, 10], wetype

> plot(parasol(1,1,0..10));

–0.4

–0.2

0

0.2

0.4

0.6

0.8

1

–0.2 0.2 0.4 0.6 0.8 1

Now we define a function phaseport that plots a phase portrait of the system.We will accomplish this by using the seq command to generate a set ofparametric solutions with various initial conditions, and then use the plot

command to plot them:

> phaseport:=trange->plot(seq(seq(parasol(a,b,trange),

a=-2..2),b=-2..2));

phaseport :=trange → plot(seq(seq(parasol(a, b, trange), a = −2..2),

b = −2..2))

Hence, by typing

> phaseport(0..10);

Page 99: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

9.4 Phase Portraits 93

–2

–1

0

1

2

–2 –1 1 2

Maple will plot 25 solutions to the linear system on the interval t ∈ [0, 10].

As in the previous example, we can plot the phase portrait together with thedirections field of the system:

> dfplot2:=dfieldplot(diff(x(t),t)=-x(t)/2+y(t),diff(y(t),t)=

-x(t)-y(t)/2,[x(t),y(t)],t=0..10,x=-2..2,y=-2..2,

colour=gray):

> phplot2:=phaseport(0..10):

> display(dfplot2,phplot2);

–2

–1

0

1

2

y

–2 –1 1 2x

From the previous graph, we can observe that all solutions to the linear sys-tem approach the origin as t→ ∞ in a spiral. This is because the eigenvalues

Page 100: Differential Equations and Computer Methods Written by ...math237/MapleNotesCesar.pdf · left-hand side of this equation, respectively. Assign the name rightto the right-hand side

94 Phase Portraits

are complex (this causes the spiral) and the real parts of the eigenvalues arenegative (this causes the solutions to approach the origin). If the real partsof the eigenvalues were positive, then the solutions would move away fromthe origin and become unbounded as t→ ∞.