Chapter5 PDF
Chapter5 PDF
in after for enter i, in after ∈, enter ”1;N”, and in under for, press left
bracket ] to get a vertical line and several .
(5) To get if operator, you need either click on the toolbar or press [Shift][Ctrl][
] ]. you will get
if
enter condition in after if and other statements in before if.
(6) To get return operator, you need either click on the toolbar or press
[Shift][Ctrl][ ], you get
return
in type any information you want to return.
After defining a function, we call it with concrete input data, as
shown in the screen shot, that call the mySolver function to find zero
for both f (x) = x2 − 1, with initial guess x = 2 and g(t) = t3 − 3t2 − 2t,
with initial guess t = −2. Notice there is two ways to call a function,
one way is to define all arguments before calling the function, another
is to define some arguments and call the function with concrete values
for the undefined arguments.
As in all programming language, Mathcad allows you to call one
function from within another. For example, we could create a function
called myDerivative which will compute derivative of a given func-
tion f (x) at a given number x and step size h > 0 using the forward
difference formula: f 0 (s) ≈ f (x+h)−f
h
(x)
. The following screen shot shows
the call of myDerivative inside mySolver,
The algorithm for this function is very simple (here, we break down
the computation in three steps),
Input f , x, and h .
Output: the approximation to f 0 (x) .
• Step One: Set w = f (x + h)
• Step Two: Set d = f (x)
• Step Three: return w−d h
2. Euler’s Method
2.1. Euler’s Method. Euler’s method is the simplest method in
find approximate solutions to first order equations. From the forward
difference formula
f (x + h) − f (x)
f 0 (x) ≈ ,
h
we have
(1) f (x + h) ≈ f (x) + f 0 (x)h
Now if x0 (t) = f (t, x) is a first order differential equations, apply (1),
we have
x(t + h) ≈ x(t) + f (t, x(t))h.
Suppose we want to find approximate solution over interval [a, b] with
initial value x(a) = x0 , we divided the interval into n subintervals
each with length h = b−a n
, the ends of the subintervals is t0 = a, t1 =
a + h, t2 = a + 2h, · · · , tn = a + nh = b. Start with x0 we can compute
x1 = x0 + f (t0 , x0 )h
x2 = x1 + f (t1 , x1 )h
..
.xn = xn−1 + f (tn−1 , xn−1 )h
Example 2.1. Find approximate to x(1) if x0 (t) = t2 −ex sin(t), x(0) =
1 with h = 0.25.
Notice the line to line corresponding between the Mathcad and the
algorithm. Since Mathcad programming language is a scripting lan-
guage, the translation between algorithm and code is straight forward,
and you don’t need to worry about the variable type, io, etc. Also,
without explicit return statement, the result of last is, by default, will
be returned.
6 1. NUMERICAL METHODS FOR ORDINARY DIFFERENTIAL EQUATIONS
The next screen shot shows a call to the myEuler and tMesh for
the equation x0 = t2 x + t2 sin(t3 ). It also shows the graph of approxi-
3
mate solution comparing with the exact solution x(t) = − 10 cos(t3 ) −
1 1
3 10 t 3
10
sin(t3 ) + 10 e
3. Runge-Kutta Method
3.1. Runge-Kutta Method. There are several Runge-Kutta meth-
ods, but the fourth order Runge-Kutta method is most popular. Sup-
pose x0 (t) = f (t, x), x(a) = x0 . and h = b−a
n
is the step length, the
fourth order Runge-Kutta method is given below,
k1 = hf (ti , xi )
k2 = hf (ti + 12 h, xi + 12 k1 )
k3 = hf (ti + 12 h, xi + 12 k2 )
k4 = hf (ti + h, xi + k3 )
xi+1 = xi + 16 (k1 + 2k2 + 2k3 + k4 )
In general Runge-Kutta method gives more accurate result than Eu-
ler’s method at the same step length. However, Runge-Kutta method
is more expensive to implement, that is, at each step, Runge-Kutta
method requires more computation than Euler’s method( four func-
tion evaluations compare one in Euler’s method).
Example 3.1.
Example 3.2. Find approximate to x(1) if x0 (t) = t2 −ex sin(t), x(0) =
1 with h = 0.25.
Solution Here the interval is [0, 1], so a = 0, b = 1. Since
b−a
h = n = 0.25 we have n = 4 and we need to compute x1 , x2 , x3 , x4
starting with x0 = x(0) = 1, t0 = 0
8 1. NUMERICAL METHODS FOR ORDINARY DIFFERENTIAL EQUATIONS
x1 :
x2 :
x3 :
x4 :
k1 = hf (t3 , x3 ) = 0.25 ∗ f (0.75, 0.568891) = −0.641483
k2 = hf (t3 + 21 h, x3 + 12 k1 )
= f (0.875, 0.568891 + 0.5(−0.641483)) = −0.485628
k3 = hf (t3 + 21 h, x3 + 12 k2 )
= 0.25f (0.875, 0.568891 + 0.5(−0.485628)) = −0.510243
k4 = hf (t3 + h, x3 + k3 )
= 0.25f (1, 0.568891 − 0.510243) = −0.308297
x4 = x3 + 61 (k1 + 2k2 + 2k3 + k4 )
= 0.568891 + 61 (−0.641483 + 2 ∗ (−0.485628) + 2 ∗ (−0.510243) − 0.308297)
= 0.446327
t4 = t3 + h = 0.75 + 0.25 = 1.0
So the approximation to x(1) is x(1) ≈ x4 = 0.446327. a
The next screen shot shows a call to the myRungeKutta and tMesh
for the equation x0 = t2 x+t2 sin(t3 ). It also shows the graph of approx-
3
imate solution comparing with the exact solution x(t) = − 10 cos(t3 ) −
1 1
3 10 t 3
10
sin(t3 ) + 10 e , and with the approximate solution using Euler’s
method. In this case Runge-Kutta provides much superior result, an
almost exact match!
a
4. NUMERICAL METHOD FOR SYSTEM OF EQUATIONS 13
a
we also plot the solution in xy-plane, which is called the velocity-
position phase plane, with y-axis represent the velocity and x-axis is the
4. NUMERICAL METHOD FOR SYSTEM OF EQUATIONS 15