Professional Documents
Culture Documents
Numerical Methods Using Mathematical Software
Numerical Methods Using Mathematical Software
Objective:
✓ Nonlinear equations
✓ Function and data approximation
✓ Numerical differentiation and integration
✓ System of Linear equations
✓ First-order differential equations – numerical solution using Euler method, Runge-
Kutta method, round off error
1
Example
Use Bisection method to find solutions accurate to within 10−2 for the following problems.
𝑓(𝑥) = 𝑥 3 − 7𝑥 2 + 14𝑥 − 6 [2.7,3.2]
Solution:
>
>
>
>
>
>
>
2
Secant Method
• The Secant command numerically approximates the roots of an algebraic function, f,
using a technique similar to Newton's method but without the need to evaluate the
derivative of f.
• Given an expression f and an initial approximate a, the Secant command computes a
sequence 𝑝𝑘 , 𝑘 = 0. . 𝑛, , of approximations to a root of f, where n is the number of
iterations taken to reach a stopping criterion.
• The Secant command is a shortcut for calling the Roots command with the
method=secant option.
• Calling Sequence
o Secant(f, x=[a, b], opts)
o Secant(f, [a, b], opts)
• Parameters
f - algebraic; expression in the variable x representing a continuous function
x - name; the independent variable of f
a - numeric; one of two initial approximates to the root
b - numeric; the other of the two initial approximates to the root
opts - (optional) equation(s) of the form keyword=value, where keyword is one
of functionoptions, lineoptions, maxiterations, output,
pointoptions, showfunction, showlines, showpoints,
showverticallines, stoppingcriterion, tickmarks, caption,
tolerance, verticallineoptions, view; the options for approximating
the roots of f
Example
Solution:
>
3
>
>
>
>
To play the following animation in this help page, right-click (Control-click, on Macintosh) the
plot to display the context menu. Select Animation > Play.
>
>
4
Newton Method
• The Newton command numerically approximates the roots of an algebraic function, f,
using the classical Newton-Raphson method.
• Given an expression f and an initial approximate a, the Newton command computes a
sequence 𝑝𝑘 , 𝑘 = 0. . 𝑛, of approximations to a root of f, where n is the number of
iterations taken to reach a stopping criterion. For sufficiently well-behaved functions
and sufficiently good initial approximations, the convergence of 𝑝𝑘 toward the exact
root is quadratic.
• The Newton command is a shortcut for calling the Roots command with the
method=newton option.
• Calling Sequence
o Newton(f, x=a, opts)
o Newton(f, a, opts)
• Parameters:
f - algebraic; expression in the variable x representing a continuous
function
x - name; the independent variable of f
a - numeric; the initial approximate root
opts - (optional) equation(s) of the form keyword=value, where keyword is
one of fixedpointiterator, functionoptions, lineoptions,
maxiterations, output, pointoptions, showfunction, showlines,
showpoints, showverticalline, stoppingcriterion, tickmarks,
caption, tolerance, verticallineoptions, view; the options for
approximating the roots of f
Example
Use Newton’s method to find solutions accurate to within 10−2 for the following problems.
𝑓(𝑥) = 𝑒 𝑥 + 2−𝑥 + 2 cos(𝑥) − 6 at 𝑥0 = 2.0
Solution:
>
>
>
>
>
>
5
>
Exercise
6
6. Repeat Exercise 5 using the Secant method.
Numerical Integration
Trapezoidal Rule:
Simpson’s Rule:
7
Example 1:
2
Compare the Trapezoidal rule and Simpson’s rule approximations to ∫0 𝑓(𝑥)𝑑𝑥 when 𝑓 (𝑥)
is
Solution:
>
>
>
>
>
>
>
>
8
>
Example 2:
48
Approximate the following integrals using the Trapezoidal rule ∫0 √1 + cos(𝑥)2 𝑑𝑥 using
Simpson's 3/8 rule
Solutions:
>
>
>
9
>
• The command to numerically approximate the integral of the expression above using
Simpson's 3/8 rule is
>
>
Exercise:
1. Use the Composite Trapezoidal rule with the indicated values of n to approximate the
following integrals.
10
2. Use the Composite Simpson’s rule to approximate the integrals in Exercise 1.
2
3. Approximate ∫0 𝑥 2 𝑙𝑛(𝑥 2 − 1) 𝑑𝑥 using ℎ = 0.25. Use
a. Composite Trapezoidal rule.
b. Composite Simpson’s rule.
c. Composite Simpson’s 3/8 rule.
Differential Equations
• dsolve solves many equations in terms of these functions. But many other differential
equations cannot be solved explicitly in terms of the functions of calculus.
• Consider, for example, the equation
• When we cannot solve a differential equation in this way, or if the formula we find is too
complicated, we turn to numerical methods to solve initial value problems
• We illustrate the use of the numerical version of dsolve by considering the initial
valuue problem
𝑑𝑦 𝑡
= , 𝑦(0) = 1 (7.1)
𝑑𝑥 𝑦
• Since we have an explicit formula for this solution, we will be able to compare the
approximate solution 𝑦𝑎 (𝑡) and the exact solution 𝑦(𝑡).
• Commands for dsolve(...,numeric) are similar to those for dsolve. To obtain 𝑦𝑎 (𝑡), first
enter the command
>
>
>
11
>
>
>
𝑑𝑦 𝑡
Figure 7.1: Numerical Solution of 𝑑𝑡 = 𝑦 , 𝑦(0) = 1
• The result is shown in Figure 7.1. Note however that the command plot (ya(t), t =0.2)
results in an error message, because the function 𝑦𝑎 allows only numerical input.
Exercise 7.1 Display both 𝑦𝑎 (𝑡) and the exact solution 𝑦(𝑡) = √𝑡 2 + 1 on the same graph.
Can you distinguish between the two curves?
• To compare the numerical solution with the exact solution we can make a table listing
the values of each of the functions at various points.
• First, we define a function that produces the entries in each row.
• To do this type
>
>
> :
>
12
Example
Consider
𝑑𝑦
= 𝑡 + 𝑦2, 𝑦(0) = 1 (7.2)
𝑑𝑥
Solution:
>
>
>
>
Exercise 7.2 The solution 𝑦(𝑡) to (7.2) is increasing for t positive, and there is a finite value
t such that lim∗ 𝑦(𝑡) = ∞; the interval of existence of the solution is (−∞, 𝑡 ∗ ). Execute the
𝑡→𝑡
previous two commands and the command plot(y2a, 0..1, 0.100). The picture should tell
you roughly where 𝑡 ∗ is located. Now try to evaluate y2a both a bit to the left and a bit to
the right of this value. Does Maple's response reveal the value of 𝑡 ∗ ?
13
Numerical Methods
Calling Sequence
Parameters
• A number of classical methods are available through use of the choice option in the
method=classical[choice] selection.
o The default is foreuler, the forward Euler method.
o heunform is the Heun formula (also known as the improved Euler method).
o impoly is the improved polygon method (also known as the modified Euler
method),
o rk2 is the second-order classical Runge-Kutta method. This is the same as the Heun
formula.
o rk3 is the third-order classical Runge-Kutta method,
o rk4 is the fourth-order classical Runge-Kutta method,
o adambash is the Adams-Bashforth method (a predictor method),
14
o abmoulton is the Adams-Bashforth-Moulton method (a predictor-corrector
method),
• In order to give some idea about how dsolve(... , numerie) calculates approximate
values, we are going to discuss several numerical methods. We will apply each method
to problem (7.1).
• We want to approximate the solution of the initial value problem
𝑑𝑦
= 𝑓(𝑡, 𝑦), 𝑦(𝑡0 ) = 𝑦0
𝑑𝑥
on an interval to 𝑡0 ≤ 𝑡 ≤ 𝑏. For n a positive integer, we divide the interval into n parts using
points to
• For simplicity, we assume that each part has the same width, or step size, ℎ = 𝑡𝑖+1 − 𝑡𝑖 =
(𝑏 − 𝑡0 )/𝑛; therefore 𝑡𝑖 = 𝑡0 + 𝑖ℎ. At each point 𝑡𝑖 we seek an approximation, which we
call 𝑦𝑖 , to the true solution 𝑦(𝑡𝑖 ) at 𝑡𝑖 :
• Its exact solution is easily found to be
𝑦(𝑡𝑖 ) ≈ 𝑦𝑖
𝑑𝑦 𝑡
= , 𝑦(0) = 1 (7.1)
𝑑𝑥 𝑦
We wish to approximate 𝑦(0.3) using the Euler Method with step size ℎ = 0.1 and three steps.
We find
>
>
>
15
>
>
>
Example 7.2 We again consider problem (7.1) and find an approximation to y(0.3) with
h = 0.1. We oblain
>
>
16
>
>
>
>
>
>
17
• Comparison Errors
>
>
>
>
18
options that tolerances can be specify tolerance values. For example, using the default
command
>
>
>
>
>
>
>
>
gives a solution value of the initial value 2.23606805572581, which problem (7.1) at t = 2 of is
an eight-digit approximation to the exact value √5. The command
>
>
>
>
19
Changes the tolerances and yields the value 2.23606797749999 at 𝑡 = 2, which is a 13 digit
approximation to the exact value. The default values will be satisfactory for most of the problems
in this book. For other problems, however, greater accuracy will be required.
The exact solution is 𝑦(𝑡) = 𝑒 −2𝑡 . Now we find the numerical solution using dsolve(. .. ,
numeric), and plot it (with a dashed line) together with the exact solution (with a solid
line):
>
>
>
>
Figure 7.2: Exact (solid line) and Approximate (dashed line) Solutions
20
Let's do this with our example. Specifically, we calculate and plot the numerical solution,
ya2, satistying more stringent accuracy requirements, together with the first numerical
solution, yal, and the exact solution, y. We display the exact solution with a solid line, yal
with a dashed line, and ya2 with a dotted line.
>
>
>
>
21
Exercises
22
23
Solving and Analyzing Second Order Linear Equations
• The most basic second order differential equations are linear equations with constant
coefficients:
• These equations model a wide variety of physical situations, including oscillations of springs,
simple electric circuits, and the vibrations of tuning forks to produce sound and of electrons
to produce light.
• Unfortunately, we often cannot find solution formulas for more general second order
equations, even for linear equations with variable coefficients:
• Such equations have important applications to physics. For example, Airy's equation,
𝑦′′ − 𝑡𝑦 = 0,
24
1
𝑦′′ + 𝑡 𝑦′ + 𝑦 = 0,
• Occurs in the study of vibrations of a circular membrane and of water waves with circular
symmetry.
𝑦 ′′ + 𝑦 ′ − 6𝑦 = 20𝑒 𝑡 . (9.3)
>
>
As expected, the general solution depends on two arbitrary constants. To solve the
differential equation with initial conditions 𝑦(0) = 0 and 𝑦’(0) = 1, we would type:
>
Maple can also solve some boundary value problems. Let's continue using the same
differential equation, and consider the boundary conditions 𝑦(0) = 0 and 𝑦(𝑙𝑛(2)) = 10.
We get the solution of the boundary value problem by typing:
>
Example 9.2 Now consider the linear second order differential equation
𝑦′′ + 𝑡 2 𝑦′ + 𝑦 = 0. (9.4)
Maple is unable to find an explicit formula for the general solution. Instead, we can use
dsolve(..., numeric) to find a numerical solution. The numerical solution with initial
conditions 𝑦(0) = 1, 𝑦′(0) = −1 can be found by typing
>
25
• A good way to understand the behavior of a general solution of a second order equation is to
plot numerical solutions corresponding to a wide range of initial values and initial slopes.
• For this example, to plot solutions satisfying the twenty five initial conditions 𝑦(0) =
−2, −1, 0, 1,2 and 𝑦′(0) = −1, −0.5, 0,0.5, 1 we use the Maple command DEplot (which is
found in the DEtools package). Here are the commands to do this.
>
>
>
>
• The result is shown in Figure 9.1. We see that the solutions seem to level off for positivet and
seem to blow up for negative t.
• Here we have the same differential equation, but we have specified values of the solution at
two different points; y(0) = 0 and y(1) = 1 are called boundary conditions. This time dsolve
cannot find a solution, so we discuss two alternate approaches. The first is called the
shooting method. Consider the initial value problem
26
where the first initial condition is the first boundary condition, and in the second initial
condition, the slope at t = 0 is specified by the parameter s. Since for any value of s, the solution
of this initial value problem satisfies the first boundary condition in (9.5), if we find the value s =
8for which the solution satisfies the second boundary condition in (9.5). then the solution of (9.6)
with s = 5 will be the solution of our boundary value problem. 8 is usually found by selecting
various numerical values for s, and solving the initial value problem (9.5) numerically with
dsolve, with an eye to finding a value for s for which the Second boundary condition in (9.5) is
satisfied.
• The following commands can be used to
>
>
• The command trial(s) evaluates the numerical solution of the initial value problem with
𝑦(0) = 0 and 𝑦′ (0) = 𝑠 at 𝑡 = 1. After some trial and error, one finds in this case
that
>
>
>
>
27
Figure 9.3: Solution of a Boundary Value Problem with dsolve(..., numeric)
>
>
>
>
>
>
>
28
• We could produce the same figure using DEplot, which also allows us to specify initial
condition for the derivatives in a higher order differential equation.
• The corresponding command is
>
>
29
for a first order system of differential equations. This initial value problem can be written
in vector form as follows:
• We can use dsolve(... , numeric) on this system much as we used it on a single first
order equation. We illustrate with the initial value problem
>
>
>
30
You can also extract functions that approximate 𝑥(𝑡) and 𝑦(𝑡), as in Chapter 7. For example,
after running dsolve(... , numeric) as above, the following commands extract and plot the
approximate solution.
>
>
>
• Note that we did not type 𝑥𝑎(𝑡) and 𝑦𝑎(𝑡) in the plot command; this is because the
functions 𝑥𝑎 and 𝑦𝑎 allow only numerical input.
• Though it is possible to produce phase portraits with dsolve(. .* , numeric) and plot,
the procedure is cumbersome.
31
• It is simpler to use DEplot (from the DEtools package), which we introduced in Chapter
8. For example, you can type
>
>
>
>
• The procedure for plotting a phase portrait also works if you wish to vary only one parameter
in the initial data. For example, consider the initial value problem
>
>
32
>
>
>
• Finally, we note that DEplot also works for systems of more than two equations, and for
time-dependent (nonautonomous) systems. An important option in these cases is scene,
which is used to specify a pair of variables to plot. For example, you can get a plot Of
versus t for a trajectory of the above system by typing:
>
33
Qualitative Theory for Systems of Differential Equations
Vector fields can be drawn by hand for simple systems, but the command fieldplot can
draw vector fields of any first order system of two equations. We illustrate the drawing of
vector fields by examining two specific systems, namely
• System (13.3) is a predator-prey model with logistic growth (for the prey).
• The command fieldplot is contained in the plots package. The following sequence of
commands draws a vector field for system (13.2):
• >
• >
• A Simple way to see the directions of vectors near the critical points is to replace
fieldplot with dfieldplot. This command is contained in the DEtools package; we
used it previously to plot the direction field of a single first order differential
equation.
• It produces plots in which every vector has the same length. The drawback, of
course, 1s that it may be harder to locate the critical points (since there are no short
arrows); the Compensating benefit is that we can see the directions more clearly.
• Here is the command we use to plot the "equal length" vector field.
34
>
• The third argument t = 0..1 is irelevant to the vector field plotted in this example, but
the syntax of dfieldplot requires that some range be specified for the independent
variable t. The result is shown in Figure 13.2.
Exercises
1. In this problem, we'll investigate the effect of damping on the pendulum, using the
model
𝜃′′ + 𝑏𝜃′ + 𝑠𝑖𝑛 𝜃 = 0.
a) Define a Maple function of b that will find a numerical solution of this differ equation
with the initial conditions 𝜃(0) = 0, 𝜃′ (0) = 4.
b) For 𝑏 = 0.5, 1, 𝑎𝑛𝑑 2, plot these solutions from 𝑡 = 0 𝑡𝑜 𝑡 = 20.
c) Do the same for the linear approximation
𝜃′′ + 𝑏𝜃′ + 𝜃 = 0.
d) Compare the linear and nonlinear behavior for these values of b. Interpret what is
happening physically in each case; i.e., describe explicitly what the graph says the
pendulum is doing.
35
Solution:
Part (a)
>
>
Part (b)
>
>
>
>
>
>
>
Part (c)
>
36
>
>
>
>
>
>
>
37
>
>
>
>
38
2. Consider the predator-prey model
in which 𝑥 ≥ 0 represents the population of the prey and 𝑦 ≥ 0 represents the population
of the predators.
a) Find all critical points of the system. At each critical point, calculate the
corresponding linear system and find the eigenvalues of the coefficient matrix; then
identify the type and stability of the critical point.
b) Plot the vector field on a region small enough to distinguish the critical points but
large enough to judge the possible solution behaviors away from the critical points.
c) Use several initial data points (zo, yo) in the first quadrant to draw a phase portrait
for the system. Identify the direction of increasing t on the trajectories you obtain.
Use the information from parts (a) and (b) to choose a representative sample of
initial conditions. Then combine the vector field and phase portrait on a single
graph.
d) Explain from your phase portrait how the populations vary over time for initial data
close to the unique critical point inside the first quadrant. What happens for initial
data far from this critical point?
e) Suppose the initial state of the population is given by
𝑥(0) = 1, 𝑦(0) = 1.
Find the state of the population at t = 1,2,3, 4,5.
f) Estimate to two decimal places the period of the solution curve that starts at (1,1).
Solution:
>
>
>
39
>
>
>
>
>
40
Part (c): first attempt
To plot the phase portrait of the system, we are going to use the command DEplot, which is
in the DEtools package. We first use a rectangular grid to generate different initial
conditions. The vector field above makes it clear which direction the solutions are going in
the portrait below as t increases. Many of the solutions rapidly approach infinity as t
decreases through negative values. Without the option stepsize = 0.01 in the command
below. Maple would give multiple warnings before drawing the portrait. We first calculate the
initial values and then plot the phase portrait.
>
>
>
>
41
>
>
>
42
Part (d): table of population values
Here is a table of the population from times 0 to 20. The population change rapidly
between t = 0 and t = 1.
>
>
>
>
3. Consider the competing species model (Boyce and DiPrima, Prob. 4, Sect. 9.4)
For 𝑥, 𝑦 ≥ 2 0.
a) Find all critical points of the system. At each critical point, calculate the
corresponding linear system and find the eigenvalues of the coefficient matrix; then
identify the type and stability of the critical point.
b) Plot the vector field on a region small enough to distinguish the critical points but
large enough to judge the possible solution behaviors away from the critical points.
c) Use several initial data points (to, yo) in the first quadrant to draw a phase portrait
for the system. Identify the direction of increasing t on the trajectories you obtain.
Use the information from parts (a) and (b) to choose a representative sample of
initial conditions. Then combine the vector field and phase portrait on a single
graph.
d) Suppose the initial state of the population is given by
43
Solution:
>
>
>
>
>
>
44
Part (b): vector field
Figure Sols.16 shows a plot of the vector field of the system. The critical points at (0,0)
and at (0,4) are easy to recognize, but the other two critical points are less clear. The latter
two points, at (1.5,0) and at (1, 1), are relatively close to each other, and the direction of
the vector field is rapidly changing in this area.
>
>
>
45