Makerere University: Chapter Four: Numerical Analysis
Makerere University: Chapter Four: Numerical Analysis
SCHOOL OF ENGINEERING
Let f(x) be a real valued function on the interval . A number, , is called a root of the
function f(x) in this interval if and correspondingly a number, , is called a zero
of the function f(x).
The need to find roots of functions is fundamental to the development and application of
mathematics, and only in simple cases can the roots be determined analytically, so in all other
cases it is necessary to find them numerically.
Numerical determination of the roots of functions
Consider a continuous function f(x) and numbers such that f(α) and f(β) have opposite
signs. Then by the intermediate value theorem, the function f(x) must varnish at least once (must
have at least one root, x0) in the interval as shown in the proceeding figures:
If f(α) and f(β), have the same sign, nothing can be deduced about the existence of a root in the
interval. Figs c – e illustrate situations in which there is a double root, two roots and no root
respectively.
The first step in the bisection method involves dividing (bisecting) the interval,
into the subinterval and , where .
Use the bisection method to find the smallest root of the function correct
to 2 decimal places.
Solution
The function in question can be represented in the figure below:
……………………………………………………………………….. (1)
Then if makes the expression on the right vanish, it follows that x0 is a root of f(x).
The expression of the function, f(x), is however, not unique because g(x) can be written in more
than one way.
If we now consider the function g(x) to map a point x into a point g(x), then a root x = x0 of
equation (1) has the property that g(x) maps the point x0 into itself, and for this reason x0 is called
a fixed point of the equation;
……………………………………………………………………………………… (2)
The fixed point iteration scheme follows from (2) by writing it as;
…………………………………………………………………………………. (3)
Example 4.2.2
Find a fixed point iteration scheme for determining when , and use it to determine
to an accuracy of six decimal places.
Solution
Therefore,
For simplicity, we can take x0 = 1; then the results of the calculation are;
, , , , ,
.
Point x4 and x5 are identical and the difference , hence the required root is
1.414214 (correct to 6 dps)
Example 4.2.3
Devise fixed point iteration schemes to find the roots of the quadratic equation;
and test them numerically
Solution
The fixed iterative schemes of the equation can be obtained by either writing it in the forms;
or
Hence the two possible schemes are;
Scheme A: or
Scheme B:
Investigation of the quadratic shows that the quadratic has roots close to and
Using scheme A:
x0 = 10
x1 = 2.0417 x1 = 10.0417
x2 = 2.0557 x2 = 10.1113
Here the scheme is also partially successful; although it can be successfully be used to determine
the zero close to 10, it can’t be used for that close to 2.
Convergence of a fixed point iterative scheme
Let g(x) be defined in the interval in which it has a fixed point x0, and let g(x) be
continuous throughout this interval with a continuous derivative, such that
. Then the equation has a unique fixed point x0 in the interval and if
the iterative scheme;
Using an approximate value x0 obtained from the graph of f, we let x1 be the point of intersection
of the x – axis and the tangent to the curve at x0, then;
f ( x0 ) f ( x0 )
tan f ' ( x0 ) hence, x1 x0
x0 x1 f ' ( x0 )
f ( x1 )
In the second step we compute x 2 x1 , in the third step we compute x3 from x2 using
f ' ( x1 )
f ( xn )
the same formula, so generally; xn 1 xn .
f ' ( xn )
The following Algorithm therefore, describes the steps taken in finding the zero of the equation
f(x) = 0:
Newton Raphson Algorithm
INPUT: f, f’, initial approximation x0, tolerance (acceptable error), or maximum number of
iterations, N.
1. Compute .
The graph shows that the function has two zeros, one close to 0.5 and the other close to 1.6.
This problem requires determining the zero of the given function to 5 dps. Hence the tolerance,
of the problem is
For ;
n xn
0 0.500000
1 0.450200 0.0498
2 0.451541 0.001341
3 0.451542 0.000001
4 0.451542 0.000000
Hence the zero of the function close to 0.5 is 0.45154 (to 5 dps)
For
n xn
Then instead of the Newton’s method formula, we have the popular secant method as;
Example 4.2.5
Find the positive solution of by the secant method, starting with x0 = 2 and
x1 = 1.9.
Solution
We have that;
n xn-1 xn Nn Dn
1 2.000000 1.900000 -0.000740 -0.174005 0.004253
2 1.900000 1.895747 -0.000002 -0.0006986 0.000252
3 1.895747 1.895494 0 0 0
Hence the root is 1.895494 (correct to 6 Dps)
-The root of
b) Use a fixed point iteration scheme to determine the required roots:
-Find the roots of and check the results by using the quadratic formula.
c) Use Newton’s method to determine the required root:
A simple calculation shows that the straight line segment representing the linear
interpolation function between the points and is given by;
Other higher order Lagrange polynomials for more than four data pairs are determined in a
similar way.
Note
x f(x)
1.5 0.405
2.1 0.742
3 1.099
Use Lagrange interpolation to find f(1.8).
Solution
From result,
Hence, (2 dps)
Example 4.3.2
Given a certain unknown function, f(x), has the following values at the cor-3.5responding x –
values:
x f(x)
1 0.368
1.2 0.301
1.3 0.273
1.5 0.223
Use Lagrange interpolation to find f(1.4).
Solution
The basis of this simple rule can be understood by Figure 4.5, in which the integral is
approximated by the area the trapezoid PQRS shown as the shaded area in the interval
.
……………………………………………………(1) In
this case,
If we denote the error in this approximation by E(h), then;
…………………………………………...(2)
Example 4.4.1
Use the composite trapezoidal rule with, n = 10, n = 30 and n = 50 subintervals to evaluate;
5
sin 3x
I dx
0 x2 x 1
For n = 10,
2 1 0.081476 0.162952
4 2 -0.10561 -0.21122
6 3 0.114301 0.228602
8 4 -0.11709 -0.23418
10 5 0.116795 0.116795
Sum 0.116795 1.0449
Therefore,
5
0.5 0.116795 1.0449 0.29042
sin 3x 1
0 x x 1
2
dx
2
Repeating the procedure for n = 30 and n = 50, using a computer we get and
respectively.
Setting , where h is the step size, and taking an additional point in the interval to be
such that it is midway between a and b, the parabola must pass through the three
consecutive points; , by the Lagrange
interpolation formula, this parabola is given by;
Integrating L(x) over the interval and simplifying the result gives;
………………………………………….(1)
Result (1) is known as the Simpson’s rule or the Simpson’s 1/3 rule. Equation (1) can also be re –
written in the form;
The accuracy of the Simpson’s rule can also be improved by increasing the number of
subintervals, but because the rule involves constructing parabolas through three consecutive
equidistant points, to use the rule for more than three points in the interval the number of points
chosen must be odd so the number of intervals must be even.
Dividing the interval into 2n equal subintervals each of length , and adding
the results gives the composite Simpson’s rule;
xi f(xi) F+L E R
0 0 0
0.5 0.754035 0.754035
1 0.081476 0.081476
1.5 -0.44852 -0.44852
2 -0.10561 -0.10561
2.5 0.3004 0.3004
3 0.114301 0.114301
3.5 -0.21494 -0.21494
4 -0.11709 -0.11709
4.5 0.158398 0.158398
5 0.116795 0.116795
Sum 0.116795 0.549369 -0.02692
At this stage the values of the samples x1 and x2 and those of the weights, of the integration
formula for the sample pints w1 and w2 are unknown. To determine the four numbers, we impose
the requirement that this formula is exact if f(x) is an arbitrary polynomial of degree .
Evaluating the integral on the left, and equating the respective multipliers of the arbitrary
coefficients c0, c1, c2, and c3 to make this result an identity, leads to;
Coefficient c0;
Coefficient c1;
Coefficient c2;
Coefficient c3;
If this approach is extended to n points, an examination of the derivation of the formula shows
that for the sample points, are simply the n roots of the Legendre polynomial
of degree n with the corresponding weights wi given by; .
The general integration formula involving n sample points becomes;
Generally, these results are called Gaussian integration formulas or Gauss – Legendre
integration formulas.
Table 4.1 shows a list of Gaussian sampling points xi and their associated weights for n = 2, 3, 4,
5, 10, and 16.
Use the five – point Gaussian formula to determine, . Compare the result with
the analytical solution of the integral.
Solution
Hence,
This clearly shows a negligible difference between the result from the Gaussian method and the
analytical solution.
Exercise 4.3
1. Use the composite Simpson’s rule with step length h = 0.5 to determine;
and verify that the rule integrates cubics exactly.
compare your result with the exact value . Repeat the calculation with Simpson’s rule
with the same step length.
3. Use the composite trapezoidal and Simpson’s rule each of 10 subintervals to evaluate,
Compare your results with the exact solution . which is the more accurate rule
at computing the integral. Ans: Itrap = 1.849317 and Isimp = 1.851944
4. Use the 3 - , 5 - and 10 – point Gaussian formulas to compute the following integrals and
compare the results with the given exactvalue:
Where .
Example 4.5.1
Use the Euler algorithm with a step size h = 0.2 to find an approximate solution of the first order
initial value problem;
Solution
Setting h = 0.2, n = 10, and in the Euler algorithm leads to the following
results; the column yexact contains the analytical solution.
The error between yn+1 and yexact can be reduced, but not limited, by choosing a smaller step size,
though for significantly greater accuracy it is necessary to make use of a different method.
and then using in place of f (xi , yi ) in Euler’s method at (xi , yi ) to find an improved
estimate at the point (xi+1, yi+1).
The modified Euler algorithm
The approximate numerical solution of the initial value problem;
Where
Setting h = 0.2, n = 10, and in the Heun’s algorithm leads to the following
results;
yn+1 =
n xn yn f(xn,yn) a=xn+h b = yn + hf(xn,yn) f(a,b) yn+0.5h[f(xn,yn)+f(a,b)]
0 0 1 -1.000000 0.2 0.8000 -0.6013 0.8399
1 0.2 0.8399 -0.641198 0.4 0.7116 -0.3222 0.7435
2 0.4 0.7435 -0.354108 0.6 0.6727 -0.1081 0.6973
3 0.6 0.6973 -0.132667 0.8 0.6708 0.0466 0.6887
4 0.8 0.6887 0.028656 1.0 0.6944 0.1470 0.7063
5 1 0.7063 0.135201 1.2 0.7333 0.1987 0.7397
6 1.2 0.7397 0.192376 1.4 0.7781 0.2073 0.7796
7 1.4 0.7796 0.205818 1.6 0.8208 0.1788 0.8181
8 1.6 0.8181 0.181482 1.8 0.8544 0.1195 0.8482
9 1.8 0.8482 0.125662 2.0 0.8733 0.0360 0.8643
10 2 0.8643 0.044948 2.2 0.8733 -0.0648 0.8624
The comparisons of the Heun’s solution, yn(mod) with the Euler’s, yn(e) and exact, yexact solutions
are as shown below:
4.5.3 The forth order Runge – Kutta method for a first order differential equation
This yet another modified method of determining the numerical solution of a first order
differential equation.
The approximate numerical solution of the initial value problem;
subject to the initial condition with step size h can obtained from the
2. Calculate;
for n = 1,2,3…….
4.5.4 The Runge – Kutta algorithm for two first order simultaneous equations
The approximate numerical solution of the initial value problem for the simultaneous first order
initial value problem;
and
and
This forth order Runge – Kutta algorithm with step size h can be modified to find the solution of
the following initial value problem for the single variable second order differential equation
written in the standard form;
with
All that is necessary is to reduce the second order equation to a system of two simultaneous first
order equations, by setting;
and
Example 4.5.3
Use the forth order Runge – Kutta algorithm with step size h = 0.2 to solve the initial value
problem;
in the interval .
Example 4.5.4
Use the forth order Runge – Kutta algorithm with step size 0.1 to find a numerical solution of the
initial value problem;
Setting h = 0.2 and , the results of the spread sheet computations are;
xn +
n xn Yn k1n (1/2)h yn+(1/2)k1n k2n yn+(1/2)k2n k3n xn+h yn+k3n k4n dn yn+1
0 0 1 -0.4 0.1 0.8 -0.2609 0.86955202 -0.28872 0.2 0.711283 -0.17158 -0.27847 0.721532
1 0.2 0.721532 -0.17568 0.3 0.63368955 -0.09681 0.67312641 -0.11259 0.4 0.608946 -0.05717 -0.10861 0.612924
2 0.4 0.612924 -0.05876 0.5 0.58354306 -0.03392 0.59596482 -0.03889 0.6 0.574037 -0.03485 -0.03987 0.573054
3 0.6 0.573054 -0.03445 0.7 0.55582826 -0.04969 0.54820966 -0.04664 0.8 0.526412 -0.07547 -0.05043 0.522623
4 0.8 0.522623 -0.07396 0.9 0.48564483 -0.10878 0.46823217 -0.10182 1 0.420806 -0.1401 -0.10588 0.416748
5 1 0.416748 -0.13848 1.1 0.34751014 -0.17055 0.33147108 -0.16414 1.2 0.25261 -0.18955 -0.16623 0.250514
6 1.2 0.250514 -0.18871 1.3 0.1561588 -0.20002 0.15050518 -0.19776 1.4 0.052758 -0.19542 -0.19661 0.053902
7 1.4 0.053902 -0.19588 1.5 -0.0440363 -0.17789 -0.0350442 -0.18149 1.6 -0.12759 -0.1482 -0.17714 -0.12324
8 1.6 -0.12324 -0.14994 1.7 -0.1982064 -0.10588 -0.1761776 -0.11469 1.8 -0.23793 -0.05938 -0.10841 -0.23165
9 1.8 -0.23165 -0.06189 1.9 -0.2625949 -0.0051 -0.2341976 -0.01646 2 -0.24811 0.043359 -0.01027 -0.24192
10 2 -0.24192 0.040886 2.1 -0.2214798 0.091955 -0.1959455 0.081741 2.2 -0.16018 0.126381 0.085776 -0.15615
11 2.2 -0.15615 0.124767 2.3 -0.093763 0.153193 -0.0795498 0.147508 2.4 -0.00864 0.162189 0.14806 -0.00809
12 2.4 -0.00809 0.161968 2.5 0.07289737 0.158441 0.07113375 0.159146 2.6 0.15106 0.139285 0.156071 0.147985
We set, , hence;
From
The initial conditions are, and , with h = 0.1, the following are the results of the computations obtained using
a spread sheet.
n xn yn zn k1n K1n k2n K2n k3n K3n k4n K4n dn Dn yn+1 zn+1
0 0.0 12.000 0.000 0.000 -9.600 -0.480 -9.648 -0.482 -9.456 -0.946 -9.403 -0.478 -9.535 11.522 -9.535
1 0.1 11.522 -9.535 -0.954 -9.408 -1.424 -9.263 -1.417 -9.073 -1.861 -8.828 -1.416 -9.151 10.106 -18.687
2 0.2 10.106 -18.687 -1.869 -8.832 -2.310 -8.492 -2.293 -8.307 -2.699 -7.870 -2.296 -8.383 7.810 -27.070
3 0.3 7.810 -27.070 -2.707 -7.872 -3.101 -7.335 -3.074 -7.159 -3.423 -6.527 -3.080 -7.231 4.730 -34.301
4 0.4 4.730 -34.301 -3.430 -6.528 -3.757 -5.793 -3.720 -5.629 -3.993 -4.801 -3.729 -5.696 1.001 -39.997
5 0.5 1.001 -39.997 -4.000 -4.800 -4.240 -3.864 -4.193 -3.717 -4.371 -2.692 -4.206 -3.776 -3.205 -43.773
6 0.6 -3.205 -43.773 -4.377 -2.688 -4.512 -1.550 -4.455 -1.422 -4.520 -0.199 -4.472 -1.472 -7.677 -45.245
7 0.7 -7.677 -45.245 -4.524 -0.193 -4.534 1.150 -4.467 1.255 -4.399 2.677 -4.488 1.216 -12.165 -44.029
8 0.8 -12.165 -44.029 -4.403 2.687 -4.269 4.236 -4.191 4.314 -3.972 5.936 -4.216 4.287 -16.380 -39.742
9 0.9 -16.380 -39.742 -3.974 5.951 -3.677 7.708 -3.589 7.756 -3.199 9.578 -3.617 7.743 -19.997 -31.999
10 1.0 -19.997 -31.999 -3.200 9.598 -2.720 11.566 -2.622 11.581 -2.042 13.603 -2.654 11.582 -22.652 -20.417