math 5525 tutorial: examples with matlabcalde014/tutorial5525.pdfthe numerical di erential equation...

26
Math 5525 Tutorial: Examples with Matlab M.Carme Calderer School of Mathematics, University of Minnesota, Minneapolis, MN 55455 January 28, 2020 Contents A Solving differential equations with Matlab A-2 A.1 Symbolic Solutions ........................................... A-2 A.2 Numerical solutions of differential equations ............................. A-3 A.2.1 First order equations ...................................... A-3 A.2.2 First order systems ....................................... A-4 A.3 Vector Fields .............................................. A-5 A.4 Two dimensional systems: phase plane diagrams .......................... A-5 A.5 Long-Term Behavior of Solutions ................................... A-7 A.6 Limit cycles ............................................... A-8 A.7 The Lorenz system ........................................... A-9 A.8 Invariant sets .............................................. A-12 A.9 Energy methods ............................................ A-14 A.9.1 Conservation of energy ..................................... A-16 A.10 The Method of Liapunov ....................................... B-17 B Linear Algebra B-18 B.1 Matrix Operations ........................................... B-18 B.2 Linear systems and vector spaces ................................... B-19 B.2.1 The row-echelon form of a matrix ............................... B-19 B.2.2 Subspaces and Basis ...................................... B-19 B.2.3 Eigenvalues, Eigenvectors and Matrix Diagonalization ................... B-20 B.2.4 The LU-factorization ...................................... C-21 C Appendix: A brief survey of Matlab C-21 C.1 Setting Your Working Environment: Creating Directories ..................... C-21 C.2 Setting up variables .......................................... C-22 C.2.1 Scalar Variables ........................................ C-22 C.2.2 Vector Variables ........................................ C-22 C.3 Graphs .................................................. C-23 C.4 Three–Dimensional Plots ....................................... C-23 C.5 Programming in Matlab ........................................ C-24 C.6 Floating-point arithmetic and round-off errors ............................ D-25 D Reference D-26 -1

Upload: others

Post on 22-Nov-2020

10 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

Math 5525 Tutorial: Examples with Matlab

M.Carme CaldererSchool of Mathematics, University of Minnesota, Minneapolis, MN 55455

January 28, 2020

Contents

A Solving differential equations with Matlab A-2A.1 Symbolic Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2A.2 Numerical solutions of differential equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-3

A.2.1 First order equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-3A.2.2 First order systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-4

A.3 Vector Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-5A.4 Two dimensional systems: phase plane diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . A-5A.5 Long-Term Behavior of Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-7A.6 Limit cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-8A.7 The Lorenz system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-9A.8 Invariant sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-12A.9 Energy methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-14

A.9.1 Conservation of energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-16A.10 The Method of Liapunov . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-17

B Linear Algebra B-18B.1 Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-18B.2 Linear systems and vector spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-19

B.2.1 The row-echelon form of a matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-19B.2.2 Subspaces and Basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-19B.2.3 Eigenvalues, Eigenvectors and Matrix Diagonalization . . . . . . . . . . . . . . . . . . . B-20B.2.4 The LU-factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-21

C Appendix: A brief survey of Matlab C-21C.1 Setting Your Working Environment: Creating Directories . . . . . . . . . . . . . . . . . . . . . C-21C.2 Setting up variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-22

C.2.1 Scalar Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-22C.2.2 Vector Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-22

C.3 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-23C.4 Three–Dimensional Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-23C.5 Programming in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-24C.6 Floating-point arithmetic and round-off errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-25

D Reference D-26

-1

Page 2: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

A Solving differential equations with Matlab

Consider the following initial value problem

y′ = f(t, y), a ≤ t ≤ b y(a) = α. (1)

A.1 Symbolic Solutions

Sometimes is possible to find an explicit solution (symbolic solution) of the differential equation (??). Thecommand to find symbolic solutions is dsolve. It operates as follows. At the Matlab prompt <<, we type

dsolve(′Dy = f(x, y)′,′ y(x0) = y0′,′ x′)

Example 1. To solve the initial value problem dydx = y − x, y(0) = 2, we type

sol1 = dsolve(′Dy = y − 4 ∗ x′,′ y(0) = 2′,′ x′).

The output is

ans1 = 4 ∗ x+ 4− 2 ∗ exp(x)

Next, we want to plot the solution on the interval [0, 3]. We take the step size equals to .1:

x = 0 : .1 : 3

If we type plot(x, ans1) we will get an error message. To be able to plot the solution, we need to make twomodifications to the answer. First of all, the object to be plotted has to be a function (the command inlinewill do the job), and secondly it has to be a vector, i.e., the function has to be calculated at all values of x (0,.1, .2, .3, .4, ...3). The command vectorize will transform the scalar function into a vector one. Type:

y1 = inline(vectorize(ans1),′ x′)

Typingplot(x, y1(x)) will produce the plot.Example 2. Suppose that we want to solve the previous problem for arbitrary initial data, i.e., for

y(0) = c.Let us run the dsolve command with the new initial data:

sol2 = dsolve(′Dy = y − 4 ∗ x′,′ y(0) = c′,′ x′).

We get sol2= 4*x+4+exp(x)*(-4+c).We need to write the answer as a function (of x and c):y2=inline(vectorize(sol2), ’x’, ’c’)To plot the family of curves, type:

axes; hold on

x=0:.1:3 for c=-1:4

plot(x, y2(x, c)) end axis tight title ’Solutions of equation 2’ xlabel x ylabel y hold off

Notice that we have used some graphics options to improve the picture. Use the help command to findout about the above graph commands.

The previous examples deal with solving first order equations. Let us now use the dsolve command forhigher order equations and systems. The notation for the first order derivative, y′ is Dy, for the second orderderivative y′′ is D2y, and proceed likewise to represent higher order ones.

Example 3. Consider the differential equation

y′′ + y′ − 6y = 20ex. (2)

A-2

Page 3: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

Let us run the dsolve command to solve it:

ode3=’D2y+Dy -6*y= 20*exp(x)’;

dsolve(ode3, ’x’)Matlab returns with

ans= -5*exp(x)+c1*exp(-3*x)+c2*exp(x)Note that the general solution of the equation depends on two arbitrary constants c1 and c2. To solve the

differential equation with initial conditions y(0) = 0 and y′(0) = 1, we type

dsolve(ode3, ’y(0)=0’, ’y(1)=1’, ’x’) Matlab returns with

ans=-5*exp(x)+ 5*exp(2*x)

A.2 Numerical solutions of differential equations

The numerical differential equation solver in Matlab is ode45.

A.2.1 First order equations

To solve a differential equation with Matlab, you first enter the function. If you are entering the data directlyinto the screen, you will type the command inline to define the function f of the right hand side of theequation. Let us look at the following example.

Example 1. Suppose you want to solve the initial value problem

y′ = y − t2 + 1, 0 ≤ t ≤ 2, y(0) = .5 (3)

At the Matlab prompt >>, you may type

f= inline(’y − t.2 + 1’, ’t’, ’y’)

Another method is to create a Matlab file, example1.m, as follows:

function z=example1(t, y); z = y − t.2 + 1;

To plot the approximate solution on the interval [0, 2], type

ode45(f, [0,2], .5)if f is an inline function, or

ode45(’example1’, [0,2], .5)

if example1 is a an M-file.

Now, suppose that you want to look at the plots of solutions for an interval of initial data, say, α = .5α = .75tα = 1.0, ... α = 2.0. (Figure 2). Type:

ode45(f, [0,2], .5:.25:2);

Note that ode45 also solves nonlinear problems.

Example 2. Solve the following IVP:

y′ =t− e−t

y + ey, t ∈ [0, 2], y(0) = 1. (4)

A-3

Page 4: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

A.2.2 First order systems

Example 3.

Solve the following initial value problem

y′1 = −3y1 − y2y′2 = −y1 − 3y2, y1(0) = −1, y2(0) = 1, (5)

on the interval 0 ≤ x ≤ 3. We think of y1 and y2 as components of a vector y. In Matlab, such components

are written as y(1) and y(2). First, we define the (vector) function corresponding to the right hand side of thesystem (5):

f=inline(’[-3*y(1)-y(2); -y(1)-3*y(2)]’, ’x’, ’y’)

[x, yanswer]=ode45(f, [0:.1:3], [-1 1]);

If you do not type a semicolon, your screen will display a 31 × 1 vector x and one 31 × 1 vector for eachof the solution components y(1) and y(2). These correspond to the numerical solution of the system.

Now, let us plot the solution. One useful graph is the one on the plane (y(1), y(2)). Type:

plot(yanswer(:,1), yanswer(:,2))The syntaxes yanswer(:,1) corresponds to plotting the table of values (: notation) for the first component

of the solution, and likewise yanswer(:,2) plots the values of the second component.You may also plot each of the solution components as functions of x:plot(x, yanswer(:,1)) and plot(x, yanswer(:,2))

Example 4.

Solve the following initial value problem:

y′1 = y2

y′2 = −y1 − y2, y1(0) = 1, y2(0) = 4, (6)

on the interval 0 ≤ x ≤ 20.

f=inline(’[ y(2); -y(1)-y(2)]’, ’x’, ’y’)

[x, yanswer]=ode45(f, [0:.1:20], [1 4]);

plot(yanswer(:,1), yanswer(:,2))

Example 5. Plot the solutions of system (6) for a collection of initial data,

y1(0) = a, y2(0) = b.

We will take a = −6 : 6, b = −5 : 5.Type:

hold on

for a=-6:6

for b=-5:5

[x, yanswer]=ode45(f, [0:.1:20], [a b]);

plot(yanswer(:,1), yanswer(:,2))

end

end

hold off

A-4

Page 5: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−15 −10 −5 0 5 10 15−8

−6

−4

−2

0

2

4

6

8

y(1)

y(2)

Solutions of Dy(1)=y(2), Dy(2)=−y(1)−y(2), y(1)(0)=a, y(2)(0)=b, a=−6:6, b=−5:5

A.3 Vector Fields

We now show a method to plot vector fields of differential equations (either first order, or autonomoussecond order ones); that is, the tangent vector to the (plane) solution curve. Example 1. Consider the

system

y′1 =1

2y1 − 2y2

y′2 = 2y1. (7)

We are going to plot the tangent vector to the solution path at every point of the plane (actually, in someportion of the plane only!). Let us call this vector s = (s1, s2). In this example, s1 = 1

2y1 − 2y2 and s2 = 2y1.We achieve this with the quiver command.

Let us choose the portion of the plane for our plot, e.g. the square (−2, 2) × (−2, 2). The meshgridcommand sets up the square. Type:

[y1, y2]=meshgrid(-2:.2:2, -2:.2, 2)

s1=1/2*y1-2*y2

s2=2 * y1

quiver(y1, y2, s1, s2, .5)

xlabel y1

ylabel y2

title ’Vector field plot of Dy1=1/2*y1-2*y2, Dy2=2 * y1’

axis tight

Now try to plot the vector fields for the problems of Homework 1. The ”.5” at the end of the quivercommand reduces the length of the vectors by half. The purpose is cosmetic only.

The resulting plot is shown in the next picture.

A.4 Two dimensional systems: phase plane diagrams

We give examples on constructing the phase plane of two dimensional systems. We will proceed along thefollowing steps: find the equilibrium (critical) points, write down the corresponding linearized systems, findthe nature of the critical points and numerically construct the orbits.

Example 1.

x′ = x− y,y′ = 1− x2. (8)

A-5

Page 6: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−2 −1.5 −1 −0.5 0 0.5 1 1.5 2−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

Vector field plot of Dy1=1/2*y1−2*y2, Dy2=2*y1

y1

y2

Figure 1: Vector field of example (7)

Step 1. Find the critical pointsEquilibrium points of the system are solutions of

x− y = 0, 1− x2 = 0.

These are (1, 1) and (−1,−1).

Step 2. Linearized system about the critical pointsLet us first consider (1, 1) and define new variables,

x = u+ 1, y = v + 1.

The original system (omitting nonlinear terms) for the new variables, i.e., the linearized system is:[u′

v′

]=

[1 −1−2 0

] [uv

].

Eigenvalues and eigenvectors, respectively, of the matrix are

λ1 = 2, v1 =

[1−1

]; λ2 = −1, v2 =

[12

].

The equilibrium point is a saddle point, and the eigenvectors denote the directions of the separatrix curvesat (1, 1).

We now consider the point (−1,−1) and define the new variables (p, q) as

x = p− 1, y = q − 1.

The linearized system has the form, [p′

q′

]=

[1 −12 0

] [pq

].

Eigenvalues of the latter matrix are,

λ1 =1

2±√

7

2i.

Therefore, (−1,−1) is an unstable spiral point.

A-6

Page 7: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−3 −2 −1 0 1 2

−4

−3

−2

−1

0

1

2

3

4

5

x

y

phase plane of system Dx=x−y, Dy=1−x2

Step 3. Plot the orbits of the original system. You may type the following statements to Matlab.

f=inline(’[x(1)− x(2); 1− x(1)2]’, ’t’, ’x’);

tspan=[0:.035:.90];

for a=-1.5:.18:1.4; for b=-1.5:.18:1.6;

hold on

xzero=[a;b];

[t, xans]=ode45(f, tspan, xzero);

plot(xans(:,1), xans(:,2))

end

end

A.5 Long-Term Behavior of Solutions

One important issue about systems of differential equations is to understand what happens to all solutions,x(t), as t → ∞. We have defined the terms of stable, unstable and asymptotically stable equilibrium pointsof liner systems (page 180 of textbook). Such concepts apply also to nonlinear systems of dimension higherthan two.

Solutions of the systemx′ = f(x),

are vector valued functions x(t) = (x1(t), x2(t), ...xn(t). An equilibrium solutions x0 satisfies f(x0) = 0. Todetermine its stability, we write down the linearized system as follows. First, do the Taylor expansion of fabout (x0):

f(x0 + u) = f(x0) + J(x0)u + R(u),

where J is the Jacobian matrix of partial derivatives:

∂f1∂y1

∂f1∂y2

.... ∂f1∂yn

∂f2∂y1

∂f2∂y2

.... ∂f2∂yn

. . .... .

. . .... .

. . .... .∂fn∂y1

∂fn∂y2

.... ∂fn∂yn

.

R(u) is the remainder of the expansion. The linearized system about the equilibrium x0 is:

A-7

Page 8: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

u′ = Ju.

Theorem A.1. Theorem Let x0 and J be as previously defined.Suppose that the real part of every eigenvalue of J is negative. Then x0 is an asymptotically stable equilibriumpoint.Suppose that J has at least one eigenvalue with positive real part. Then x0 is an unstable equilibrium point.

A.6 Limit cycles

Consider the system

x′ = −y + x(1− x2 − y2)

y′ = x+ y(1− x2 − y2).

Express the system in polar coordinates and describe all solutions.Let x = r cos θ, y = r sin θ. Solving for r and θ gives:

r2 = x2 + y2, (9)

tan θ =y

x. (10)

Let us calculate the time derivative of both sides of equation (9):

2rr′ = 2(xx′ + yy′)

= 2[x(−y + x(1− x2 − y2)) + y(x+ y(1− x2 − y2))]

= 2(x2 + y2)(1− x2 − y2) = r2(1− r2).

So, for r 6= 0,r′ = r(1− r2). (11)

Likewise, we differentiate both sides of equation (10) with respect to t:

sec2 θθ′ =d

dt(y

x) =

(xy′ − yx′)x2

1

x2[x(x+ y(1− x2 − y2))− y(−y + x(1− x2 − y2))]

=(x2 + y2)

x2=r2

x2= sec2θ.

This givesθ′ = 1, (12)

and therefore, θ = t+ C.Note that the original system reduces to two uncoupled equations (11) and (12). From the solution of (12),

i.e. θ = t + C, we see that θ is steadily increasing with speed 1. This means that all solution curves spiralaround the origin in the counterclockwise direction.

Note that equation (11) has two equilibrium points, r = 0, 1. Moreover, for r > 1, r′ < 0, and so r(t)decreases to r = 1; for r < 1, r′ > 0 andr(t) increases to r = 1. Thus r = 1 is an asymptotically stableequilibrium point, and all other solution curves starting away from the origin spiral toward it. Hence the unitcircle is the limit set of any point of R2, except the origin.

The circle r = 1 is a limit cycle; it attracts solutions starting in either side of the circle. Note that r = 1 isan orbit of the problem. We can also say that the curve r = 1 (x2 + y2 = 1) is a invariant set of the equations.(Why?)

A-8

Page 9: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−1 −0.5 0 0.5 1 1.5−1

−0.5

0

0.5

1

1.5

x

y

Example of limit cycle

A.7 The Lorenz system

The model discovered by the mathematician and meteorologist Lorenz (1963) applies to atmospheric turbu-lence:

x′ = −ax+ ay,

y′ = rx− y − xz,z′ = −bz + xy.

a, r and b are parameters. The equilibrium points of the system are:

c0 =

000

, c+ =

√b(r − 1)√b(r − 1)r − 1

, c− =

−√b(r − 1)

−√b(r − 1)r − 1

. (13)

Note that if r < 1 there is only an equilibrium solution, and there are three for r > 1. The Jacobian matrixof the Lorenz system is:

J(x, y, z) =

−a a 0(r − z) −1 −xy x −b

, (14)

where (x, y, z) denotes an equilibrium point. Let us consider the special set of parameters a = 10, b = 8/3, r =28 and study the properties of the equilibrium points. The matrix

J(0, 0, 0) =

−10 10 028 −1 00 0 −8/3

(15)

has eigenvalues −22.8277, 11.8277, −2.6667. So, (0, 0, 0) is unstable.The matrix

J(6√

2, 6√

2, 27) =

−10 10 0

1 −1 −6√

2

6√

2 6√

2 −8/3

(16)

has eigenvalues −13.8546, 0.094 + 10.1945i, 0.094− 10.1945i. So, (6√

2, 6√

2, 27) is unstable. Likewise,

A-9

Page 10: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−20 −15 −10 −5 0 5 10 15 200

5

10

15

20

25

30

35

40

45

50

y1

y3

Lorenz equations

−15−10

−50

510

15

−20

−10

0

10

20

5

10

15

20

25

30

35

40

45

y1

Lorenz equations

y2

y3

J(−6√

2,−6√

2, 27) =

−10 10 0

1 −1 6√

2

−6√

2 −6√

2 −8/3

(17)

has the same eigenvalues as the previous one and consequently the equilibrium point is unstable.We now plot the solution corresponding to initial data [−2,−1, 1] on the time interval tspan = [0, 70],

using ’ode45’. Observe the different graphs. (Notation: x = y(1), y = y(2), z = y(3).)

A-10

Page 11: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−30 −20 −10 0 10 20 300

5

10

15

20

25

30

35

40

45

50

y2

y3

Lorenz equations

0 10 20 30 40 50 60 70 80 90 100

−15

−10

−5

0

5

10

15

t

y1

Lorenz equations

0 10 20 30 40 50 60 70 80 90 100−30

−20

−10

0

10

20

30

t

y2

Lorenz equations

A-11

Page 12: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

0 10 20 30 40 50 60 70 80 90 1000

5

10

15

20

25

30

35

40

45

50

t

y3

Lorenz equations

A.8 Invariant sets

An invariant set for a system x′ = f(x) of dimension n is a set S ⊂ Rn with the property that if x(t) is asolution to the system with its initial value x(0) ∈ S, then x(t) ∈ S for all t ≥ 0.

Example Consider the system

x′ = (1− x− y)x, y′ = (4− 7x− 3y)y.

• Find and classify critical points;

• Show that the x-axis and the y-axis are invariant sets;

• Show that the positive quadrant is an invariant set;

• Show that the square x ∈ (0, 1), y ∈ (0, 1.5) is an invariant set;

• Find all the invariant sets inside the square;

• Discuss and justify the behaviors of all the trajectories of the system.

1. There are four equilibrium points:

• (0, 0) is a nodal source;

• ( 14 ,

34 ) is a saddle point;

• (0, 43 ) and (1, 0) are nodal sinks.

2. Consider initial data (x0, 0). Let (x(t), y(t)) be a solution of the system x′ = (1− x)x, y′ = 0. Note thatx is a function satisfying x′ = (1− x)x and y(t) = 0. It is easy to check that (x(t), 0) also solves the originalsystem, for initial data (x0, 0). Moreover, (x(t), 0) is the unique solution to such a system for the given initialdata. Consequently, any solution with y(0)=0, satisfies y(t) = 0 for all t > 0. Thus, the line y = 0, the x-axis,is an invariant set. Likewise, we show invariance of the y-axis.3. The positive quadrant is invariant. Notice that the only way for solutions starting in the positive quadrantto leave the region is by crossing one of the axis. However, a trajectory reaching a point in the x-axis (or they-axis) has to continue on the line due to the invariance shown in part 2.4. First of all, let us show that a trajectory starting inside the given square and meeting the line x = 1cannot cross it. For this, let us set x = 1 in the governing system: x′ = −xy and y′ = −3(1 + y)y. Note thatx′ < 0, y′ < 0 at points (1, y), i.e., the vector field (tangent vector to the trajectory) points SW. So, since thetrajectory turns SW, it cannot leave the square. Likewise, we argue about how trajectories cannot cross theline y = 1.5.5. To determine all invariant sets, let us find the line where x′ = 0 and the line where y′ = 0 (these are oftencalled nullclines of the system).

A-12

Page 13: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.2

0.4

0.6

0.8

1

1.2

1.4

x

y

Nullclines of $Dx=(1−x−y)x, Dy=(4−7x−3y)y$

x+y=1x+y=1

3y+7x=4

I

III

IV

Setting x′ = 0 in the first equation gives us: x = 0 or x + y = 1. In part 2, we have shown that x = 0 isinvariant (and therefore trajectories cannot cross it). We only need to examine the line x + y = 1. Let ussubstitute x = 1− y in the second equation; this gives

y′ = (−3 + 4y)y.

Therefore,

• x′ = 0 and y′ < 0 for y < 3/4 (on the line x+y = 1): the vector field is vertical and pointing downwards;

• x′ = 0 and y′ > 0 for y > 3/4 (on the line x+ y = 1): the vector field is vertical and pointing upwards.

Setting y′ = 0 in the second equation gives us: y = 0 or 7x+ 3y = 4. In part 2, we have shown that y = 0 isinvariant (and therefore trajectories cannot cross it). We only need to examine the line 7x + 3y = 4. Let ussubstitute y = 1

3 (4− 7x) in the first equation; this gives

x′ = −1

3(−1 + 4x).

Therefore,

• y′ = 0 and x′ > 0 for x > 14 (on the line 7x+ 3y = 4); the vector field is horizontal and points towards

the right);

• y′ = 0 and x′ < 0 for x < 14 (on the line 7x+ 3y = 4); the vector field is horizontal and points towards

the left).

The information gathered so far is shown in the next figure (equilibrium points are marked with circles). Notethat the arrows in the nullcline graphs show invariant regions I, II, III and IV. 6. Solutions corresponding toinitial data in region II, in addition to remaining there for all time, also satisfy, x(t) → 1 and y(t) → 0, ast→∞. This is due to the fact the the vector field points SE for all time. Once the trajectory gets sufficientlyclose to the equilibrium point, the fact that it is asymptotically stable (nodal sink), cause the trajectory totend to it as t grows. Likewise, we show that trajectories with initial data in region IV will tend to (0, 4/3) ast→∞.

Similarly, a solution with initial data in region I will approach (0, 4/3) or (1, 0) or will enter region II orregion IV. In either case, trajectories will tend to either (1, 0) or (0, 4/3) as t→∞.

A-13

Page 14: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.5

1

A.9 Energy methods

Finding an energy equation provides a method for the rigorous justification of the phase plane diagram of atwo-dimensional autonomous system. Furthermore, it is also applicable to systems of many dimensions. Toillustrate the main points of such a method, we will begin with a very well known example:

The linear oscillator.my′′ + ky = 0.

Letting u = y, u′ = v, we rewrite it as

u′ = v, v′ = − kmu.

Denote a2 = km , and calculate

dv

du=dv

dt

dt

du.

Using the given equations,

dv

du= −a

2u

v, vdv = −a2udu.

Integrating the latter yields,

1

2v2 +

a2

2u2 = E, (18)

where E is an arbitrary constant. Identifying the first term on the left with the kinetic energy of the systemand the second one with the potential energy, i.e.,

U(u) =a2

2u2,

equation (18) establishes conservation of energy: the sum of kinetic plus potential energy is constant.The collection of curves in the u − v plane (phase plane) corresponding to different values of E give the

trajectories or orbits of the system. Solutions of the given system of differential equations correspond to orbitsin the phase plane.

Let us show how to use the orbits that we found to obtain solutions of the system.Suppose that we want to calculate the solution corresponding to initial values u(0) = u0 and v(0) = v0.

Substituting these values in equation E, it singles out the special value, E0, whose orbit corresponds to thesolution that we are looking for. For such an E0, we calculate,

v = ±√

2(E0 − U(u)). (19)

Properties of the solutions:

A-14

Page 15: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−6 −4 −2 0 2 4 6−15

−10

−5

0

5

10

15

u

v

Orbits of the system $Du=v, Dv=−a2u$

• Note that (19) is symmetric with respect to the u-axis; (these is the case for all orbits of the system);

• Observe the v = 0 at those points u such that U(u) = E. In such points, the speed v = u′ changessign, turning from positive to negative (or viceversa); at such point, the displacement u reaches eachmaximum (or minimum), and begins to decrease (or increase), up to reaching the minimum value (ormaximum) where v = 0 again. This process keeps repeating itself, and therefore, showing the periodicnature of the solutions.

In addition to finding the energy equation (18) to calculate orbits, we also need the study of the criticalpoints, in order the justify the phase plane structure of the problem. In this case, we see that (0, 0) is the onlyequilibrium point. You can easily check that the linearized system about (0, 0), or equivalently, the Jacobianmatrix of the system has purely imaginary eigenvalues; i.e., (0, 0) is a center.

Now, you can use Matlab to construct the phase plane diagram and check the results previously obtained.

The equations of motion of the pendulum of mass m attached to a solid rod of length L are given by:

T −mg cos θ = 0,

mLθ′′ = −mg sin θ. (20)

The first equation expresses the balance of the weight component along the direction of the rod by the support,and the second one corresponds to Newton’s law of motion due to the transverse component of the weight.Letting ω = θ′, we can express the second equation as a system of two first order ones:

θ′ = ω, ω′ = −a sin θ, (21)

where a = gL .

In order to study all solutions of the (21) we study the equilibrium points and find the energy equation.Observe that the equilibrium points of the system satisfy

ω = 0, and sin θ = 0.

The latter givesθ = πn, n = 0,±1,±2, ... (22)

The Jacobian matrix of the system is; [0 1

−a cos θ 0

]We can distinguish two types of equilibrium points. In the case that θ = πn, n = 0,±2,±4, ..., cos θ = 1, and

J =

[0 1−a 0

].

A-15

Page 16: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

−15 −10 −5 0 5 10 15−3

−2

−1

0

1

2

3

θ

U(θ

)

Potential U(θ) of the nonlinear pendulum

E0

E1

E3

E4

Eigenvalues of J are λ = ±i√a. Therefore, points (0, πn), n = 0,±2, ... are centers.

Likewise, we find (check the Jacobian) that eigenvalues of J in the case that θ = nπ, n = ±1,±3, ... aresaddle points.

Next, we obtain the energy equation following the same procedure as for the harmonic oscillator of theprevious example. It is given by,

1

2ω2 + U(θ) = E, (23)

where U(θ) = −a cos θ. Let us plot U(θ) for the pendulum.Solving (23 for ω yields,

ω = ±√

2(E − U(θ)) (24)

We now have enough information to obtain all the orbits of the system. For this, we analyze (24) for thefollowing choices of energy constant:

• E = E0 = −a;

• E = E1; −a < E1 < a;

• E = E2 = a;

• E > a.

The only solutions that exist in case 1 are those corresponding to the stable critical points (centers). Nosolution exist for initial data other than (nπ, 0), with n even. For initial data as in case 2, closed orbitsaround the centers can be found. These correspond to periodic solutions of the system. Orbits correspondingto E2 = a are separatrices of the saddle points; these enclose periodic orbits. Solutions with initial datasuch that E > a have always nonzero velocity ω, therefore θ is monotonic and the corresponding orbits areunbounded.

As an exercise, use Matlab to draw the phase diagram for this problem.

A.9.1 Conservation of energy

The techniques described in the previous examples are valid for systems of the form

x′ = y, y′ = f(x), (25)

where f is a continuous function on its domain of definition. Let us calculate,

dy

dx=dy

dt

dt

dx=f(x)

y,

A-16

Page 17: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

and consequently, ydy = f(x)dx, yielding

y2

2+ U(x) = E, where U(x) = −

∫f(x) dx. (26)

Equation (26) gives the orbits of the system. Moreover, the properties of the critical points of U(x) determinethe nature of the equilibrium solutions of the equation. In fact, the Jacobian of the system (25) is

J =

[0 1

f ′(x) 0

],

with eigenvalues λ = ±√f ′(x). where x corresponds to an equilibrium value. Note that

• U ′(x) = −f(x): equilibrium points satisfy U ′(x) = 0 = f(x);

• U ′′(x) = −f ′(x): a maximum of U corresponds to f ′(x) > 0, and therefore the eigenvalues of J are real(with opposite sign), in which case (x, 0) is a saddle point; likewise a minimum of U at x corresponds toa center of the system.

A.10 The Method of Liapunov

Now, consider a more general autonomous system

x′ = f(x, y), y′ = g(x, y). (27)

Suppose that (x0, y0) is an equilibrium point of the system. This method consists in finding a continuouslydifferentiable function V (x, y) with the following properties:

• V (x0, y0) = 0; moreover there is an interval U of (x0, y0) such that V (x, y) ≥ 0, in U , and V (x, y) > 0for (x, y) 6= (x0, y0);

• On solutions (x(t), y(t)) of the system, V is a function of t, i.e., V (t) = V (x(t), y(t)), such that dVdt ≤ 0.

A function V (x, y) with Property 1 is called a positive definite function. Likewise a function satisfyingProperty 2 is called negative semidefinite (note that we are not requiring d

dtV (x(t), y(t)) < 0, i.e., strictly).Let us explore Property 2. By the chain rule and using (27), we find that

d

dtV (x(t), y(t)) = ∇V (x(t), y(t)) · (f(x(t), y(t)), g(x(t), y(t))). (28)

A system of differential equations having a function V (x, y) with Properties 1 and 2 is said to have aLiapunov function. The following theorem states how the existence of a Liapunov function V (x, y) helpsdetermining the stability properties of the equilibrium solutions.

Theorem (Liapunov). Suppose that (x0, y0) is an equilibrium solution of (27). Suppose that there is acontinuously differentiable function V defined on a neighborhood U of (x0, y0) that is positive definite with aminimum at (x0, y0) .

• If ddtV is negative semidefinite in U then (x0, y0) is a stable equilibrium point.

• If ddtV is negative definite in U then (x0, y0) is an asymptotically stable equilibrium point.

This result is valid for any system dimension, not necessarily a plane system only.Exercise 1. Consider the damped harmonic oscillator,

my′′ + µy′ + ky = 0

where m,µ and k are positive (nonzero) constants. Show that the energy V (x, y) = m2 y′2 + k

2y2 is a Liapunov

function of the system. Use it to determine the type of stability of the equilibrium point (0, 0).Exercise 2. Consider the equation of the damped nonlinear pendulum,

mLθ′′ = −mg sin θ − µθ′,

where m,L, g, µ are positive (nonzero) constants.

• Find a Liapunov function for the system.

• Use the Liapunov function to study the stability of all equilibrium solutions.

B-17

Page 18: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

B Linear Algebra

One of the most useful features of Matlab is the number of functions that can operate on the individualelements of a matrix.

For instance, typingy = 0 : 0.5 : 2

creates the 1× 5 matrix displayed as:

ans = 0 0.5000 1.0000 1.5000 2.0000

Now, typing cos(y), gives

ans = 1.0000 0.8776 0.5403 0.0707 − 0.4161

Operators that work with matrices areˆ , · , ·∗ ·/

Examples. Entering the matrix a,

a = [1 2; 3 4];

and typing a2 produces the matrix product aa.

Typing a · 2 squares each entry of a.

Typing cos(a · /2) divides each entry of the matrix by 2, then takes the cosine of each entry.

B.1 Matrix Operations

First, to enter the matrix

a =

2 3 4 5 6−4 −5 −6 −7 −83 5 7 9 14 6 8 10 12−2 −3 −4 −5 −6

(29)

enter the following array of numbers:

a = [2 3 4 5 6;−4 − 5 − 6 − 7 − 8;

3 5 7 9 1; 4 6 8 10 12;

−2 − 3 − 4 − 5 − 6] (30)

Now, execute the statements

v = [1 3 5];

b = a(v, 2)

and see the result. Note that b is composed of the elements of the first, third and fifth rows in the secondcolumn of a.

Executing

c = a(v, :) (31)

gives

B-18

Page 19: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

c =

2 3 4 5 63 5 7 9 1−2 −3 −4 −5 −6

(32)

Thus c is composed of the first, third and fifth rows of a.Now, execute the following statements and see the result:

d = zeros(3)

d(:, 1) = a(v, 2).

In your next test, execute

e = a(1 : 2, 4 : 5) (33)

and see the result.To transpose a matrix, use the symbol ’.Execute the command a′ with a as in 29 to get the transpose of a.

B.2 Linear systems and vector spaces

B.2.1 The row-echelon form of a matrix

Consider the linear system,

x+ 2y + z = 4

3x+ 8y + 7z = 20

2x+ 7y + 9z = 23.

Let us solve it by using the row-echelon reduction of the augmented matrix,

a =

1 2 1 43 8 7 202 7 9 23

.Now, we use Matlab to find the reduce row echelon form r of a.

Enter the matrix a, and then type:

r=rref(a)

r =

1 0 0 50 1 0 −20 0 1 3

Writing the system in the new form, the solution immediately follows:

x = 5; y = −2; z = 3.

B.2.2 Subspaces and Basis

Now let us apply the row-echelon reduction to determine basis of subspaces.

Example. Determine whether the vectors

u = (2, 0, 1), v = (−3, 1,−1), w = (0,−2,−1)

are independent. If they are not, identify the independent subset of vectors and express the remaining onesas their linear combination.

B-19

Page 20: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

To test for independence, we need to calculate the determinant of the matrix with columns the componentsof the given vectors:

a =

2 0 1−3 1 −10 −2 −1

T

Using Matlab, we find that det(a)= 0, so they are linearly dependent.Now, we identify the independent vectors and express the dependent ones as linear combinations of the

former. For this, we look for scalars a1, a2, a3 such that

a1u + a2v + a3w = 0. (34)

Note that this vector equation can be written as a system of three linear equations with unknowns a1, a2, a3 :

2a1 − 3a2 = 0,

a2 − 2a3 = 0,

a1 − a2 − a3 = 0.

Equivalently,

a

a1a2a3

= 0.

(Note that there are many solutions to this system). To solve the system, we find the reduced row-echelonform of the matrix a.

At the Matlab prompt, type:

r=rref(a)

It yields r =

1 0 −30 1 −20 0 0

.

Now, solve

r

a1a2a3

= 0,

to geta1 − 3a3 = 0, a2 − 2a3 = 0,

i.e.,a1 = 3a3 and a2 = 2a3. Setting a3 = 1, (34) gives

3u + 2v + w = 0, (35)

i.e., w = −3u− 2v.

A related interpretation:

{u,v} is a basis of the subspace of R3 spanned by {u,v,w}.We also found that rank(a) = 2, since two of its columns (rows) are independent.

B.2.3 Eigenvalues, Eigenvectors and Matrix Diagonalization

The number λ is said to be an eigenvalue of an n × n matrix A provided there exists a vector v 6= 0 suchthat

Av = λv.

To find eigenvalues of a matrix A, at the Matlab prompt, we type

eig(A)

B-20

Page 21: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

This yields the n eigenvalues (real or imaginary) of A.

If you type,

[V, D]= eig(A)

you will get a diagonal matrix D of eigenvalues and a n×n matrix V whose columns are the correspondingeigenvectors. The matrix D is also the diagonalized form of A.

Similar matrices. We will now establish a connection with the matrix similarity discussed in class (page39, equation (10) of Hirsch and Smale). Suppose that A is nonsingular. A and B are similar if there is anonsingular matrix Q such that

B = QAQ−1.

A case of special interest is when B is a diagonal matrix, say B = D. Given A, D is obtained by the eigcommand previously discussed, i.e., typing [V,D] = eig(A).

The matrix Q of the similarity relation can be directly obtained from V , by setting

Q = inv(V ).

B.2.4 The LU-factorization

This provides another method to solve linear systems. Suppose that A is a nonsingular matrix that doesnot have the LU factorization; however, the matrix PA can be written as PA = LU , with P a permutationmatrix. The command lu(A) gives the matrices L, U and the permutation P .

Let

A =

1 2 64 8 −1−2 3 5

(36)

Execute the statement

[L,U, P ] = lu(A) (37)

and see the result.The commands inv(A) and det(A) give the inverse and the determinant of a nonsingular, square matrix

A. The command rank(A) now gives the rank of the matrix A.

C Appendix: A brief survey of Matlab

The purpose of this Appendix is to help you to get started with Matlab. Extensive Matlab tutorials areavailable on-line.

To access Matlab, you should type matlab at the prompt of your xterm window. (Please, be aware thatMatlab is case sensitive, and to start running it, you need to type the word in low-case characters.) Matlabwill then appear in your screen with the prompt >>. You may type demo to get an introduction to theprogram. It is helpful to explore helpwin and helpdesk as well. There are many Matlab tutorials availableon line. You may access some through the website www.mathworks.com, or do an Internet search forMatlab tutorials. Please, practice as much as possible with demos and tutorials. You may also find verygood tutorials in YouTube. Please, browse extensively and find your comfort zone.

C.1 Setting Your Working Environment: Creating Directories

Files and directories are tools that will allow you to save and organize your work.You may want to start with creating the main directory to keep all your working materials (files and

subdirectories). After logging into the system, open an xterm window, type mkdir math5525 and pressreturn. This will create a directory named math5525 where you will be working from and store your coursematerials in the form of files and subdirectories. (You may choose another name for it, instead of math5525,if you wish.)

The command mkdir is used to create a directory.In order to change to the new directory, at the computer prompt you should type cd math5525.The command cd changes directory.

C-21

Page 22: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

If you subsequently type ls, the screen will display all the files and subdirectories of the main directory(math5525; at the beginning ls will show an empty directory). The command ls shows the list of files anddirectories.

In order to save disc space, files should be compressed after use. For this, you should type the commandgzip followed by the name of the file. For instance, if you have a file named homework.pdf in your directory,typing gzip homework.pdf will transform it into homework.pdf.gz. To retrieve the original file, type thecommand gunzip. So, gunzip homework.pdf.gz will give back homework.pdf.

C.2 Setting up variables

C.2.1 Scalar Variables

Suppose we want to set the value of the variable x to be 9. Then, we type x = 9 at the Matlab prompt.Matlab returns

x = (38)

9 (39)

indicating that now x equals 9. To prevent Matlab from printing the value to the screen, we append asemicolon ;to the command (i.e. x = 9;).

1. Arithmetic operations such as +,−, ∗, / are straightforward. As a test, let x = 42, y = 24 and calculatex+ y, x− y, x ∗ y, x/y.

2. Powers are performed as in x2, x11, .... Test xy from the previous example. Note that only 5 digits ofthe mantissa are shown. Matlab calculates with many more digits, though: type the command formatlong and then enter xy again. To go back to the shorter mantissa, type format short. Note that youcan store xy in a variable z by typing z = xy.

3. Trigonometric functions have the expected terminology, sin, cos, tan, cot, sec, csc, with sin(x),cos(x),... giving the numerical values for a real number x. The exponential function responds to thecommand exp. The natural logarithm function corresponds to log, and log10 is the decimal logarithm.With the data provided above, compute sin(y), cos(y), tan(y), etc. with y given above. Typing thecommand elfun you will get a list of the elementary functions available in Matlab.

C.2.2 Vector Variables

One of the outstanding features of Matlab is the handling of vector variables. To enter a row vectorx = (1, 2, 3) one types x=[1 2 3] at the Matlab prompt. The corresponding column vector is representedas y=[1 2 3]’ or y=[1; 2; 3]. As a test, type size(x), size(y), length(x), length(y) at the Matlab promptand see the results. What are the roles of the functions size and length? (You may want to check help sizeand help length.

In addition to providing matrix representation for linear algebra, one important role of the vector functionsin Matlab is to replace loop statements in programs. For this, we need to introduce the colon : command.Typing k=1:n returns all positive integers between 1 and n. Try entering n=10 at the Matlab prompt, pressreturn, and then type k=1:n. (Also, check help colon). Now, suppose that we want to calculate sin(kπ), fork = 1, ...n. We first type k=1:n, press return, and then enter x=sin(k*pi). Test for n = 10.

A related problem is the calculation of sin(xk ∗ pi), where xk are the points 0 = x1 < x2 < ... < xn = 1of the uniformly spaced grid for the interval [0, 1] (with subinterval length 1/n). This is solved through thecommand linspace. Typing z=linspace(a, b, n) creates a row vector z with

z(k) = a+ (k − 1) ∗ (b− a)/(n− 1), for k = 1, ...n.

Test by typing z=linspace(0,1,11). Next enter y=sin(z*pi), and compare the new vector y with theformer x.

By the way, notice that we keep using x, y, z to represent variables. To avoid confusion, once a variable isof no further use, we may remove it with the command clear, by typing clear x. If you type only clear, allthe variables will be removed. It is safer to remove one at a time.

C-22

Page 23: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

C.3 Graphs

The most important command to generate graphs is the plot command. It can be used to generate two aswell as three dimensional objects. The entries of the plot command are vectors:

plot(x, y) plots the vector x against the vector y.

Example. The plot function can be used to join-the-dots x− y plots. Typing:

x = [1.22.12.34.25.56.78.9];

y = [3.12.65.37.63.94.41.7];

plot(x, y)

produces a picture where the points (x(i), y(i)) are joined in sequence.

Example. Create the plot of y = sin(x) over the interval [0, π]. Let us take the x-step size equals to .1.Type:

x = 0 : .1 : π;y = sin(x);plot(x,y)

The graphics command fplot is a useful alternative to the plot command for functions. As a test, trytyping

fplot(’tanh’, [-2,2])

The command plot has many attributes. For instance you may want to print labels for the axes, plotmultiple graphs in one set of axes, vary the thickness and properties of the graph.

Try repeating the two previous examples with the following plot commands:

plot(x, y, ’LineWidth’, 2)

plot(x, y, ’m–’, ’LineWidth, 3, ’MarkerSize’, 5)

To find out how to use all such attributes, type help plot.

C.4 Three–Dimensional Plots

The command plot3 is the three-dimensional analogue of plot. Work out the followingExample. Draw the joint-the-dots curve by taking the points (x(i), y(i), z(i)) in order, where x(i), y(i) andz(i) are generated as follows:

t = −4 : .005 : 4;

x = (1 + t2) sin(20t);

y = (1 + t2) cos(20t);

z = t;

plot(x,y,z)

grid on

xlabel(’x(t)’), ylabel(’y(t)’), zlabel(’z(t)’)

title(’plot3 example’, ’FontSize’, 14)

The function meshgrid is very important in setting up data for three dimensional graphics. Given twovectors, x and y, typing

[X, Y]= meshgrid(x,y) yields two matrices, X and Y, such that each row of X is a copy of x and each columnof Y is a copy of y.

Type the commands help contour and help mesh to learn about producing contour plots and wire-framesurface plots.

C-23

Page 24: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

C.5 Programming in Matlab

Relational operators

1. == Equal to

2. = not equal to

3. < Less than

4. > Greater than

5. <= Less or equal to

Logical operators

1. Not (Complement)

2. & And (True if both are true)

3. | Or (True if either or both operands are true)

The for and while statements in Matlab operate in a similar manner in other languages. They have thefollowing form:

1. for (loop-variable = loop-expression)

executable-statements

2. end

3. if (logical expression)executable-statements

4. else (logical–expression)executable-statements

5. end

6. while (while-expression)executable-statements

7. end

The following example uses nested loops to generate a matrix.Example

for i = 1 : 5A(i, 1) = 1;A(1, i) = 1;

endfor i = 2 : 5

for j-2:5

A(i, j) = A(i, j − 1) +A(i− 1, j);

endend

A

The break command is used to exit from a loop.Example

for k = 1 : 100x = sqrt(k);

C-24

Page 25: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

if ((k > 10)&(x-floor(x)==0)breakend

endk(The floor command rounds a number to the nearest smaller integer.)

The disp command can be used to display text or a matrix.Example

n=10; k=0;while k <= n

x = k/3;

disp([x x2 x3])k = k + 1;

end

C.6 Floating-point arithmetic and round-off errors

We express nonzero floating-point numbers in the form

x = ±(1 + f)2e,

with

0 ≤ f < 1

−1022 ≤ e ≤ 1023, and

The fraction f is representable in the binary system using at most 52 bits. So,

0 ≤ 252f < 252.

Note that 252f is an integer. Double-precision floating-point numbers are stored in a 64-bit word, with 52bits for f , 11 bits for e, and 1 bit for the sign of the number. The sign of e is accommodated by storinge+ 1023, which is between 0 and 211 − 1. Let us denote the minimum and maximum values of the exponentby emin, emax, respectively.

Note that in each binary interval 2e ≤ x ≤ 2e+1, the numbers are equally spaced with an increment of2e−52.

Exercise. Consider a binary system with t = 3 bits to represent the mantissa f , and with emin = −4 andemax = 3. Find the spacing between two consecutive numbers in the binary intervals [1, 2] and [ 12 , 1]. Repeatthe exercise with t = 5.

Notation. Note the difference in notation of the exponent e of the binary representation of x, with respect tothe one used in class, where we write c = e+ 1023.

One important quantity characterizing the accuracy of a computer is known as eps (”machine epsilon”).It is the distance from 1 to the next floating-point number. For the previous model eps = 2−t; for the64-digit pc, eps = 2−52 ≈ 2.2204 · 10−16. We can say that the round-off level is 16 decimal digits. Note thatthe maximum relative error incurred when the result of an arithmetic operation is rounded to the nearestfloating-point number is eps

2 .The following exercise illustrates the effect off round-off error in adding and subtracting numbers.

Exercise. Plot the seventh degree polynomial:

x = .988 : .0001 : 1.012;

y = x7 − 7x6 + 21 ∗ x5 − 35x4 + 35x3 − 21x2 + 7x− 1.

Next, the polynomial y = (x− 1)7 on the same interval. Discuss the plots that you obtain.In plotting the first polynomial, observe that the y-axis scale factor is 10−14. The small values of y are

being computed by taking sums and differences of numbers as large as 35×1.0124. There is severe subtractivecancellation.

D-25

Page 26: Math 5525 Tutorial: Examples with Matlabcalde014/tutorial5525.pdfThe numerical di erential equation solver in Matlab is ode45. A.2.1 First order equations To solve a di erential equation

D Reference

There are many excellent books and guides to Matlab. I strongly recommend the following:Matlab Guide: by Desmond J. Higham and Nicholas J. Higham, SIAM, 2017 (3th edition). The early

editions are also very helpful.

D-26