CS计算机代考程序代写 scheme matlab AI Excel algorithm Numerical Methods in Engineering (ENGR20005) Book

Numerical Methods in Engineering (ENGR20005) Book
A. Ooi a.ooi@unimelb.edu.au
July 24, 2020

2

Contents
1 Mathematical Preliminaries 5
2 Root Finding 11
2.1 Findingrootsofequations …………………… 12 2.1.1 GraphicalMethod …………………… 13
2.2 Bracketingmethods………………………. 14 2.2.1 TheBisectionMethod …………………. 15 2.2.2 MethodofFalsePosition………………… 17
2.3 Openmethods…………………………. 20 2.3.1 Fixed(One)PointIteration ………………. 21 2.3.2 NewtonRaphsonMethod ……………….. 24 2.3.3 SecantMethod …………………….. 29
2.4 Systemofnonlinearequations …………………. 30
3 Systems of linear algebraic equations 35
3.1 DirectMethods ………………………… 37
3.1.1 GaussElimination …………………… 37
3.1.2 LUDecomposition …………………… 42
3.1.3 Using LU decomposition to find the inverse of a matrix . . . . 51
3.2 IterativeMethods……………………….. 54 3.2.1 PointJacobi………………………. 55 3.2.2 Gauss-Seidel………………………. 56 3.2.3 Convergenceofiterativescheme…………….. 59
3.3 Newton’s method for nonlinear equations (revisited) . . . . . . . . . . 62
4 Least Squares 67
4.1 Linearleastsquaresapproximation ………………. 68 4.2 Polynomialleastsquaresapproximation. . . . . . . . . . . . . . . . . 69
3

4 CONTENTS
5 Interpolation 73
5.1 NewtonInterpolation……………………… 74
5.1.1 LinearNewtonInterpolation………………. 74
5.1.2 QuadraticNewtonInterpolation…………….. 75
5.1.3 General form of Newton’s Interpolating Polynomials . . . . . . 77
5.2 LagrangeInterpolatingPolynomials………………. 79
5.3 SplineInterpolation………………………. 87 5.3.1 Linearsplines ……………………… 88 5.3.2 Quadraticspline ……………………. 89 5.3.3 CubicSplines ……………………… 94
6 Differentiation and Integration 105
6.1 Approximationofthe2ndDerivative ……………… 107 6.2 Integration…………………………… 109 6.3 Simpson’srule…………………………. 113
7 Boundary Value Problem 119
8 Ordinary Differential Equations 123
8.1 Euler’smethod ………………………… 123
8.2 BackwardEulerMethod ……………………. 126
8.3 TrapezoidalorCrankNicolsonmethod …………….. 127
8.4 RungeKuttamethods …………………….. 129
8.4.1 SecondOrderRungeKuttaMethod. . . . . . . . . . . . . . . 130
8.4.2 4thOrderRungeKuttaScheme(RK-4) . . . . . . . . . . . . 133
8.5 Stabilityanderroranalysis…………………… 135
8.6 SystemsofOrdinaryDifferentialEquations . . . . . . . . . . . . . . . 138
8.6.1 Multi-dimensional Runge-Kutta methods . . . . . . . . . . . . 139

Chapter 1
Mathematical Preliminaries
In this chapter, some of the theorems that will be used for the rest of the course will be reviewed. Only a brief descrition is given here. More details can be found in the excellent text by [1].
Theorem 1 (Rolle’s Theorem) Suppose f is a continuous function in the domain (a,b). If f(a) = f(b) = 0, then a number c exist in (a,b) such that f′(c) = 0. This theorem is illustrated in Fig. 1.1.
Exercise1.1: Forthefunctionf(x)=sin(x)inthedomain(0,π),
(a) Sketch the function f(x) in the domain specified above.
(b) What is the value of f(0) and f(π).
(c) What is the value of c such that f(c) = 0 ? Your answer should confirm that 0 < c < π. Theorem 2 (Mean Value Theorem) If f is a contiuous function and differen- tiable between (a, b), then a number c exist such that f′(c) = f(b) − f(a). (1.1) b−a This theorem is illustrated in Fig. 1.2. 5 6 CHAPTER 1. MATHEMATICAL PRELIMINARIES f(x) df(x)/dx=0 ac b x Figure 1.1: Figure illustrating Rolle’s theorem y Slope=(f(b)-f(a))/(b-a) y=f(x) Slope=df(c)/dx acb x Figure 1.2: Figure illustrating Mean value theorem. Theorem 3 (Extreme Value Theorem) If f (x) is a continuous function in [a, b], then there exist two numbers, c1 and c2 in [a,b] such that f(c1) ≤ f(x) ≤ f(c2). Furthermore, if f is differentiable on (a, b), then c1 and c2 occur either at endpoints a and b or where f′(x) = 0. This theorem is illustrated in Fig. 1.3. y f(c2) f(c1) Figure 1.3: Figure illustrating extreme value theorem. 7 y=f(x) ac2 c1bx Exercise 1.2: Find the maximum value for |f(x)| where f(x)= 13(2−ex +2x). (1.2) Do this on the intervals [0,1] and [0,0.5]. Theorem 4 (Taylor’s Theorem) If f ∈ C n [a, b], that f (n+1) exist on [a, b], and x0 ∈ [a, b]. For every x ∈ [a, b] there exists a number ξ(x) between x0 and x with where Pn(x) = f(x) = Pn(x) + Rn(x), 􏰐n f(k)(x0)(x − x0)k k! k=0 = f(x0)+f′(x0)(x−x0)+ f′′(x0)(x−x0)2 +···+ f(n)(x0)(x−x0)n 2! n! and 8 CHAPTER 1. MATHEMATICAL PRELIMINARIES Rn(x) = f(n+1)(ξ(x))(x − x0)n+1 (n+1)! Pn(x) is the nth Taylor Polynomial for f about x0 and Rn(x) is the remainder term which is also known at the truncation error. Exercise 1.3: Find the second Taylor polynomial P2(x) for the function f(x) = ex cos(x) about x0 = 0. (a) For 0 ≤ x ≤ 0.5, find upper bound for the error between P2(x) and f(x) (|f(x)−P2(x)|) using the error formula, and compare it to the actual error. (b) For 0 ≤ x ≤ 1.0, find upper bound for the error between P2(x) and f(x) (|f(x) − P2(x)|) using the error formula. Compare it to the actual error. (c) Approximate 􏰏 1 f(x)dx using 􏰏 1 P2(x)dx. 00 (d) Find an upper bound for the error in (c) using 􏰏 1 |R2(x)|dx, and compare 0 it to the actual error. Part solution to the above exercise is shown in Fig. 1.4. Theorem 5 (Weighted Mean Value Theorem for Integrals) If f is a contin- uous function and differentiable between (a,b) and if g(x) does not change sign in (a, b), then there exist a number c such that 􏰒b 􏰒b f(x)g(x)dx = f(c) g(x)dx. (1.3) aa 9 2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 0 0.2 0.4 0.6 0.8 1 x P2(x)=1+x f(x) Figure 1.4: Figure showing part solution to Exercise 1.3. 10 CHAPTER 1. MATHEMATICAL PRELIMINARIES Chapter 2 Root Finding The text by [2] has this excellent example where root finding becomes important. Consider the forces acting on a mass thrown out from a plane. According to Newton’s law: nett force on an object = mdv dt where m is the mass of the turtle, v velocity of the turtle and t is time. For a mass in free fall, Therefore nett force = weight of mass + drag on turtle = mg−cv dv = g − c v dt m (2.1) (2.2) Exercise 2.1: Solve Eq.(2.2) and show that m ifv=0att=0. ThisfunctionisshowninFig. 2.1. v(t) = gm 􏰄1 − e− ct 􏰅 (2.3) c Suppose that the velocity of the mass is governed by Eq. (2.3), find the value of the drag coefficient, c, such that the mass of, m = 5, can attain a prescribed velocity, v = 10, at a particular time, t = 9. Use g = 10. In the context of the problem 11 12 CHAPTER 2. ROOT FINDING gm/c t Figure 2.1: Figure showing the function v(t) defined by Eq. (2.3) mentioned above, m, v, and t are constants (parameters) of the problem. The only variable that we can adjust is c. To solve this problem, rewrite Eq. (2.3) as (2.4) c So the task of solving the problem reduces to merely finding the value of c, such that f(c) = 0, for the given values of v, m and t. The graph of f(c) (Eq. (2.4)) is shown in Fig. 2.2. We need to find a value of c such that f(c) = 0. In numerical analysis this problem is called finding the roots of equation. There are many methods of finding the the roots of an equation. They are given in the following sections. 2.1 Finding roots of equations The methods of solving roots of equations can be broadly classified as • Graphical method m f (c) = gm 􏰄1 − e− ct 􏰅 − v(t). c Substituting the numerical values of m, t, g and v gives 5 f(c)= 50􏰄1−e−9c􏰅−10. v(t) 2.1. FINDING ROOTS OF EQUATIONS 13 10 8 6 4 2 0 −2 −4 −6 −8 −10 0 1 2 3 4 5 6 7 8 9 10 c Figure 2.2: Figure showing the function f(c) defined by Eq. (2.4) • Bracketing methods • Open methods All these methods have their advantages and disadvantages. 2.1.1 Graphical Method One crude way of finding roots for an equation is to just plot the graph and visually inspect where the function evaluates to zero. For example, plot f(c) and find the value of c where f(c) = 0. From Fig. 2.2, the root at f(c) occur when c is approx- imately 5. The exact solution is c = 4.99382. As you can probably imagine, this method is not very practical because the accuracy of the solution is very poor. You are just relying on how accurate your eyes can read the graph ! Furthermore, it is very inefficient if you ever need to solve f(c) = 0 for different values of m, g and t. However, the graphical method have a few advantages that must be stated. By drawing a graph of the function, you can see exactly how the function behaves. It should also be clear how many roots the equation can have. The graphical method is also often used to obtain the initial guess for the root of the equation for more f(c) 14 CHAPTER 2. ROOT FINDING accurate methods such as the Bracketing and Open methods (see below). There are various Bracketing and Open methods available and they are shown in Fig. 2.3. Numerical methods for finding roots of equations Bracketing methods Bisection Method of method false postion One point iteration Open methods Secant method Newton- Raphson method 2.2 Figure 2.3: Classification of root finding methods Bracketing methods There are two different Bracketing methods. They are • Bisection Method • Method of false position These methods are known as bracketing methods because they rely on having two initial guesses, xl, which is the lower bound and xu, which is the upper bound. xl and xu must bracket (be either side of) the root. This requirement can give rise to various situations which is illustrated in Fig. 2.4. • If f(xu) and f(xl) are of different sign, then there must be at least one root, xr, in between xu and xl. i.e. xl < xr < xu. This is shown in Fig. 2.4 (a). 2.2. BRACKETING METHODS 15 • If f(xu) and f(xl) are of different sign, there might be an odd number of roots (see Fig. 2.4 (b)). • Figure 2.4 (c) shows that if f(xu) and f(xl) are of the same sign, then there might not be any root in between xu and xl. There are, however, several exceptions to the rules above. When the function is tangential to the x-axis, multiple roots occur (see Fig. 2.5 (a)). As is shown in Fig. 2.5 (b), in regions in the vicinity of discontinuities, there might also be excetions to the rules above. 2.2.1 The Bisection Method To find roots of an equation using the Bisection method, follow the steps below 1. Obtain two guesses for the root, xl and xu, that “brackets” the real root, xr. These two guesses can be found by plotting a graph of f(x) and see where it approximately crosses the x axis. In general, if f(xu)f(xl) < 0 then you can be reasonably sure that you have at least one root in between xl and xu. 2. Estimate the root of f(x) to be x ′r = x l + x u 2 3. Make a choice: • If f(x′r) = 0, then x′r is the root i.e. x′r = xr. Stop. There is nothing more to do. • If f(xl)f(x′r) < 0, then the root is in the lower subinterval. Therefore set xu =xr andgobacktoSTEP2. • If f(xu)f(x′r) < 0, then the root is in the upper subinterval. Therefore set xl =xr andgobacktoSTEP2 You usually cannot get exactly f(xr) = 0. In practice, if |f(xl)f(xr)| < ε, where ε is a small value (the stopping criterion), you would take xr to be the root. 16 CHAPTER 2. ROOT FINDING f(x) (a) f(x) (b) f(x) x xr x uu xl xr xxl xrxr x (c) f(x) (d) xr xr xu x xl Figure 2.4: Illustration of various situations that could occur when one brackets the roots. xl xu x Exercise 2.2: Use the Bisection method to obtain the root of the following equation 5 f(c)= 50􏰄1−e−9c􏰅−10. c 2.2. BRACKETING METHODS 17 f(x) (a) xlxr f(x) (b) Multiple roots occur here xu xl x xr xr Figure 2.5: Illustration of the exception to the rules of the bracketing methods. xux The matlab file for solving this exercise is given in Matlab Program 2.1. You might like to check your answer with the output from this Matlab Program. 2.2.2 Method of False Position The method of false position is based on the following observation. If f(xl) is closer to zero than f(xu), then the root is likely to be closer to xl than xu. See Fig. 2.6. Using similar triangles, we can obtain the following expression −f(xl) = f(xu) (2.5) xr − xl xu − xr 18 CHAPTER 2. ROOT FINDING Matlab Program 2.1 A matlab program illustrating the Bisection method. %%%%%%%%%%%%%%%%%%%%%%%%%% % This file can be found in CM/MATLAB/RootFinding/Bisection.m % Written by Andrew Ooi 3/6/2003 %%%%%%%%%%%%%%%%%%%%%%%%%%%% %clearing all variables clear all; EPS=1.0e-6;% EPS, small value which will determine the accuracy of your answer maxiter=50; %maximum number of iterations c_l=3.0; %Initial lower guess of root c_u=9.0; %Initial upper guess of root for i=1:maxiter, c_r=(c_l+c_u)/2.; %Guess that the real root is in between c_l and c_u f_l=(50/c_l)*(1-exp(-(9*c_l/5)))-10; f_u=(50/c_u)*(1-exp(-(9*c_u/5)))-10; f_r=(50/c_r)*(1-exp(-(9*c_r/5)))-10; % Storing results in a matrix Results(i,1)=i;Results(i,2)=c_r;Results(i,3)=f_r; % testing to see if we have found the root % If the root is found then exit the for loop if abs(f_r) < EPS break; end %Algorithm to find out whether the upper or lower guess % is closer to the actual root if (f_r*f_l)<0 c_u=c_r; elseif (f_r*f_u)<0 c_l=c_r; else break; end; end; % Displaying results disp(’ iter c_r disp(Results); f(c_r)’); 2.2. BRACKETING METHODS 19 f(x) xl f(xl) xr f(xu) x Figure 2.6: Figure illustrating the method of false position. xu or Exercise 2.3: Show that Eq. (2.5) reduces to xr = xuf(xl) − xlf(xu) f(xl) − f(xu) xr = xu − f(xu)(xl − xu) f(xl) − f(xu) (2.6) So, to use the method of False Position to find roots, use Steps 1-3 of the Bisection method but replace Step 2 with the formula given in Eq. (2.6). Exercise 2.4: Use the method of false position to obtain the root of 5 f(c)= 50􏰄1−e−9c􏰅−10 c You can check your answer by modifying Matlab Program 2.1 to use the method of false position to find the roots of f(c). Compare with your hand 20 CHAPTER 2. ROOT FINDING written solution. Figure 2.7 shows the solution of solving Eq. (2.4) using both Bisection and method of false position with initial lower and upper guess cl = 3 and cu = 9 respective. It can be seen that there is a monotonic convergence to the exact solution using the method of False position. On the other hand the Bisection method oscillates towards the exact answer. It is also clear that the method of False position converges slightly faster to the exact answer. 9 8 7 6 5 4 1 2 3 4 5 6 7 8 9 10 iter Exact solution Bisection method Method of false position Figure 2.7: Figure illustrating the differences between the Bisection method and the method of false position. 2.3 Open methods The open methods methods which require either only one initial guess or two initial guesses which do not have to bracket the actual root. It follows that it is easier to obtain the initial guess for open methods (you can choose almost anything !). In 2.3. OPEN METHODS 21 general, if they open methods converges, they have a tendency to do so much more rapidly than bracketing methods. However, unlike bracketing methods which always converge (i.e. the root for a particular equation is found), the open methods of root finding MAY NOT converge. The open methods that will be discussed in this course are • Fixed-point iteration • Newton-Raphson method • Secant method 2.3.1 Fixed (One) Point Iteration To find the root of f(x) = 0, arrange the function so that x is on the left hand side of the equation. i.e. x = g(x). We will rewrite the above equation as xi+1 = g(xi). (2.7) (2.8) Eq. (2.8) above provides us with a iterative formula to find the roots of Eq. (2.7). Note that the functional form of Eq. (2.7) is not changed. Eq. (2.8) is just a rearrangement of Eq. (2.7). Suppose we want to find the root to f(x)=e−x −x=0. (2.9) We will rearrange it to get x = e−x I.e. g(x) = e−x and h(x) = x. Hence, the formula for one point iteration scheme for solving Eq. (2.9) is xi+1 = e−xi 22 CHAPTER 2. ROOT FINDING Exercise 2.5: Use one point iteration to obtain the root of 5 f(c)= 50􏰄1−e−9c􏰅−10 c The convergence of fixed point iteration The iterative equation for fixed-point iteration is xi+1 = g(xi) (2.10) Since f(xr) = 0 and Eq. (2.10) is obtained by just rearraging f(x), xr must satisfy xr = g(xr). Subtracting Eq. (2.11) from (2.10) gives xi+1 − xr = g(xi) − g(xr). The above equation can be re-written as 􏰆g(xi) − g(xr)􏰇 xi+1 −xr = (xi −xr) (xi −xr) The derivative mean value theorem states that dg (x = ξ) = g(b) − g(a) dx b−a (2.11) (2.12) where ξ is somewhere in between a and b. In plain English, derivative mean value theorem say that there is a point (x = ξ) of a function (g) in between a and b such the derivative of that function is given by g(b) − g(a)/(b − a). If we apply the derivative mean value theorem to the bracketed term in Eq. (2.12), we get dg (ξ) = g(xr) − g(xi) (2.13) dx xr − xi where ξ is somewhere in between xr and xi. Substituting Eq. (2.13) into Eq. (2.12), we get 􏰆dg 􏰇 xi+1−xr= dx(ξ) (xi−xr) 2.3. OPEN METHODS 23 The above equation says that the newer guess will be closer to the actual root than the older guess providing that |dg(ξ)/dx| < 1 . So this is one condition for convergence. Note that it does not mean that if you have |dg(ξ)/dx| > 1, the solution process will blow up. Use this condition only as a good guide. Sometimes you can still get a converged solution if you start have |dg(ξ)/dx| > 1. It really depends on the problem you are trying to solve.
Exercise 2.6: Find the root for the following equation f(x)=ex −10x
by using the fixed point iteration. Try to find the solution with g(x) = ex/10 and x0 = 1.0 and x0 = 4.0 as your initial guesses. Which one of your solutions blowup? Why? Canyougetasolutionwithx0 =3.5?
Exercise 2.7: Let
f(x)=x2 −2x−3=0 (a) Find the two roots for f(x) analytically.
(b) Show that f(x) can be rearranged into the following forms √
x=g1(x)= 2x+3 x=g2(x)= 3
2
(c) With these three expressions for gi(x), use the fixed point iteration method to find the approximate root for f(x). Start with the initial guess of x0 = 4. Compare your solutions to the answer you get in part a).
(d) Sketch all the gi(x) and show what is happening graphically.
x = g3(x) =
x−2 (x2 −3)

24 CHAPTER 2. ROOT FINDING 2.3.2 Newton Raphson Method
The Newton Raphson method is the most widely used root finding method. It converges quickly and only need one initial guess.
From fig. 2.8, it is easy to express xi in terms of f(xi), f′(xi) and xi+1. f(x)
Slope df(xi)/dx
xi+1
Figure 2.8: Figure illustrating the Newton Raphson method
f′(xi) = f(xi) xi − xi+1
xi −xi+1 = f(xi) f′(xi)
which gives the following iterative formula for the Newton Raphson method xi+1 = xi − f(xi)
xi x
(2.14)
1. Let the initial guess be xi
2. Find xi+1 by using Eq. (2.14)
3. Let xi = xi+1 repeat steps 2 and 3 until you feel your answer is accurate enough.
f′(xi)
So to find a root using Newton-Raphson method, do the following:

2.3. OPEN METHODS 25
Exercise 2.8: Use Newton Raphson method to find the root of 5
f(c)= 50􏰄1−e−9c􏰅−10 c
You should be able to check your answer by modifying Matlab program 2.1 or 2.2.
Exercise 2.9:
Use Newton Raphson method to find the root of
f (x) = cos x − x
The matlab file showing how one can use Newton Raphson method for solving Ex. 2.9 is given in Matlab Program 2.2. You can check your hand written solution with the output from this matlab program.
Exercise 2.10: Let
f(x)=x2 −2x−3=0 (a) Find the two roots for f(x) analytically.
(b) Find the roots of f(x) using the Newton-Raphson method. Start with x0=4 and x0 = −2
(c) Sketch f(x) and show the convergence graphically.
One of the reasons why Newton Raphsons method is so popular is that it con- verges very fast. Proof:-
The Taylor series expansion of a function f(x) can be written as
f(xi+1) = f(xi) + f′(xi)(xi+1 − xi) + f′′(ξ)(xi+1 − xi)2 2!
where ξ lies somewhere in the interval from xi to xi+1. If we let xi+1 = xr, then we will have f(xi+1) = f(xr) = 0. Put this information into the above equation to get

26 CHAPTER 2. ROOT FINDING
Matlab Program 2.2 A matlab program illustrating the Newton Raphson method. %%%%%%%%%%%%%%%%%%%%%%%%%%
% This file can be found in CM/MATLAB/RootFinding/NewtonRaphson.m
% Written by Andrew Ooi 3/6/2003
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;%clearing all variables
EPS=1.0e-8;% EPS, small value which determine the accuracy of your answer
maxiter=50; %maximum number of iterations
xi=0.2; %Initial lower guess of root
fi=cos(xi)-xi;
for i=1:maxiter,
% Storing results in a matrix
Results(i,1)=i;Results(i,2)=xi;Results(i,3)=fi;
fi=cos(xi)-xi;
dfi=-sin(xi)-1;
xi=xi-fi/dfi;
% testing to see if we have found the root
% If the root is found then exit the for loop
if abs(fi) < EPS break; end end; % Displaying results disp(’ iter x_i disp(Results); f(x_i)’); 2.3. OPEN METHODS 27 0 = f(xi) + f′(xi)(xr − xi) + f′′(ξ)(xr − xi)2 2! We can also rearrange Eq. (2.14) to get 0=f(xi)+f′(xi)(xi+1 −xi). Subtracting Eq. (2.16) from Eq. (2.15) will give 0 = f′(xi)(xr − xi+1) + f′′(ξ)(xr − xi)2. 2! One can then rearrange the above equation to get xr − xi+1 = − f′′(ξ) (xr − xi)2 (2.15) (2.16) (2.17) (2.18) thus 2f′(xi) 􏰃􏰃 f′′(ξ) 􏰃􏰃 2 |xr −xi+1|=􏰃 􏰃(xr −xi) 􏰃2f′(xi)􏰃 According to Eq. (2.18), the magnitude of the error in Newton Raphson method is roughly proportional to the square of the previous error. Therefore we have quadratic convergence. Note that quadratic convergence of the Newton Raphson method can only be achieved if f′(xr) ̸= 0. If f′(xr) = 0, then another method has to be introduced in order to achieve quadratic convergence. Exercise 2.11: The formula for Newton-Raphson method is given by xi+1 = xi − f(xi) (2.19) f′(xi) which can be used to find the root, xr, of a function, f(x). It has been been shown in lectures that the Newton-Raphson method has quadratic convergence providing that f′(xr) ̸= 0. In cases where f′(xr) = 0 one will not be able to achieve quadratic convergence with Eq. (2.19). In order to achieve quadratic convergence for cases where f′(xr) = 0, we have to define another function, μ(x), where μ(x)= f(x). f′(x) 28 CHAPTER 2. ROOT FINDING (a) Assume that f(x) could be written as f(x) = (x − xr)2q(x), (2.20) where q(xr) ̸= 0 (i) Show that f(xr) = f′(xr) = 0 (ii) Show that μ(xr) = 0 but μ′(xr) ̸= 0. (iii) The results above show that finding the root of f(x) is equivalent to finding the root of μ(x). Hence, show that a modified version of the Newton-Raphson method that can be used in order to achieve quadratic convergence is xi+1 = xi − f(xi)f′(xi) . [f′(xi)]2 − f(xi)f′′(xi) (iv) Use Eqs. (2.19) and (2.21) to obtain the root of f (x) = e(x−1) − x. (2.21) (2.22) You might like to plot the function to see how it looks like. Start with an initial guess of x0 = 5. Which of the two methods give you faster convergence ? The exact answer is xr = 1. (b) Solve using both Eq. (2.19) and Eq. (2.21). Start with an initial guess of x0 = 5. (c) Compare the convergence and discuss the advantages and disadvantages of each schemes. x3 +4x−9=0 (2.23) Several pitfalls of Newton Raphson method • Requires the evaluation of the derivative of a function. Sometimes, this is difficult or inconvenient to do. • Have lots of problems in the vicinity of local maxima or minima because 2.3. OPEN METHODS 29 f′(xi) = 0 if xi is at a local maxima or minima. This is illustrated in Fig. 2.9. f(x) xi xi+1 x Figure 2.9: Figure illustrating the pitfalls of Newton Raphson method. 2.3.3 Secant Method One of the main reasons in introducing the secant method is that in the Newton Raphson method, you have to analytically calculate the derivative f′(xi). Sometimes, this is very inconvenient. In the secant method, the derivative of a function is approximated by a backward finite divided difference f′(xi) ≈ f(xi) − f(xi−1) xi − xi−1 Put the above equation into Eq. (2.14) gives the following formula for the Secant method xi+1 =xi − (xi −xi−1)f(xi) (2.24) (f(xi) − f(xi−1)) Notice that Eq. (2.24 requires two initial estimates of x, i.e. xi and xi−1. However, because the function f(x) is not require to change signs between the estimates, it is not classified as a bracketing method. 30 CHAPTER 2. ROOT FINDING False position xl f(x) Secant xi-1 f(x) f(x) f(x) xr' xu x xr' xi x xr' lxux xixi-1x Figure 2.10: Figure illustrating the difference between the Secant method (which is classified as an open method) and the method of false position (which is classified as bracketing method). 2.4 System of nonlinear equations So far, we have only talked about methods of finding roots for one equation. What if we need to simultaneously find roots for two or more equations ? For example, what if you to find a set of x and y values so that they satisfy the following two equations. y = x2 + 1 (2.25) y = 3 cos(x) (2.26) These two equations are illustrated in Fig. 2.11. In general, what we would like to do is to find all the xi’s so that we satisfy all of the following set of equations. x xr' 2.4. SYSTEM OF NONLINEAR EQUATIONS 31 5 4 3 2 1 0 −1 −20 0.5 1 1.5 2 2.5 3 x y=x2+1 Solution to the system of equations y=3cos(x) Figure 2.11: Figure showing Eqs. (2.25) and (2.26). The point of intersection is the solution we are after. f1(x1, x2, ......., xn) = 0 f2(x1, x2, ......., xn) = 0 f3(x1, x2, ......., xn) = 0 . . . fn(x1, x2, ......., xn) = 0 For the purpose of this course, let’s just confine ourselves to n = 2. So we wish to find x1 = x and x2 = y so that the following two equations are satisfied u(x, y) = 0 v(x, y) = 0 32 CHAPTER 2. ROOT FINDING The multivariable Taylor series for u and v can be written as ui+1 = ui + (xi+1 − xi) ∂u(xi, yi) + (yi+1 − yi) ∂u(xi, yi) ∂x ∂y vi+1 =vi +(xi+1 −xi)∂v(xi,yi)+(yi+1 −yi)∂v(xi,yi) ∂x ∂y For simplicity, the following notation will be used ∂u 􏰆∂u􏰇 ∂x(xi,yi)≡ ∂x i So the above two equations can be re-written as ui+1 =ui +(xi+1 −xi) vi+1 =vi +(xi+1 −xi) which can be simplified to be 􏰆∂u􏰇 􏰆∂u􏰇 ∂x +(yi+1 −yi) ∂y ii 􏰆∂v􏰇 􏰆∂v􏰇 ∂x +(yi+1 −yi) ∂y ii 􏰆∂u􏰇 􏰆∂u􏰇 iiii 􏰆∂v􏰇 􏰆∂v􏰇 􏰆∂v􏰇 􏰆∂v􏰇 ∂x xi+1 + ∂y yi+1 =−vi +xi ∂x +yi ∂y iiii 􏰆∂u􏰇 􏰆∂u􏰇 ∂x xi+1 + ∂y yi+1 =−ui +xi ∂x +yi ∂y (2.27) (2.28) if we set ui+1 = vi+1 = 0. The above two equations are a set of two linear equations. They can easily be solved. Exercise 2.12: Solve Eqs. (2.27) and (2.28) for xi+1 and yi+1 and show that v 􏰄∂u􏰅 −u 􏰄∂v􏰅 i ∂y i i ∂y i xi+1 =xi +􏰁∂u􏰂 􏰄∂v􏰅 −􏰄∂u􏰅 􏰁∂v􏰂 ∂x i ∂y i ∂y i ∂x i ui􏰁∂v􏰂 −vi􏰁∂u􏰂 ∂x i ∂x i yi+1 =yi +􏰁∂u􏰂 􏰄∂v􏰅 −􏰄∂u􏰅 􏰁∂v􏰂 ∂x i ∂y i ∂y i ∂x i 2.4. SYSTEM OF NONLINEAR EQUATIONS 33 Exercise 2.13: Provide a simultaneous solution to the two equations out- lined below using the two equation Newton-Raphson method y = x2 + 1 y = 3 cos(x) 34 CHAPTER 2. ROOT FINDING Chapter 3 Systems of linear algebraic equations In many problems of engineering interest, you will need to be able to solve a set of linear algebraic equations. The methods can be broadly classified as shown in Fig. 3.1. Solution of linear algebraic equations Direct methods Figure 3.1: Classification of different methods for solving a system of algebraic equa- tions Consider the following set of equations 35 Iterative methods Gauss-Elimination Gauss-Jordan LU Decomposition Gauss-Seidel Gauss-Jacobi 36 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS a11x1 + a12x2 + a13x3 + ...... + a1nxn = c1 a21x1 + a22x2 + a23x3 + ...... + a2nxn = c2 . . . . an1x1 + an2x2 + an3x3 + ...... + annxn = cn where all the a’s and c’s are constants. We need to find all the xi’s such that all the above equations are satisfied. Sometimes, the matrix notation will be used where and   .  x n   c 1   a11  a21 [A] =  a31 a12 a22 a32 .. a1n  .. a2n  .. a3n , . . .  [A]{X}={C} (3.1)  . . an1 an2 . . ann  x 1      x2  {X} = x3     c2  {C}= c3 .   .  c n  Equation (3.5) can be solved by just inverting the matrix [A] and then multiply [C] by [A]−1. {X} = [A]−1 {C} But this might be difficult and time consuming, sometimes impossible to do if A is large. Can we numerically try to solve it ? 3.1. DIRECT METHODS 37 3.1 Direct Methods In these methods, no iterations are involved. 3.1.1 Gauss Elimination If you want to use the Gauss elimination procedure to solve the following system of linear equations, you have to: a11x1 + a12x2 + a13x3 + ...... + a1nxn = c1 = a1,n+1 a21x1 + a22x2 + a23x3 + ...... + a2nxn = c2 = a2,n+1 . . . . an1x1 + an2x2 + an3x3 + ...... + annxn = cn = an,n+1 • first augment [A] with {C}. We have to next do forward elimination to get the matrix into a form that is easily solvable.  a(1) 11  a(1) 21 (1)  a31 . . . . . . a(1) n1 a(1) a(1) 12 13 a(1) a(1) 22 23 (1) (1) a32 a33 . . . . . . . . . . . . a(1) a(1) n2 n3 ....a(1) 1n ....a(1) 2n | a(1)  1,n+1 | a(1)  2,n+1 ....a3n ..... ..... ..... ..... ..... ..... | a3,n+1  | .  | .  | .  | .  | .  | .  ....a(1) |a(1) • Eliminate x1 from the second and subsequent equations. For example, to elim- inate x from the ith row, multiply row 1 by a(1)/a(1) and subtract from row 1 i1 11 i. (1) (1) nn n,n+1 38 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS where a(2) = a(1) ij ij a(2) = 0 ij and j= 1, 2, 3.., ,n+1 (2) (1) 􏰆a(1)􏰇 (1) i1 a for j= 2, 3,....n+1 a=a ij ij a(1) 1j 11  a(1) 11  a(1) 21 (1)  a31 . . . . . . a(1) n1  a(2) 11 0 0 . . . . . . 0 for for − a(1) 12 a(1) 22 (1) a32 . . . . . . a(1) n2 a(2) 12 a(2) 22 (2) a32 . . . . . . a(2) n2 i= 1 a(1) ....a(1) 13 1n a(1) ....a(1) 23 2n | a(1)  1,n+1 | a(1)  2,n+1 (1) (1) (1) a33 ....a3n . ..... . ..... . ..... . ..... . ..... | a3,n+1  | .  | .  | .  | .  | .  | .  . ..... a(1) ....a(1) |a(1) n3 nn ↓ a(2) ....a(2) 13 1n a(2) ....a(2) 23 2n n,n+1 | a(2)  1,n+1 | a(2)  2,n+1 (2) (2) (2) a33 ....a3n . ..... . ..... . ..... . ..... . ..... | a3,n+1  | .  | .  | .  | .  | .  | .  . ..... a(2) ....a(2) |a(2) n3 nn n,n+1 i= 2,3,...,n and j= 1 i= 2, 3,...,n and • Next, eliminate x2 from the third and subsequent equations. For example, to eliminate x from the ith row, multiply row 2 by a(2)/a(2) and subtract from 2 i2 22 row i. Hence, 3.1. DIRECT METHODS 39 where • If you keep repeating something like the steps above, you will end up with a matrix that look  a(2) 11 a(2) a(2) ....a(2) 12 13 1n a(2) a(2) ....a(2) 22 23 2n | a(2) 1,n+1 | a(2) 2,n+1         0 0 . . . . . . 0          a(3)  1,n+1 a(3)  2,n+1 (3) a3,n+1  .  .  .  .  .   a(3) 11 . . ..... a(2) a(2) ....a(2) |a(2) n2 n3 nn ↓ a(3) a(3) ....a(3) 12 13 1n a(3) a(3) ....a(3) 22 23 2n         0 0 . . . . . . 0 0 . . . . . . 0 (3) (3) a33 ....a3n . ..... . ..... . ..... . ..... . ..... . ..... a(2) ....a(2) a(3) = a(2) ij ij a(3) = 0 ij | | | | | | | | (2) (2) (2) (2) a32 a33 ....a3n . . ..... . . ..... . . ..... . . ..... . . ..... | a3,n+1 | . | . | . | . | . n3 nn 􏰌a(2)􏰍 a(3) = a(2) − i2 a(2) ij ij a(2) 2j 22 | . n,n+1 .  | a(2) n,n+1 | 40 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS where a(n)a(n)a(n)a(n)...a(n)|a(n)  11 12 13 14 1n 1,n+1  0 a(n) a(n) a(n) ...a(n) |a(n)   22 23 24 2n 2,n+1   0 0 a(n) a(n) ...a(n) |a(n)  33 34 3n 3,n+1  0 0 0 a(n) ...a(n) |a(n)  44 4n 4,n+1 ........|. (3.2)   ........|. .. . .. . .. . 00 0 .....| .....| .....| . ...a(n) | nn a(k) = a(k−1) ij ij a(k) = 0 ij .  .  .  a(n) n,n+1 􏰌 a(k−1) 􏰍 a(k) = a(k−1) − i,k−1 ij ij a(k−1) k−1,k−1 a(k−1) k−1,j We can solve the system of equations given by Eq. (3.2) by using back substitution. First look at the last row of Eq. (3.2) a(n)x = a(n) nn n n,n+1 a(n) nn a(n) x +a(n) x =a(n) n−1,n−1 n−1 n−1,n n n−1,n+1 a(n) −a(n) x xn−1 = n−1,n+1 n−1,n−1 n (3.4) a(n) n−1,n−1 Since we already know xn from Eq. (3.3), we can also get a numerical value for xn−1 from Eq. (3.4). Similarly, looking at the 3rd last row of Eq. (3.2) a(n) xn = n,n+1 (3.3) So we can get a numerical value for xn. Next, look at the 2nd last row of Eq. (3.2) 3.1. DIRECT METHODS 41 a(n) x n−2,n−2 n−2 + a(n) x n−2,n−1 n−1 + a(n) x n−2,n n = a(n) n−2,n+1 a(n) −􏰄a(n) x +a(n) a(n) n−2,n−2 x 􏰅 n−2,n+1 n−2,n n n−2,n−1 n−1 xn−2 = In general, the formula for getting the numerical value for xi is and a(n) xn = n,n+1 a(n) nn n − 􏰎 a(n)xj ij j =i+1 a(n) ii xi = a(n) i,n+1 for i = n − 1, n − 2, n − 3, .., 2, 1. The pitfalls of the Gauss elimination method for solving a linear system of equa- tions are: • It is very expensive, compared to other methods that are currently available. • The formula for the forward elimination stage requires that we divide by the diagonal components of the matrix A. So, we have to always rearrange our equations so to ensure that we are not dividing by zero. For example the following system 2x2+3x3 =1 3x1+5x2+6x3 = 2 9x1+2x2+3x3 = 3 For this case, a11 = 0, so you will run into problems in the first forward elimination stage. If you re-write the above equation as 3x1+5x2+6x3 = 2 2x2+3x3 =1 9x1+2x2+3x3 = 3 42 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS will eliminate the problem. The process of interchanging rows so that you are not dividing by zero is called partial pivoting. In general, if the diagonal elements are very small, you would like to pivot the rows so that you are not dividing by a small number. Exercise 3.1: Solve the following system of 4 equations using Gauss Elimi- nation x1 +2x2 +3x3 +4x4 3x1 +4x2 +8x3 +9x4 10x1 +12x2 +4x3 +3x4 5x1 +6x2 +7x3 +8x4 = 3 = 4 = 8 = 10 3.1.2 LU Decomposition Suppose we want to solve the following linear equation [A] {X} = {C} . If we can somehow re-express [A] such that (3.5) [A] = [L][U] where [L] and [U] are lower- and upper-triangular matrices. The structure of [L] and [U] are shown below  . [L]= . . . . . . .  l11 0 0 0 l21l2200 0 0  0  l31  . 0  .  .  . .  .  .   .  .  . l32 l33 . . . . . . . . . . . . ln1 ln2 ln3 ln4 . . . . . ..... ..... . . . . . . . . . . ..... . . . . . . . . . . . . . . . .....lnn (3.6) 3.1. DIRECT METHODS 43 1u12 u12 u12 .....u1n 0 1 u23 u24 .....u2n 0 0 1 u34 .....u3n  . . . . . . . . . .   . . . . . . . . . .  [U]=.. . .......  . . . . . . . . . .   . . . . . . . . . .   . . . . . . . . . .  0000.....1 If we now substitute Eq. (3.6) into Eq. (3.5), then we will have [L] [U] {X} = {C} Let [U]{X} = {R} and substitute Eq. (3.8) into Eq. (3.7) to give [L] {R} = {C} (3.7) (3.8) (3.9) Thus we have replaced the original problem, Eq. (3.5), by two problems given by Eqs. (3.8) and (3.9). However, Eqs. (3.8) and (3.9) are much easier to solve because [L] and [U] are lower- and upper-triangular matrices respectively. This means that to solve Eqs. (3.8) and (3.9), all you have to do is do backward and forward substitution respectively. A strategy of solving Eq. (3.5) using Eqs. (3.8) and (3.9) is the following • Decompose [A] into [L] and [U] • Solve Eq. (3.9) and obtain {R} by forward subtitution. • Use the {R} obtain in the step above and substitute into Eq. (3.8) so that you can solve for {X} by backward substitution. The flow of information in the LU decomposition method is shown in Fig. 3.2. The problem now is how do we find [L] and [R] from [A] ? To illustrate the procedure of finding [L] and [R], we will look at how it can be done for a small 4 × 4 [A] matrix. 44 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS 1 decomposition [A]{X}={C} [L] [U] [L]{R}={C} {R} [U]{X}={R} {X} 2 forward substitution 3 backward substitution Figure 3.2: The flow of information in the LU decomposition method. a11 a12 a13 a14 l11 0 0 01u12 u13 u14 a21 a22 a23 a24=l21 l22 0 00 1 u23 u24  a31 a32 a33 a34 l31 l32 l33 00 0 1 u34 a41 a42 a43 a44 l41 l42 l43 l44 0 0 0 1 If we multiply the rows of [L] by the first column of [U] and compare it with [A], we get l11 =a11, l21 =a21, l31 =a31, l41 =a41. So the first column of [L] is the first column of [A] Multiply the first row of [L] by the columns of [U] to get } } 3.1. DIRECT METHODS 45 l11u12 = a12, l11u13 = a13, l11u14 = a14. Hence, the first row of [U] can be determined to be u12 = a12,u13 = a13,u14 = a14 (3.10) l11 l11 l11 We keep alternating between getting a column of [L] and a row of [U]. So we next get the equation for the second column of [L] by multiplying the rows of [L] by the second column of [U]. l21u12 + l22 = a22 l31u12 + l32 = a32 l41u12 + l42 = a42 This gives the 2nd column of [L] to be second row of [L] by the columns of [U] to get l21u13 + l22u23 = a23 l21u14 + l22u24 = a24 This allows us to obtain the 2nd row of [U] to be l22 = a22 − l21u12 l32 = a32 − l31u12 l42 = a42 − l41u12 (3.11) Since we already know u12, l21, l31 and l41 from Eqs. (3.9) and (3.13). Multiply the u23 = a23−l21u13 l22 u24 = a23−l21u14 l22 Since Eqs. (3.9), (3.13) and (3.14) will give you l21, u13, u14 and l22. If you keep going you can get l33 = a33 − l31u13 − l32u23, l43 = a43 − l41u13 − l42u23 u34 = a34−l31u14−l32u24 l33 l44 = a44 − l41u14 − l42u24 − l43u34 In general, the formula for getting the elements of [L] and [U] from the elements an n × n [A] matrix can be written as 46 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS li1 = ai1 for i = 1,2,...,n u1j = a1j l11 for j = 2,3,...,n Begin lij=aij−􏰎likukj j = 2,3,...,n for i=j,j+1,...,n for loop j−1 k=1 j−1 ajk− 􏰎 ljiuik ujk = i=1 for ljj k=j+1,j+2,...,n End for loop uii = 1.0 for i = 1,2,...,n (3.12) The LU decomposition algorithm outlined in Eq. (3.12) is also known as the Crout’s LU decomposition algorithm. Matlab implementation of Crout’s method is shown in Matlab Program 3.1 After obtaining the [L] and the [U] matrix, we need to solve for {R} [L]{R} = {C}   l11 0 0 0 r1 c1 l21 l22 0 0 r2  c2    = (3.13) l31 l32 l33 0r3 c3 l41 l42 l43 l44 r4  c4  by forward substitution. From the first row, From the 2nd row, l11r1 = c1 r1 = c1 l11 l21r1 + l22r2 = c2 r2 = c2 − l21r1 l22 (3.14) we can get a numerical value for r2 because we know r1 (from Eq. (3.14)). From the third and fourth rows of Eq. (3.13) we can get 3.1. DIRECT METHODS 47 Matlab Program 3.1 A matlab program to perform LU decomposition using Crout’s method. clear all; A=[1234 8; 3 4 8 9 9; 10 12 4 3 1 ; 5 6 7 8 2; 1 2 5 7 9]; [m,n]=size(A); L=zeros(m,n); U=zeros(m,n); for i=1:n L(i,1)=A(i,1); end; for j=2:n U(1,j)=A(1,j)/L(1,1); end; for j=2:n for i=j:n sum=0.0; for k=1:j-1 sum=sum+L(i,k)*U(k,j); end; L(i,j)=A(i,j)-sum; end; for k=j+1:n sum=0.0; for i=1:j-1 sum=sum+L(j,i)*U(i,k); end; U(j,k)=(A(j,k)-sum)/L(j,j); end; end; for i=1:n U(i,i)=1.0; end; 48 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS r3 = c3−l31r1−l32r2 l33 r4 = c4−l41r1−l42r2−l43r3 l44 In general, the formula for ri for a n × n system is Lastly, we need to solve [U]{X} = {R}   (3.15) r1 = c1/l11 􏰌 i−1 􏰍 ci−􏰎 lijrj ri = j=1 for i=2,3,4,.....,n lii 1 u12 u13 u14  x1   r1  0 1 u23 u24 x2  r2  = 0 0 1 u34x3 r3 0 0 0 1  x 4   r 4  Use the method of back substitution which we was introduced in the method of Gauss elimination. Start with the last (fourth row) x4 = r4 (3.16) Next, use the 2nd last (third row) to give x3 + u34x4 = r3 x3 = r3 − u34x4 (3.17) You can get a value for x3 because we know x4 from Eq. (3.16). The second and first row will give x2 = r2 − u23x3 − u24x4 x1 =r1 −u12x2 −u13x3 −u14x4 which can be use to solve for x2 and x1 (in that sequence). In general, the formula for a n×n system is (3.18) xn = rn n xi =ri − 􏰎 uijxj for i=n−1,n−2,n−3,.....,2,1 j =i+1 3.1. DIRECT METHODS 49 Exercise 3.2: Solve the following system of 4 equations using LU Decom- position x1 +2x2 +3x3 +4x4 3x1 +4x2 +8x3 +9x4 10x1 +12x2 +4x3 +3x4 5x1 +6x2 +7x3 +8x4 = 3 = 4 = 8 = 10 In the example above, the LU decomposition was conducted using an [U] matrix with ones on the diagonal. This is called the Crout’s method. One can also factorize a matrix by assuming ones along the diagonal of the [L] matrix instead of the [U] matrix. This is called the Doolittle’s method, which for a 4×4 system, would look something like a11 a12 a13 a14 1 0 0 0u11 u12 u13 u14 a21 a22 a23 a24=l21 1 0 0 0 u22 u23 u24 a31 a32 a33 a34  l31 l32 1 0 0 0 u33 u34  a41 a42 a43 a44 l41 l42 l43 1 0 0 0 u44 The algorithm to perform Doolittle’s method is very similar to that to do Crout’s method. The Matlab program to perform Doolittle’s method is shown in Matlab Program 3.2 Exercise 3.3: Use the Doolittle’s method and find the [L] and [U] matrices for the following 3×3 matrix  1 1 −3   3 1 4   201 50 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS Matlab Program 3.2 Matlab program to perform LU decomposition using Doolit- tle’s method. clear all; A=[1 2 3 4; 3 4 8 9 ;10 12 4 3 ;5 6 7 8 ]; [m,n]=size(A); L=zeros(m,n); U=zeros(m,n); for j=1:n U(1,j)=A(1,j); L(j,j)=1.0; end; for i=2:n L(i,1)=A(i,1)/U(1,1); end; for i=2:n for j=i:n sum=0.0; for k=1:i-1 sum=sum+L(i,k)*U(k,j); end; U(i,j)=A(i,j)-sum; end; for m=i+1:n sum=0.0; for nn=1:i-1 sum=sum+L(m,nn)*U(nn,i); end; L(m,i)=(A(m,i)-sum)/U(i,i); end; end; 3.1. DIRECT METHODS 51 3.1.3 Using LU decomposition to find the inverse of a matrix Sometimes, it is useful to find the inverse of a matrix. LU decomposition can be used to find inverse of a matrix. The inverse of a nonsingular matrix [A], [A]−1 is defined as [A][A]−1 = [I] (3.19) where [I] is the identity matrix. I will demonstrate the method for a 4×4 system. The extension to a n × n system is straightforward. For a 4×4 system, Eq. (3.19) can be written as a11 a12 a13 a14b11 b12 b13 b14 1 0 0 0 a21 a22 a23 a24 b21 b22 b23 b24 =0 1 0 0 a31 a32 a33 a34 b31 b32 b33 b34  0 0 1 0 a41 a42 a43 a44 b41 b42 b43 b44 0001 Observe that one can break the above equations down into the following 4 equations   a11 a12 a13 a14 b11  1 a21 a22 a23 a24 b21  0   = (3.20) a31 a32 a33 a34 b31  0 a41 a42 a43 a44 b41  0   a11 a12 a13 a14 b12  0 a21 a22 a23 a24 b22  1   = (3.21) a31 a32 a33 a34 b32  0 a41 a42 a43 a44 b42  0   a11 a12 a13 a14 b13  0 a21 a22 a23 a24 b23  0   = (3.22) a31 a32 a33 a34 b33  1 a41 a42 a43 a44 b43  0   a11 a12 a13 a14 b14  0 a21 a22 a23 a24 b24  0   = (3.23) a31 a32 a33 a34 b34  0 a41 a42 a43 a44 b44  1 Thus solve Eqs. (3.20) - (3.23) will give you all the bij s which are the elements of the [A]−1 matrix. Note that this method is very efficient because the [A] matrix remain 52 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS the same. Hence one can use the same [L] and [U] matrices when solving Eqs. (3.20) - (3.23). Note that you do not need to use to use LU Decomposition to solve Eqs. (3.20) - (3.23). You can use any method you like. As long as you can solve Eqs. (3.20) - (3.23), you should be able to write down [A]−1. Example 3.1: Let’s try to use LU Decomposition to find the inverse of 1234  3 4 8 9  [A]=10 12 4 3  5678 Following the discussion above, we will need to define the following 4 equations 1234 b11 1  3 4 8 9  b21   0  = (3.24) 10 12 4 3b31  0  b41  0  b43  0 5678 1234 b12 0  3 4 8 9  b22   1  = (3.25) 10 12 4 3b32  0  b42  0 5678 1234 b13 0  3 4 8 9  b23   0  = (3.26) 10 12 4 3b33  1 5678 1234 b14 0  3 4 8 9  b24   0  = (3.27) 10 12 4 3b34  0  b44  1 5678 3.1. DIRECT METHODS 53 Using LU decomposition to solve Eq. (3.24)- (3.27) will give you  −10 9 b21=36  b31 −13    18  b41 5 9  −79   b12   b22   49  =   b11     37   b32   13     9  b42 −10  9  −13   b13   b23   13  b43  −1  3  14  9 b24= 36 b34 −25    18  b44 11 9 Thus the inverse of the original matrix can be written down as −10 −7 −1 14  9939 =  b33   1     3    b14   −41 37 4 1 41 −1  36 9 3 −36  [A]=−13 13 1 −25 18 9 3 18 5 −10 −1 11 9939 Exercise 3.4: Given that the values of a, b, c and d that satisfies 54 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS 1 1 0 2a    a     b  p−1      2 5 2 0b 3831 c = r−2     5 0 1 3  d   s − 1 are Use the information above to find  1 1 0 2 −1  2 5 2 0   3 8 3 1   5013 β=   2q+p−5/4r−9/4−1/4s  c   p−3+3q−2r =  41 r + 61 − 7/2 p − 15/2 q + 5/8 s  8 8   d  1/8 s + 17 − 1/2 p − 5/2 q + 13 r  q−2 88 3.2 Iterative Methods As the name implies, these methods require iterative procedure in order to obtain the solutions. Suppose you would like to solve a system that looks like [A] {x} = {C} (3.28) where the square brackets denote a matrix and the curly brackets denote a vector. Suppose that the matrix [A] is rewritten as [A] = [M] − [N] Substituting into Eq. (3.28) gives [M ] {x} = [N ] {x} + {C } (3.29) 3.2. ITERATIVE METHODS 55 An iterative method of solving for the vector {x} would be [M]{x}(k+1) =[N]{x}(k) +{C} (3.30) {x}(k) is the current (k’th) guess of the true solution, {x}. Naturally, one would like to choose the matrix [M] such that Eq. (3.30) is easier to solve than Eq. (3.28). In this course, we will cover two different methods, 1. If [M] consist of only the diagonal elements of [A], then we have the point Jacobi method. 2. If [M] consist of the lower triangular elements of [A], then we have the Gauss- Seidel method. So one can take the following steps when using iterative techniques to solve linear systems 1. Define the [M] and [N] matrices 2. Obtain an initial guess, {x}(0) 3. Solve Eq. (3.30) to obtain a new guess. 4. Define and obtain the maximum absolute value of the residual vector, {r}=[A]{x}(k) −{C} If the maximum absolute value of {r} is greater than a small specified value, then repeat step 3. Else, end. 3.2.1 Point Jacobi For this method, the matrix [M] consist of only the diagonal elements of [A]. The negative of all other elements is placed in [N]. This method will be illustrated with the 3 × 3 system below   a11 a12 a13 x1 c1  a21 a22 a23  x2 = c2 a31 a32 a33 x3 c3 56 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS [M] will contain the diagonal elements of [A] a11 0 0 [M]=0a22 0 0 0 a33 and the -ve of all other elements are placed in the matrix [N]  0 −a12 −a13  [N]=−a21 0 −a23  −a31 −a32 0 For this case, the general formula given in Eq. (3.30) will actually look like   (k+1)   (k)   0 −a12 −a13  x1   c1  =−a21 0−a23x2 +c2 −a31 −a32 0 x3 c3 Solving this system of equation will give you x(k+1) = 1 􏰄−a x(k) −a x(k) +c 􏰅 1 a1221331 11 x(k+1) = 1 􏰄−a x(k) −a x(k) +c 􏰅 2 a2112332 22 x(k+1) = 1 􏰄−a x(k) −a x(k) +c 􏰅 3 a3113223 33 It is straightforward to see that if [A] is an N × N matrix, then the generic formula for x(k+1) is  1N x(k+1) = 􏰐−aijx(k) +ci j ̸=i a11 0 0  x1  0a220x2 0 0 a33 x3 i 3.2.2 Gauss-Seidel i a j  ii j=1 This method is the most commonly used iterative method. For conciseness, I will illustrate the method with the 3 × 3 system below 3.2. ITERATIVE METHODS 57 a11x1 + a12x2 + a13x3 = c1 a21x1 + a22x2 + a23x3 = c2 (3.31) (3.32) a31x1 + a32x2 + a33x3 = c3 [M] will contain the diagonal elements of [A] a11 0 0 [M]=a21a22 0 a31 a32 a33 The negative of all other elements is placed in [N]. 0−a12 −a13 [N]=0 0 −a23  000 For this case, the general formula given in Eq. (3.30) will actually look like   (k+1)   (k)   (3.33) For this method, the matrix [M] consist of only the lower triangular elements of [A]. 0−a12 −a13 x1 c1 =0 0 −a23 x2 + c2 0 0 0 x3 c3 Solving this system in sequence from the top row to the bottom row will give you a11 0 0 x1 a21 a22 0 x2 c −a x(k) −a x(k) x(k+1) = 1 12 2 13 3 a31 a32 a33 x3 1 a11 c −a x(k+1) −a x(k) (3.34) (3.35) (3.36) 21 1 a22 x(k+1) − a 31 1 a33 We can now start the iterative process. WARNING, you have to be careful that none of your aii’s are zero !! x(k+1) = 2 2 − a 23 3 x(k+1) 32 2 c x(k+1) = 3 3 58 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS 1. 2. 3. Assume initial guess values for x , x , and x : x(0), x(0) and x(0). 123123 Substitute the guess values for x(0), and x(0) into Eq. (3.34) to obtain a better 23 estimate for x . Call this value x(1). 11 Substitute x(1) and x(0) into Eq. (3.35) to obtain a new estimate for x = x(1). 13 22 Use x(1) and x(1) in Eq. (3.36) to obtain a better estimate for x = x(1). 12 33 4. The flow of information for the Gauss Seidel method is shown in Fig. 3.3. x1 = c1 - a12x2 - a13x3 a11 x2 = c2 - a21x1 - a23x3 a22 x3 = c3 - a31x1 - a32x2 a33 x1 = c1 - a12x2 - a13x3 a11 x2 = c2 - a21x1 - a23x3 a22 x3 = c3 - a31x1 - a32x2 a33 First iteration Second iteration Figure 3.3: The flow of information in the Gauss Seidel method. Note that one can extend the point Jacobi and Gauss-Seidel methods to solve non- linear systems too ! Exercise 3.5: Use point Jacobi and the Gauss-seidel iterative method to solve the following linear system of equations 3.2. ITERATIVE METHODS 59 9x1 + 4x2 x1 −2x2 x1 + 6x2 Which method converges faster ? +x3 = −17 −6x3 =14 = 4 It is straightforward to see that if [A] is an N × N matrix, then the generic formula for x(k) using the Gauss-Seidel method 3.2.3 Convergence of iterative scheme Let’s now look at under what conditions will the approximated solution, {x}(k) con- verge to the exact solution {x}. Define the error matrix at the k’th iteration to be {ε}(k) = {x}(k) − {x} Substracting Eq. (3.29) from Eq. (3.30) gives {ε}(k+1) = [M]−1 [N]{ε}(k) {ε}(k+1) = [P ] {ε}(k) where [P] = [M]−1 [N] One should be able to deduce from this equation that the error at the k + 1’th iteration is related to the initial error {ε}(k+1) = [P ]k+1 {ε}(0) If we assume that the all eigenvalues of the matrix [P ] are linearly independent, then we can write i 1􏰌 ji

60 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS
Matlab Program 3.3 A matlab program containing the Gauss-Seidel method to solve the equations in Exercise 3.5
clear all
maxiter=1000;
A=[9 4 1; 1 6 0;
1 -2 -6];
C=[-17; 4;
14];
%%%%%%%%%%%%%%%%%%%%%%
%Initial Guess
%%%%%%%%%%%%%%%%%%%%%%%%
x=zeros(size(C));
[rows,columns]=size(A);
for k=1:maxiter
for i=1:rows
sum=0.0;
for j=1:columns
if j~=i
sum=sum-A(i,j)*x(j);
end
end
x(i)=(sum+C(i))/A(i,i);
end
residual=A*x-C
if max(abs(residual)) < 1.0e-8 fprintf(’Solution obtained !’); x break; end; end; 3.2. ITERATIVE METHODS 61 [P] = [S][Λ][S]−1 where the columns of [S] are the eigenvectors of [P] and [Λ] is a diagonal matrix whose elements are the eigenvalues of [P]. Thus we can write the error at the k’th iteration as {ε}(k+1) = [S] [Λ]k+1 [S]−1 {ε}(0) So in order for the error to go to zero as fast as possible, we would need that the magnitude of all the eigenvalues of [P ] less than 1. Exercise 3.6: Consider the system of equations in Exercise 3.5. Define the [M] and [N] matrices. Calculate the eigenvalues of [M]−1 [N] and show that they are consistent with your results in Exercise 3.5. Exercise 3.7: The equation [A] {x} = {C} (3.37) can be solved by iterative schemes which can be written as [M]{x}(k+1) =[N]{x}(k) +{C} (3.38) {x}(k) is the approximate value of the vector {x} at the k’th iteration. [A] = [M] − [N] are matrices {C} is a constant vector. 1. Consider a system of two equations where 􏰈a b􏰉 [A]=[M]−[N]= c d What is the condition among the elements of [A] for the iterative scheme to converge if 􏰈a 0􏰉 [M]= c d (3.39) 62 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS 2. Solve 􏰈17􏰉􏰊x1􏰋 􏰊4􏰋 23x=5 (3.40) 2 with the iterative scheme and the structure of matrix [M] given by Eq. (3.39). Start with 􏰊 x1 􏰋(0) 􏰊 2 􏰋 x=0 and write down the the behaviour of your approximated solution for 4 iterations. Can you get a converged solution ? 3. Swap the rows of Eq. (3.40) and try to find the solution again. Can you get a converged solution ? Explain why. 2 3.3 Newton’s method for nonlinear equations (re- visited) Earlier in the course, we talked about finding roots of two simultaneous nonlin- ear equations. What if we want to find roots of 3 or more simultaneous nonlinear equations ? The methods that we have mentioned so far can only cope with linear equations. What can we do for nonlinear equations ? For simplicity, the theory for solving a system of 3 or more simultaneous nonlinear equations will be developed for 3 × 3 system. Extension to larger systems is straight forward. Consider the following system of 3 simultaneous nonlinear equations u(x,y,z) = 0 v(x,y,z) = 0 w(x,y,z) = 0 Do a Taylor series expansion on the left hand side and truncate at the first order terms to get 3.3. NEWTON’S METHOD FOR NONLINEAR EQUATIONS (REVISITED) 63 where u(xi+1, yi+1, zi+1) v(xi+1, yi+1, zi+1) w(xi+1, yi+1, zi+1) ≈ u(xi, yi, zi) + ux(xi, yi, zi)(xi+1 − xi) + uy(xi,yi,zi)(yi+1 −yi) + uz(xi,yi,zi)(zi+1 −zi) =0 ≈ v(xi, yi, zi) + vx(xi, yi, zi)(xi+1 − xi) + vy(xi,yi,zi)(yi+1 −yi) + vz(xi,yi,zi)(zi+1 −zi) =0 ≈ w(xi, yi, zi) + wx(xi, yi, zi)(xi+1 − xi) + wy(xi,yi,zi)(yi+1 −yi) + wz(xi,yi,zi)(zi+1 −zi) =0 ux = ∂u,uy = ∂u,uz = ∂u ∂x ∂y ∂x vx = ∂v,vy = ∂v,vz = ∂v ∂x ∂y ∂z wx = ∂w,wy = ∂w,wz = ∂w ∂x ∂y ∂z We can put the above system of equations into matrix form  ux uy uz  xi+1 − xi   u   vx vy vz  yi+1 − yi = − v (3.41) wxwywz zi+1−zi w 64 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS Equation (3.41) is a linear equation so we should be able to use any of the methods discussed earlier to solve it ! Hence, to solve the original system of equations, do the following 1. Calculate all the required derivatives. 2. Guess the (initial) values for xi, yi and zi. 3. Solve Eq. (3.41) to obtain (xi+1 − xi), (yi+1 − yi) and (zi+1 − zi). You can use Gauss elimination, LU decomposition, Gauss-Seidel etc. 4. Calculate xi+1, yi+1, zi+1 since you know xi, yi and zi from step 2 above. 5. Repeat steps 2, 3 and 4 until a converged solution is obtained. This method is a more general version of the Newton-Raphson method mentioned earlier. Example 3.2: If it is desired to solve the following set of nonlinear algebraic equations 1 2 e−xy +20z+10π−3 = 0 3 using the Newton-Raphson method, one would have to define the functions u(x, y, z), v(x, y, z), w(x, y, z) and calculate the corresponding partial derivatives. Firstly, let u(x,y,z) = 3x+cos(yz)− 12 v(x,y,z) = x2 −81(y+0.1)2 +sin(z)+1.06 w(x,y,z) = e−xy+20z+10π−3. 3 The corresponding partial derivatives are 3x + cos(yz) = x2 − 81(y + 0.1)2 + sin(z) = −1.06 3.3. NEWTON’S METHOD FOR NONLINEAR EQUATIONS (REVISITED) 65 ∂u = −y sin(yz) ∂z ∂v =cos(z) ∂z ∂w = −ye−xy ∂w = −xe−xy ∂x ∂y ∂z In order to use Eq. (3.41), we would need to guess initial values of x,y and z. Starting with (x0, y0, z0) = (0, 0, 0) and substituting these numbers into Eq. (3.41) gives  3.0 0.0 0.0  x1 − x0  −0.5000000000    0.0 −16.2 1.0  y1−y0 = −0.25     0.0 0.0 20.0  z1 − z0  −10.47197551  3.0   −0.33334  0.016841 0.0046301 0.00014934  x2 − x1  0.000039     y2 − y1 =  −0.02827      z2 − z1  0.0028 ∂u=3 ∂u =−zsin(yz) ∂x∂y ∂v =2x ∂v =−16.2−162y ∂x∂y −13.464 0.16619 0.86602 20.0  3.0 0.0040504 0.00011437  x3 − x2  0.00000061    −0.33331  0.014744 −13.805 0.166246 0.86608  y3 − y2 =  0.000359     20.0  z3 − z2  −0.0000 and so on. Note that with every iteration, the right hand side becomes smaller and smaller. Eventually, the right hand side becomes a zero vector and xn+1 = xn = −0.16667, yn+1 = yn = −0.01481, zn+1 = zn = −0.523476 and we get a converged solution when n is large. ∂w = 20 Exercise 3.8: Solve the following set of nonlinear equations 66 CHAPTER 3. SYSTEMS OF LINEAR ALGEBRAIC EQUATIONS xyz−x2+y2 = 1.34 xy−z2 = 0.09 ex−ey+z = 0.41 using the multi-dimensional Newton-Raphson method. One can easily extend the example above to a system of N equations. Suppose it is desired to solve f1 (x1, x2, x3, x4, ......., xN ) = 0 f2 (x1, x2, x3, x4, ......., xN ) = 0 f3 (x1,x2,x3,x4,.......,xN) = 0 , . . fN (x1, x2, x3, x4, ......., xN ) = 0 Then Eq. (3.41) extended to this system is   ∂f1 ∂f1 ∂x1 ∂x2 ∂f2 . . x2 −x2  f2 x1 ,x2 =− ,...,xN    􏰄 (k−1) (k−1) (k−1)􏰅   (k) (k−1) f x ,x ,...,x  ···112N ∂xN x1 −x1   􏰄 􏰅  . .  (k) (k−1)   (k−1) (k−1) (k−1)  ∂f1 ..  ∂x1  . . .. . .   .  .....  ∂f ··· ··· ∂f  x(k) −x(k−1) N N N N    f 􏰄 􏰅  x(k−1),x(k−1),...,x(k−1)  ∂x1 ∂xN N12N x(k−1) is the previous guess of xi and x(k) is your next (hopefully better !) guess of ii xi. The matrix of partial derivatives on the left hand side, ∂fi/∂xj, is usually known as the Jacobian. It is evaluated at x(k−1). i Chapter 4 Least Squares Suppose from an experiment you get a set of x, y values that look something like in Fig. 4.1. The data looks like it can be well approximated by a straight line. How can you draw a line a best fit through the data ? I.e. which of lines p, q or r above best fit the data ? A method of systematically calculating the line of best fit is called Least Squares approximation. y(x) q r Figure 4.1: Fitting a straight lines through a set of data points. 67 x p 68 CHAPTER 4. LEAST SQUARES 4.1 Linear least squares approximation Let’s represent any one of the lines in Fig. 4.1 as Y = ax + b (4.1) So our task now is to find a and b such that the predicted by Eq. (4.1) above is as close to the actual value of yi at a particular value of xi. The error between the predicted y value and yi is denoted by ei = yi − 􏰕􏰔􏰓􏰖 actual value (axi + b) 􏰕 􏰔􏰓 􏰖 predicted value Because we have N data points, let’s square the error at every point and sum the all up. That way we have the total error between the line and the actual data points. S = e21 + e2 + e23 + e24 + ....... + e2N N = 􏰐 e 2i i=1 N S = 􏰐 (yi − axi − b)2 i=1 We would like to find a and b, such that S is minimized. So we differentiate S with respect to a and b and the derivatives to zero. Remember that xi and yi are constants (data points) when we do the differentiation. N ∂S =􏰎2(yi−axi−b)(−xi)=0 N ∂S = 􏰎2(yi −axi −b)(−1) = 0 Exercise 4.1: Show that Eq. (4.2) can be re-written as ∂a i=1 (4.2) ∂b i=1 4.2. POLYNOMIAL LEAST SQUARES APPROXIMATION 69 NNN a 􏰐 x 2i + b 􏰐 x i = 􏰐 x i y i i=1 i=1 i=1 NN a 􏰐 xi + bN = 􏰐 yi i=1 i=1 The above two equations are just a system of 2 × 2 linear equations. They can be solved to get a and b. NNN 􏰎2􏰎 􏰎 i i􏰊a􏰋ii xxxy i=1 i=1  = i=1 􏰎N b 􏰎N i  i  xNy i=1 i=1 Exercise 4.2: Example: Suppose that you have done an experiment and collected the following data on how the resistance of a wire varies with tempera- ture T (oC) 20.5 32.7 51.0 73.2 95.7 R (ohms) 765 826 873 942 1032 Obtain a linear least squares fit of the data. 4.2 Polynomial least squares approximation Suppose we have data as shown in Fig. 4.2. Clearly, it is not appropriate to fit a straight line through the data. For this data, it is more appropriate to fit a polynomial through it. Assume a functional relationship of the form Y =a0 +a1x+a2x2 +......+anxn 70 CHAPTER 4. LEAST SQUARES 1.005 1.004 1.003 1.002 1.001 1 0.999 0.998 0.997 0.996 0.995 Figure 4.2: Data points requiring nonlinear fit of data. The error between the actual data and and the polynomial above is defined as y 0 0.2 0.4 0.6 0.8 1 x ei = yi − Yi =yi−􏰁a0+a1xi+a2x2i +......+anxni􏰂 =yi−a0−a1xi−a2x2i −......−anxni Let’s square the error and sum it NN S=􏰐e2i =􏰐􏰁yi−a0−a1xi−a2x2i −......−anxni􏰂2 i=1 i=1 (4.3) Equation (4.3) represent the total error. We would like to minimize the. To do this, calculate the partial derivatives with respect to ai’s and set them to zero. 4.2. POLYNOMIAL LEAST SQUARES APPROXIMATION 71 N ∂S =􏰎2(yi−a0−a1xi−a2x2i −......−anxni)(−1)=0, N ∂S =􏰎2(yi−a0−a1xi−a2x2i −......−anxni)(−xi)=0, N ∂S =􏰎2(yi−a0−a1xi−a2x2i −......−anxni)(−x2i)=0, . . . N ∂S =􏰎2(yi−a0−a1xi−a2x2i −......−anxni)(−xni)=0, Equation (4.4) can be written in the following matrix form ∂a0 i=1 ∂a1 i=1 ∂a2 i=1 (4.4) ∂an i=1 􏰎􏰎2􏰎3􏰎n􏰎 Nxi xi xi···xia0yi  􏰎 􏰎2 􏰎3 􏰎4 􏰎n+1  􏰎  xxxx···xaxy iiii 1ii  i 􏰎x2 􏰎x3 􏰎x4 􏰎x5 ··· 􏰎xn+2 a  􏰎x2y  iiiii2ii  􏰎x3 􏰎x4 􏰎x5 􏰎x6 ··· 􏰎xn+3  a = 􏰎x3y i i i i i3ii  . . . . . .  .   .     􏰎xn 􏰎xn+1 􏰎xn+2 􏰎xn+3 ··· 􏰎x2n  a   􏰎xny  iiiiinii The above matrix system can then be solved using techniques such as Gauss elimi- nation or LU decomposition to give the ai’s. 72 CHAPTER 4. LEAST SQUARES Chapter 5 Interpolation Sometimes, you may need to estimate the value between data points. One of the more common method is polynomial interpolation. We fit the data with the function f(x)=a0 +a1x+a2x2 +···+anxn (5.1) and require that the function passes through all the data points. See examples shown in Fig. 5.1. f(x) f(x) f(x) xxx Figure 5.1: Polynomial interpolation through a set of data points. 73 74 CHAPTER 5. INTERPOLATION 5.1 5.1.1 Figure 5.2: Linear Newton Interpolation. Newton Interpolation Linear Newton Interpolation From similar triangles, f(x1) f(x) f(x0) b x0 x x1 c a For linear interpolation, we simply draw a straight line between the two given data points a and b (see Fig. 5.2). If we want to estimate the value at point x, linear interpolation will give us the value at c whereas the true value is d. Thus there is an error associated with the linear interpolation. Shape of actual function f(x) d Linear interpolated function f1(x) f1(x) − f(x0) = f(x1) − f(x0) (5.2) x − x0 x1 − x0 Exercise 5.1: Rearrange Eq. (5.2) to give f1(x) = f(x0) + f(x1) − f(x0) (x − x0) (5.3) x1 − x0 and 5.1. NEWTON INTERPOLATION 75 􏰆 x1 − x 􏰇 􏰆 x − x0 􏰇 f1(x)=f(x0) x −x +f(x1) x −x (5.4) 10 10 A few things worth noting about the linear interpolation • Equation (5.3) is called the linear-interpolation formula • The term (f (x1 ) − f (x0 ))/(x1 − x0 ) in Eq. (5.3) is called the finite divided difference approximation of the first derivative (see later). • In general, the smaller the interval between x0 and x1, the better the approxi- mation at point x. • The subscript 1 in f1(x) denote a first order interpolating polynomial. Exercise 5.2: Use linear interpolation to estimate the value of the function f(x) = 1 − e−x at x = 1.0. Use the interval x0 = 0 and x1 = 5.0. Repeat the calculation with x1 = 4.0, x1 = 3.0 and x1 = 2.0. Illustrate on a graph how you are approaching the exact value of f(1) = 0.6321. 5.1.2 Quadratic Newton Interpolation For quadratic interpolation, let’s assume that the polynomial is of the following form f2(x)=b0 +b1(x−x0)+b2(x−x0)(x−x1) (5.5) The interpolated function is required to have the the same values as the actual function at the data points, hence • f2(x0) = f(x0) • f2(x1) = f(x1) • f2(x2) = f(x2) 76 CHAPTER 5. INTERPOLATION Shape of actual function f(x) Quadratically interpolated function f2(x) x0 x1 x2 Figure 5.3: Quadratic Newton Interpolation. Note that the subscript 2 in f2(x) denote that this is a second order interpolating polynomial. The following exercise shows that Eq. (5.5) is just another form of the polynomial function defined in Eq. (5.1). f(x2) f(x1) f(x0) Exercise 5.3: Show that Eq. (5.5) can be re-written as f2(x) = a0 + a1x + a2x2 where a0 = b0 − b1x0 + b2x0x1 a1 = b1 − b2x0 − b2x1 a2 = b2 Hence, in order to do quadratic interpolation, all we need to do is to find all the b’s in Eq. (5.5). (1) Set x = x0 in Eq. (5.5). Therefore b0 = f(x0) (5.6) 5.1. NEWTON INTERPOLATION 77 (2) If we now let x = x1 in Eq. (5.5) and use the result from step (1) above, to get b1 = f(x1) − f(x0) (5.7) x1 − x0 (3) Since f2(x2) = f(x2), we can use Eqs. (5.6) and (5.7) together with Eq. (5.5) to obtain f(x2)−f(x1) − f(x1)−f(x0) b2 = x2−x1 Equations (5.7) and (5.8) can be simplified by introducing the following notation b1 =f[x1,x0]= f(x1)−f(x0) (5.9) x1 − x0 b2 =f[x2,x1,x0]= f[x2,x1]−f[x1,x0] (5.10) x2 − x0 The advantage of using the square brackets will be clear when we look at the general form of Newton’s interpolating polynomials in the next section. 5.1.3 General form of Newton’s Interpolating Polynomials In general, if you have n data points, you will fit a polynomial of order n−1 through all the n data points. fn(x) = b0 +b1(x−x0)+b2(x−x0)(x−x1)+···+bn(x−x0)(x−x1)···(x−xn−1) where all the b’s are defined as x2 − x0 x1−x0 (5.8) where b0 = b1 = b2 = . . bn= f(x0) f[x1,x0] f[x2,x1,x0] . f[xn,xn−1,··· ,x2,x1,x0] 78 CHAPTER 5. INTERPOLATION f[xi,xj]= f(xi)−f(xj) xi − xj is called the first divided difference, f[xi,xj,xk]= f[xi,xj]−f[xj,xk] xi − xk is called the second divided difference f[xi,xj,xk,xl]= f[xi,xj,xk]−f[xj,xk,xl] xi − xl is called the third divided difference and f[xn,xn−1,...,x1,x0]= f[xn,xn−1,...,x1]−f[xn−1,xn−2,...,x0] xn − x0 is called the n’th divided difference. Notice that the second divided difference is calculated from the first divided difference. The third divided difference is calculated from the second divided difference and so on. This is illustrated in Fig. 5.4. f (xi ) First Second Third f(x0) f[x1,x0] f[x2,x1,x0] f[x3,x2,x1,x0] i xi 0 x0 1 x1 2 x2 3 x3 Figure 5.4: Calculation of Divided Difference. f(x1) f[x2,x1] f[x3,x2,x1] ] f(x2) f[x3,x2] f (x3) 5.2. LAGRANGE INTERPOLATING POLYNOMIALS 79 f(x1) f(x0) x0 x1 Figure 5.5: Two data points Exercise 5.4: Use the Newton’s interpolating polynomial to fit a function through f(x) = 1 − e−x. Assume that you have data at points x =0, 1, 3 and 8. 5.2 Lagrange Interpolating Polynomials Suppose we want to interpolate between two data points, (x0,f(x0)), (x1,f(x1)) shown in Fig. 5.5. Assume that we have two functions, L0(x) and L1(x). They are defined such that 􏰊 1 if L0(x) = 0 if 􏰊 0 if L1(x) = 1 if x = x0 x = x1 x = x0 x = x1 It should be easy to convince yourself that L1(x) and L2(x) can be written as 80 CHAPTER 5. INTERPOLATION 1 L0(x) x0 L1(x) x1 Figure 5.6: Illustrative sketch of L0(x) and L1(x). 5.2. LAGRANGE INTERPOLATING POLYNOMIALS 81 f1(x)=f(x0)L0(x)+f(x1)L1(x) f(x1) f(x0)L0(x) f(x0) Figure 5.7: Illustrative sketch of Lagrange interpolation polynomial. L0(x)= (x−x1) (x0 −x1) L1(x)= (x−x0) (x1 −x0) These two functions are sketched in Fig. 5.6. The first order Lagrange interpo- lating polynomial is obtained by a linear combination of L0(x) and L1(x). f1(x) = f(x0)L0(x) + f(x1)L1(x) (5.11) A sketch of f1(x) is shown in Fig. 5.7. Suppose we three data points, (x0, f(x0)), (x1, f(x1)), (x2, f(x2)) and we want to fit a polynomial through it (see Fig. 5.8). Assume that we have three functions, L0(x),L1(x) and L2(x). They are defined such that  1 i f x = x 0 L0(x)= 0 if x=x1 (5.12) 0 if x=x2 f(x1)L1(x) x0 x1 82 CHAPTER 5. INTERPOLATION f(x2) f(x1) f(x0) x0 x1 x2 Figure 5.8: Three points for polynomial Lagrange interpolation. 0 if x=x0 L1(x)= 1 if x=x1 0 if x=x2 0 if x=x0 L2(x)= 0 if x=x1 1 if x=x2 It should be easy to convince yourself that the following and L2(x) satisfies the conditions listed in Eqs. (5.12) - (5.13) (5.14) expressions for L0(x), L1(x) (5.14). (5.15) (5.16) (5.17) in Fig. 5.9. is obtained by a linear com- L0(x) = L1(x) = L2(x) = (x − x1)(x − x2) (x0 − x1)(x0 − x2) (x − x2)(x − x0) (x1 − x2)(x1 − x0) (x − x1)(x − x0) (x2 − x1)(x2 − x0) For illustrative purposes, Eqs. (5.15) - (5.17) are shown The second order Lagrange interpolating polynomial bination of L0(x), L1(x) and L2(x). f2(x) = f(x0)L0(x) + f(x1)L1(x) + f(x2)L2(x) (5.18) 5.2. LAGRANGE INTERPOLATING POLYNOMIALS 83 1.5 11 0.5 0 0 -0.5 0 L0(x) L1(x) L2(x) xxx 012 12345678 Figure 5.9: Graphical illustration of Eqs. (5.15) - (5.17). 84 CHAPTER 5. INTERPOLATION In general, the nth order Lagrange polynomial (i.e. the Lagrange polynomial that can be fit through n + 1 data points) can be represented concisely as where n fn(x) = 􏰐 Li(x)f(xi) i=0 Li(x) = 􏰑n x − xj j=0 xi −xj j ̸=i (5.19) (5.20) Exercise 5.5: Given the following data x0=1 f(x0)=2 x1 = 4 f(x1) = 3.386294 x2 = 6 f(x2) = 3.791760 Estimate the value of f(x = 2) using both first and second order Lagrange Interpolating polynomials. The matlab program solution for this exercise is given in Matlab Program 5.1 Note that • The Lagrange interpolating polynomial is just a different form of the Newton interpolating polynomials. • The main advantage is that it is slightly easier to program. • It is slightly slower to compute than the Newton Interpolating polynomials. Exercise 5.6: Show that both the Lagrange and Newton interpolating poly- nomials are identical by reducing the first and second order Newton polynomials to first and second order Lagrange polynomials respectively. 5.3. SPLINE INTERPOLATION 85 Matlab Program 5.1 Matlab program for Exercise 5.5. clear all; xi=[1 4 6]; fxi=[2 3.386294 3.791760]; x=2 N=length(xi); fx=0; for i=1:N Lix=1.0; for j=1:N if j ~= i Lix=Lix*(x-xi(j))/(xi(i)-xi(j)); end end fx=fx+fxi(i)*Lix; end 86 CHAPTER 5. INTERPOLATION 20 15 f(x) x) 10 (f 5 Cubic spline interpolation Lagrange interpolation Exact function Data points 00 2 4 6 8 Figure 5.10: Illustration of the advantage of spline interpolation. x x 5.3. SPLINE INTERPOLATION 87 (a) polynomial function S(x) S1(x) S2(x) (b) fi(x) is a single S0(x) xx Figure 5.11: Newton or Lagrange interpolation polynomial (a) and Spline function (b). 5.3 Spline Interpolation Newton or Lagrange interpolation can lead to erroneous results when there is an abrupt change in data (see example in Fig. 5.10) Splines are made up of piecewise polynomials connecting only two data points. This is different to Newton or Lagrange polynomials where you have only one polynomial connecting all data points. See Fig. 5.11. • The spline function,S(x), is made up of several polynomial functions, Si(x). • S(x)=Si(x)forxi x(i)
j=j-1;
break end
end
f(i)=a(j)+b(j)*(x(i)-x_data(j))+c(j)*(x(i)-x_data(j))*(x(i)-x_data(j));
end
plot(x_data,f_data,’ko’,x,f);
xlabel(’x’);ylabel(’f(x)’);

96
CHAPTER 5.
INTERPOLATION
10
8
6
4
2
0
−2
−4
0 1 2 3 4 5 6 7 8 9 10
x
Figure 5.15: The output from Matlab program 5.2. quadratic spline, ◦ data points.
• Note that S(x) is continuous, the derivative of S(x), S′(x), is continuous and the second derivative of S(x), S′′(x), is also continuous at x = xi.
• If there are n + 1 data points, there are only n number of defined Si(x).
We need to find all the ai’s, bi’s, ci’s and di’s (i = 0..n−1) in order to completely determine S(x). For reasons that will become apparent later, we need to ”introduce”
f(x)

5.3. SPLINE INTERPOLATION
97
S2(x)
S0(x)
S1(x)
f(x3) f(x1)
f(x2) f(x0)
Figure 5.16: Cubic spline.
an and cn in order to completely determine S(x). Hence, there are n + 1 number of ai’s and ci’s (i = 0..n) but only n number of bi and di (i = 0..n−1) . The unknowns that need to be solve for in order to completely determine cubic splines are
a0, a1, …., an
b0, b1, …., bn−1
c0, c1, …., cn
d0, d1, …., dn−1
Note that there are n + 1 ai’s and ci’s but only n number of bi’s and di’s. Thus the total number of unknowns are (n+1)+n+(n+1)+n = 4n+2. In order to find all the unknowns, we need 4n + 2 equations. To find these equations,
1. S(x) have the values of f at x = xi, hence,
Si(xi) = ai = f(xi) (5.40) where i = 0..n. Equation (5.40) will give you n + 1 equations.
x0 x1 x2 x3

98 2.
CHAPTER 5. INTERPOLATION
the function values at adjacent polynomials must be equal at the interior knots.
Si(xi+1) = Si+1(xi+1)
ai + bi(xi+1 − xi) + ci(xi+1 − xi)2 + di(xi+1 − xi)2 = ai+1
For the sake of conciseness, let hi = xi+1 − xi, so
ai + bihi + cih2i + dih3i = ai+1 (5.41) where i = 0..n − 1. This will give us another n number of equations.
We also want the derivative of Si(x) to be continuous at the interior nodes ′′
which leads to
bi + 2cihi + 3dih2i = bi+1 (5.43) where i = 0..n−2. Note that for most cubic splines, bn does not exist. Equation
3.
4.
(5.43) will give us another n − 1 equations.
the double derivative of Si(x) to be continuous at the interior nodes.
which gives
ci + 3dihi = ci+1
where i = 0..n − 1. This will give us another n equations.
Si(xi+1) = Si+1(xi+1) (5.42)
′′ ′′
(5.44)
(5.45)
Si (xi+1) = Si+1(xi+1)
The four conditions above will give us (n+1)+n+(n−1)+n = 4n number of equations (Eqs. (5.40), (5.41), (5.43), (5.45)). We are still in need of two more equations because there is a total of 4n + 2 number of unknowns. If you know more information about the function you are trying to approximate (e.g. its derivative at the end points etc.) you can use this information to generate the two additional

5.3. SPLINE INTERPOLATION 99
information. In the absence of any information, it is common to require that the double derivative of the cubic spline is zero at the two end points. This will lead to the following two equations
c0 = 0 (5.46) cn = 0 (5.47)
These two additional equations will give us a total of 4n + 2 equations. So it is now possible to solve for all the ai’s, bi’s, ci’s and di’s.
In order to solve for all the unknowns, do the following. Re-arrange (5.45) to get
di = ci+1 − ci 3hi
Put Eq. (5.48) into Eq. (5.43) to get
bi+1 = bi + (ci + ci+1) hi Put Eq. (5.48) into Eq. (5.41) to get
bi = 1 (ai+1 −ai)− hi (2ci +ci+1) hi 3
(5.48)
(5.49)
(5.50)
Exercise 5.9: Substitute Eq. (5.50) into Eq. (5.49) and show that you will get the following relationship between ai’s and ci’s
hj−1cj−1 +2(hj +hj−1)cj +hjcj+1 = 3 (aj+1 −aj)+ 3 (aj−1 −aj) (5.51) hj hj−1
where j = 1..n − 1.
Equation (5.51) is a tridiagonal system and can be solve if we assume that c0 = cn = 0. The linear system of equation equation that we need to solve looks like the following
where
[A]{X} = {C} (5.52)

100
CHAPTER 5. INTERPOLATION
1000···0  h0 2(h0 +h1) h1 0 ··· 0 
 .. 0 h1 2(h1+h2) h2 . 0 
[A]= 0 0 … … … .  00 ···h2(h+h)h
n−2 n−2 n−1 n−1 000···01
 c 0  
   c1   . . .
{X} = .
   . . .
  c n 
0 
 3 (a − a ) + 3 (a − a )   2 1 0 1  h1 h0 
 3(a3−a2)+3(a1−a2)  h2 . h1
{C} =
Hence, to construct cubic splines, do the following
1. Make use of Eq. (5.40) and set ai = f(xi).
2. Solve Eq. (5.51) to obtain all other values of ci’s. Note that by solving Eq. (5.51) we are assuming that c0 = 0 and cn = 0. This is equivalent to saying that the second derivative at x0 and xn are zero !
3. Use Eq. (5.50) to obtain the bi’s.
4. Use Eq. (5.48) to obtain the di’s.
After steps 1-4, we can evaluate the function values at the point you are interested from the following formula Si(x) = ai + bi(x − xi) + ci(x − xi)2 + di(x − xi)3. The only thing tricky is to find the interval that corresponds to your point.
 .  
 3 (a −a )+ 3 (a −a )
 n n−1
 hn−1 0
hn−2
n−2 n−1  

5.3. SPLINE INTERPOLATION 101
Exercise 5.10: Given the set of data points below, use cubic splines to find thevalueoff atx=5.
f(xi) 3.0 2.5 4.5 1.0 7.0 2.5 9.0 0.5
xi
Exercise 5.11:
You are given the four data points shown in Fig. 5.14 and asked to fit a cubic spline through the four data points. The cubic spline can be thought as be- ing made up of three ’real’ quadratic polynomial S0(x), S1(x), S2(x) and one ’imaginary’ polynomial S3(x). The three polynomials can be written as
S0(x)=a0 +b0(x−x0)+c0(x−x0)2 +d0(x−x0)3
S1(x)=a1 +b1(x−x1)+c1(x−x1)2 +d1(x−x1)3
S2(x)=a2 +b2(x−x2)+c2(x−x2)2 +d2(x−x2)3
S3(x)=a3 +b3(x−x3)+c3(x−x3)2 +d3(x−x3)3 (a) Show that
ai =f(xi) fori=0,1,2,3
(5.53) (5.54) (5.55) (5.56)
(5.57)
(b) By requiring that adjacent Si(x) to have common values at the interior points, show that
a0 + b0h0 + c0h20 + d0h30 = a1 (5.58) a1 + b1h1 + c1h21 + d1h31 = a2 (5.59)

102 CHAPTER 5. INTERPOLATION
a2 + b2h2 + c2h2 + d2h32 = a3 (5.60) (c) By requiring that the derivative of the adjacent Si(x) to have common values
where hi = xi+1 − xi.
at the interior points, show that
b0 + 2c0h0 + 3d0h20 = b1 (5.61) b1 + 2c1h1 + 3d1h21 = b2 (5.62)
(d) By requiring that the double derivative of the adjacent Si(x) to have common values at the interior points, show that
d0 = c1 − c0 3h0
d1 = c2 − c1 3h1
b1 =b0 +(c0 +c1)h0 (5.66) b2 =b1 +(c1 +c2)h1 (5.67)
(f) Substitute Eqs. (5.63) – (5.65) into Eqs. (5.58) – (5.60) and show that
b0 = a1 − a0 − h0 (2c0 + c1) (5.68)
h0 3
b1 = a2 − a1 − h1 (2c1 + c2) (5.69) h1 3
(5.63)
(5.64)
(5.65) (e) Substitute Eqs. (5.63) – (5.65) into Eqs. (5.61) – (5.62) and show that
d2 = c3 − c2 3h2

5.3. SPLINE INTERPOLATION
103
b2 = a3 − a2 − h2 (2c2 + c3) (5.70) h2 3
(g) Use Eqs. (5.66) – (5.70) together with the assumption that c0 = 0 and c3 = 0 and show that the ci’s could be obtained by solving the following matrix equation
 1 h0
0 0 0  c0  2(h0+h1) h1 0 c1 =
 0
0 0 0 1 c3
h1 2(h1+h2) h2 c2  0
3 (a2 −a1)+ 3 (a0 −a1)  h1 h0
 h2 h1 
(5.71)
(f) Explain how you can obtain the coefficients for S0(x), S1(x), S2(x) from the results in parts (a)-(g).
3 (a3 −a2)+ 3 (a1 −a2) 0
Exercise 5.12:
Find the cubic spline that interpolates the following data points
f(xi) 1.0 2.0 3.0 4.0 4.0 9.0 7.0 2.0
xi
Exercise 5.13: A natural cubic spline, S(x), which interpolates the data given below
xi f(xi)
11 21
30

104 CHAPTER 5. INTERPOLATION
is defined to be
􏰊 S0(x)=1+P(x−1)−Q(x−1)3 ifα≤x<β S(x)= S1(x)=1+p(x−2)−34(x−2)2+q(x−2)3 ifγ≤x<λ Find the values of α, β, γ, λ, P, Q, p and q. Chapter 6 Differentiation and Integration In order to find a formula to differentiate a function, consider the Taylor series f(xi+1)=f(xi)+f′(xi)(xi+1 −xi)+ 1f′′(xi)(xi+1 −xi)2 + 1f′′′(ξ1)(xi+1 −xi)3 where 2! 3! (6.1) xi+1 = xi + ∆ f′(x)= df dx and ξ1 is somewhere in between xi and xi+1. Equation (6.1) can be re-arranged to (6.2) (6.3) obtain f ′ (xi ) = f (xi+1 ) − f (xi ) − 1 f ′′ (xi ) (xi+1 − xi ) + 1 f ′′′ (ξ1 ) (xi+1 − xi )2 (xi+1 −xi) 2! 3! Hence f′(x) can be approximated as f′(xi)= f(xi+1)−f(xi) (xi+1 − xi) Equation (6.3) is called the Forward Difference Scheme (FDS) The other terms that have been neglected in Eq. (6.2) gives an approximation of the error in approx- imating f′(xi) with Eq. (6.3). the leading term in the truncation error for the FDS approximation is 105 106 CHAPTER 6. DIFFERENTIATION AND INTEGRATION EFDS = 1f′′(ξ1)(xi+1 −xi) 2! The Taylor series expansion Eq. (6.1)could also be used to obtain an expression for f(xi−1). f(xi−1) = f(xi)+f′(xi)(xi−1 −xi) +1f′′(xi)(xi−1 −xi)2 + 1f′′′(ξ2)(xi−1 −xi)3 2! 3! = f(xi)−f′(xi)(xi −xi−1) +1f′′(xi)(xi −xi−1)2 − 1f′′′(ξ2)(xi −xi−1)3 2! 3! (6.4) Equation (6.4) can be re-arranged to obtain an another approximation for f′(xi) f′(xi)= f(xi)−f(xi−1) (6.5) (xi − xi−1) Equation (6.5) is called the Backward Difference Scheme (BDS) approximation of the first derivative. If we assume that xi−1 is very close to xi, then the leading term in the truncation error for the FDS approximation is EBDS = 1f′′(ξ2)(xi −xi−1) 2! Exercise 6.1: Subtract Eq. (6.4) from Eq. (6.1) and show that, after some algebra, that the first derivative can be written as f′(xi)= f(xi+1)−f(xi−1) −f′′(xi)(xi+1−xi)2−(xi−xi−1)2 xi+1 −xi−1 − f′′′ (ξ1) (xi+1−xi)3 − f′′′ (ξ2) 3! (xi+1 −xi−1 ) 3! 2!(xi+1 −xi−1 ) (xi−xi−1)3 (xi+1 −xi−1 ) Following Ex. (6.1), yet another formula for computing f′(xi) is f′(xi)= f(xi+1)−f(xi−1) xi+1 − xi−1 (6.6) This formula is called the Central Difference Scheme (CDS) and its leading order error is given by 6.1. APPROXIMATION OF THE 2ND DERIVATIVE 107 ECDS =f′′(xi)(xi+1 −xi)2 −(xi −xi−1)2 2!(xi+1 −xi−1) Exercise 6.2: For the case where all the xi’s are equally spaced, i.e. xi+1 − xi = ∆ = const, show that Eqs. (6.3), (6.5), and (6.6) simplifies to the following expressions for the FDS, BDS and CDS f′(xi)= f(xi+1)−f(xi) ∆ f′(xi)= f(xi)−f(xi−1) ∆ f′(xi)= f(xi+1)−f(xi−1) 2∆ Also show that the leading error term in the FDS and BDS is O(∆) and the leading error term in the CDS is O(∆2). Exercise 6.3: An alternative way of obtaining approximations to the deriva- tive of a function is to fit a Lagrange polynomial through the data points and then differentiating the resulting curve. 1. Fit a Lagrange polynomial through two data points and show that you can obtain the forward and backward differencing scheme. 2. Fit a Lagrange polynomial through three data points and show that you can obtain the central differencing scheme. 6.1 Approximation of the 2nd Derivative A formula for the 2nd derivative can be obtained by evaluating f′(x) at points halfway between xi+1 and xi and between xi and xi−1 (see Fig. 6.1). d2f df (xi+1/2) − df (xi−1/2) dx dx dx2 (xi) = 􏰁xi + 12 (xi+1 − xi)􏰂 − 􏰁xi−1 + 12 (xi − xi−1)􏰂 (6.7) 108 CHAPTER 6. DIFFERENTIATION AND INTEGRATION llmm xi-1 xi xi+1 xi-1/2 xi+1/2 Figure 6.1: Figure showing the data points used to calculate the second derivative Using Eq. (6.6) to approximate the first derivative, Eq. (6.7) can be written as d2f dx2 (xi) = f(xi+1)−f(xi) − f(xi)−f(xi−1) xi+1−xi xi−xi−1 (6.8) 12 (xi+1 − xi−1) Exercise 6.4: Show that Eq. (6.8) simplifies to d2f (xi) = f(xi+1) (xi − xi−1) − f(xi) (xi+1 − xi−1) + f(xi−1) (xi+1 − xi) dx2 12 (xi+1 − xi−1) (xi+1 − xi) (xi − xi−1) In addition, if one assumes that xi+1 − xi = xi − xi−1 = ∆, then d2f (xi) = f(xi+1) − 2f(xi) + f(xi−1) (6.9) dx2 ∆2 Exercise 6.5: Apply the forward difference scheme (FDS) twice and show that an approximation for the 2nd derivative can be written as d2f (xi) = f(xi+2) − 2f(xi+1) + f(xi) (6.10) dx2 ∆2 Similarly, apply the backward difference scheme (BDS) twice and show that an approximation for the 2nd derivative can be written as d2f (xi) = f(xi) − 2f(xi−1) + f(xi−2) (6.11) dx2 ∆2 6.2. INTEGRATION 109 f(x1) f(x0) Figure 6.2: Area under a curve f1(x) f(x1) f(x0) x0 x1 x0 x1 Area A' Figure 6.3: The trapezoidal approximation of area under a curve. 6.2 Integration Integration means finding the area under a curve between two points. Let’s call these two points x0 and x1 (see Fig. 6.2). If we only have information at these two points, then the best we can do is to fit a straight line through these two points and find the area under it. We are estimating A with A’ (see Fig. 6.3). We know that the Lagrangian interpolating polynomial for two points can be written as x0 x1 f(x1) f(x0) Area A 110 CHAPTER 6. DIFFERENTIATION AND INTEGRATION f1(x)= (x−x1)f(x0)+ (x−x0)f(x1)+1f(2)(ξ(x))(x−x0)(x−x1) (x0 −x1) (x1 −x0) 2 So to obtain the area A’ all we have to do is to integrate f1(x) from x0 to x1. 􏰒x1 x0 f(x0) 􏰒x1 f1(x)dx = (x −x ) (x−x1)dx (x−x0)dx (x − x0)(x − x1)dx 0 1 x0 f(x1) 􏰒 x1 +(x −x) 1 0 x0 1 (2) 􏰒 x1 (6.12) +2f (ξ1) x0 where ξ1 is some number in (x0,x1). Note that the Weighted Mean Value Theorem for Integrals (see page 8) have been used in evaluating the integral involving the error term. This can be done because (x−x0)(x−x1) does not change sign if x0 ≤ x ≤ x1. Exercise 6.6: Integrate Eq. (6.12) and show that 􏰒 x1 x1 − x0 ∆3 f1(x)dx = 2 (f(x0) + f(x1)) − 12 f(2)(ξ) (6.13) x0 where ∆ = x1 − x0. You might like to use the following result A+m∆ 􏰒 3􏰆m3 m2 􏰇 (x−(A+k∆))(x−(A+l∆))dx=∆ 3 − 2 (k+l)+klm A The Trapezoidal rule is derived from Eq. (6.13) and it approximates the integral of f(x) as 􏰒 x1 ∆ f(x)dx ≈ 2 (f(x0) + f(x1)) (6.14) x0 In general, at any two points, xi and xi+1, the trapezoidal rule can be written as 􏰒 xi+1 xi ∆ f(x)dx ≈ 2 (f(xi) + f(xi+1)) (6.15) 6.2. INTEGRATION 111 The error of this approximation is ∆3 12f(2)(ξ) abab Figure 6.4: Integration over larger interval. So if the size of the interval, h, is large, the error would be large as well. What happens if we want to minimize the error as we integrate over a large interval ? Consider the situation shown in Fig. 6.4. Clearly, in this situation, one cannot expect to get an accurate answer if one were to use the trapezoidal rule to integrate over the interval a ≤ x ≤ b. In order to get a more accurate answer, it is common to divide the domain into smaller intervals, usually of equal length, and apply the Trapezoidal rule in each of the subintervals (see Fig. 6.5). Area estimated by the trapezoidal rule Actual area under a curve 112 CHAPTER 6. DIFFERENTIATION AND INTEGRATION ∆1 a=x0 x1 ∆1 ∆2 ∆2 ∆2 ∆2 b=x2 a=x0 x1 x2 x3 b=x4 Figure 6.5: Decomposing a large interval into smaller intervals. Intuitively, it should be clear that as you divide the large interval up into smaller in- tervals, you should get more accurate answers than simply just applying Trapezoidal rule once over the entire large interval. What is the general formula for Trapezoidal rule if we apply it to smaller subintervals ? Consider the situation shown in Fig. 6.6. If we apply the Trapezoidal rule to each of the subintervals, A0 = ∆2 (f(x0)+f(x1)) A1 = ∆2 (f(x1)+f(x2)) A2 = ∆2 (f(x2)+f(x3)) A3 = ∆2 (f(x3)+f(x4)) The trapezoidal rule estimate of the area under the curve would be just the sum of all the Ai’s 6.3. SIMPSON’S RULE 113 A = = = = In which A0+A1+A2+A3 ∆2 (f(x0) + f(x1)) + ∆2 (f(x1) + f(x2)) + ∆2 (f(x2) + f(x3)) + ∆2 (f(x3) + f(x4)) ∆2 (f(x0) + 2f(x1) + 2f(x2) + 2f(x3) + f(x4)) ∆􏰌3􏰍 2 f(x0)+2􏰐f(xi)+f(x4) i=1 general, the formula for Trapezoidal rule applied to a large interval a < x < b is divided into N subintervals of equal size h is (6.16) b 􏰌N−1 􏰍 􏰒∆􏰐 a f(x)dx≈ 2 f(a)+2 f(xi)+f(b) i=1 Exercise 6.7: Do the following integral 10 􏰒 1dx x 2 using the Trapezoidal rule with increasing number of integrals. Compare with the exact value of 1.6094379. 6.3 Simpson’s rule The Trapezoidal rule was derived by fitting the Lagrange polynomial through two points. A more accurate method of calculating integrals would be try and fit a Lagrange polynomial through three equally spaced points. Then find the area under the Lagrange polynomial (see Fig. 6.7). What we are trying to do here is to estimate the area A under the actual function with A′ which is the area under the Lagrange polynomial (see Fig. 6.8). For three data points, the Lagrange interpolating Polynomial can be written as 114 CHAPTER 6. DIFFERENTIATION AND INTEGRATION f(x) A0 A1 A2 A3 ∆∆∆∆ a=x0 x1 x2 x3 b=x4 Figure 6.6: Decomposing a large interval into smaller intervals. The total area under the curve can be quite accurately represented by A0 + A1 + A2 + A3. f2(x) = (x−x1)(x−x2) f(x0) (x0 −x1)(x0 −x2) + (x−x0)(x−x2) f(x1) (x1 −x0)(x1 −x2) + (x−x0)(x−x1) f(x2) (x2 −x0)(x2 −x1) +61(x − x0)(x − x1)(x − x2)f(3)(ξ(x))dx 6.3. SIMPSON’S RULE 115 Actual function f(x2) f(x1) f(x0) x0 where the last term is the error term associated with the Lagrange polynomial. ∆ ∆ Approximation of the actual function with Lagrange polynomial through 3 data points. x1 x2 Figure 6.7: Fitting a Lagrange polynomial through three data points. Exercise 6.8: Assume that x0, x1 and x2 are equally spaced. Integrate between x0 and x2 and show that where x2 􏰒∆ f2(x) = 3 (f(x0) + 4f(x1) + f(x2)) x0 x2 − x1 = ∆ Ignore the error term. You might like to use the following result x1 − x0 = ∆ A+m∆ 􏰒 3􏰆m3 m2 􏰇 (x−(A+k∆))(x−(A+l∆))dx=∆ 3 − 2 (k+l)+klm A (6.17) 116 CHAPTER 6. DIFFERENTIATION AND INTEGRATION f(x2) f(x1) f(x0) f(x2) f(x1) f(x0) ∆ x0 x1 x2 Actual area A x0 x1 x2 Calculated area A’ ∆ Figure 6.8: Physical illustration of Simpsons rule. ∆ ∆ In general the simpson’s rule can be written as xi+2 􏰒∆ f2(x) = 3 (f(xi) + 4f(xi+1) + f(xi+2)) xi It is more accurate than the Trapezoidal rule. If you want to integrate a function over a large interval, it is better to split the function into smaller interval and use the Simpsons rule over smaller sub-intervals. See the example in the section on Trapezoidal rule. For example, if we divide up the domain of a function into 10 intervals, i.e. if we have 11 data points, (x0, f(x0)), (x1, f(x1)), (x2, f(x2)),....,(x10, f(x10)), (6.18) 6.3. SIMPSON’S RULE 117 􏰒 x10 f (x)dx x0 ∆ ≈ 3 + ∆3 + ∆3 + ∆3 + ∆3 (f(x0) + 4f(x1) + f(x2)) (f(x2) + 4f(x3) + f(x4)) (f(x4) + 4f(x5) + f(x6)) (f(x6) + 4f(x7) + f(x8)) (f(x8) + 4f(x9) + f(x10)) = ∆3[f(x0) + 4(f(x1)+f(x3)+f(x5)+f(x7)+f(x9)) + 2(f(x2)+f(x4)+f(x6)+f(x8)) + f(x10)] In general, the simpson’s rule can be written as  ∆ N−1 N−2 3 f(x0)+4􏰐f(xi)+2 􏰐 f(xi)+f(xN) (6.19) i=1 i=1 iodd ieven where N is the number of intervals. Note that Simpson’s rule only works of N is even i.e. the total number of data points (N + 1) is odd. Exercise 6.9: A large interval, x0 < x < xN , can be divided into N equally spaced subintervals of size ∆ with N + 1 data points (see Figure 6.9). Assuming that N is even, use Eq. (6.17) on two subintervals of size ∆ and show that the integral of f(x) over the large interval can be approximated as 􏰒 x ∆ 4∆ N − 1 N􏰐 f(x)dx ≈ 3f(x0)+ 3 f(xn) n odd 2∆ N − 2 ∆ + 􏰐 f(xn)+ f(xN) (6.20) x0 3 neven 3 118 CHAPTER 6. DIFFERENTIATION AND INTEGRATION f(xN) f(xN-1) f(x2) f(x1) f(x0) f(x) x0 x1 x2 xN-1 xN ∆ ∆ ∆ Figure 6.9: Equally spaced subintervals of size ∆ which are inside the large interval, x0 ≤x≤xN. Chapter 7 Boundary Value Problem In many engineering problems, you will have to solve ordinary differential equations that look something like where d2y + p(x)dy + q(x)y = r(x) dx2 dx (7.1) a≤x≤b We are also usually given the conditions at the boundaries y(a) = α y(b) = β Exercise 7.1: Using CDS approximation for all the derivatives, show that Eq. (7.1) can be approximated as 􏰄−pi + 2 􏰅yi−1 xi+1 −xi−1 (xi+1 −xi−1 )(xi −xi−1 ) + 􏰄− 2 (xi+1 −xi )(xi −xi−1 ) + q 􏰅 y i i (7.2) +􏰄 pi + xi+1 −xi−1 2 (xi+1 −xi−1 )(xi+1 −xi ) yi = y(xi) pi = p(xi) qi = q(xi) ri = r(xi) 􏰅yi+1 =ri where 119 120 CHAPTER 7. BOUNDARY VALUE PROBLEM From the boundary conditions, we know that y0 = α (7.3) and yN =β Eq. (7.2) together with Eq. (7.3) represent a linear system that can be solved. They can be represented by where [A]{X} = {C} 1000···0 χ1ε1η1 0···0  .. 0χ2ε2η2 .0 [A] =  0 0 ... ... ... .   0 0 · · · χ ε η  N−2 N−2 N−1 0000···1  y 0      y1   . . . {X} = .    . . .   y N   α    1  r  r  {C} = .2  .   r   N−1   β  􏰆pi 2􏰇 χi = −x −x +(x −x )(x−x ) εi = −(xi+1 −xi)(xi −xi−1) +qi 􏰆pi 2􏰇 121 i+1 i−1 i+1 i−1 i i−1 􏰆2􏰇 ηi = x −x +(x −x )(x −x) i+1 i−1 i+1 i−1 i+1 i Example 7.1: Suppose you are asked to solve d2y + 3dy + 2y = 0 (7.4) dx2 dx in the domain 0 ≤ x ≤ 1. You are also given the boundary condition y(0) = 1.0 and y(1) = e−2. From the previous chapter, you know that the derivative and double derivative can be approximated as dy(xi)= yi+1 −yi−1 (7.5) dx 2∆ d2y (xi) = yi+1 − 2yi + yi−1 (7.6) dx2 ∆2 If we now substitute Eqs. (7.5) and Eqs. (7.6) into Eq. (7.4), you will end up with 􏰆13􏰇􏰆2􏰇􏰆13􏰇 ∆2−2∆ yi−1+ −∆2+2 yi+ ∆2+2∆ yi+1=0 (7.7) Equation (7.7) is the discretised form of Eq. (7.4). Suppose we want to implement Eq. (7.7) on a grid as shown in Fig. (7.1). This grid is equally spaced with the spacing between grid points, x∆ = 0.25. The corresponding values of y on the grid shown in Fig. (7.1) are y0, y1, y2, y3, y4 respectively. The yi’s are the unknowns that you need to solve for. Thus for this grid, you have 5 unknowns. So implementing Eq. (7.7)onx=x1,x=x2,x=x3,willgive 10y0 − 30y1 + 22y2 10y1 − 30y2 + 22y3 10y2 − 30y3 + 22y4 = 0 = 0 = 0 (7.8) (7.9) (7.10) 122 CHAPTER 7. BOUNDARY VALUE PROBLEM We also know from the given boundary conditions that y0 = 1.0 y4 = e−2 You can put Eqs. (7.8)-(7.12) in matrix form as 10000y1 0      1  10−3022 0 0y0       0 10 −30 22 0  y2 = 0 0 0 10−3022y0  3  0 0 0 0 1   y     e − 2   4 One can now solve Eq. (7.13) to get y1 0 = (7.11) (7.12) (7.13)    1      y   0.5972   y2  y   3    y   4 0.3598  0.2192  (7.14) which is the approximate solution to Eq. (7.4) at x = 0, 0.25, 0.5, 0.75, 1.00.     e − 2   ∆=0.25 x0=0 x1=0.25 x2=0.5 x3=0.75 x4=1.0 Figure 7.1: Figure showing the grid used in Example 7.1 Chapter 8 Ordinary Differential Equations In many engineering problems, you will need to solve differential equations that look something like in the domain with the initial condition 8.1 Euler’s method dx = f(t,x) dt a≤t≤b x(t = a) = α (8.1) Euler’s method is probably the simplest method used to solve Eq. (8.1). Consider Taylor’s theorem x(tn+1) = x(tn) + (tn+1 − tn)dx(tn) + (tn+1 − tn)2 d2x(ξn) (8.2) dt 2dt2 where ξn is somewhere in between tn and tn+1. If we let tn+1 − tn = h and substitute Eq. (8.1) into Eq. (8.2), we get x(tn+1 ) = x(tn ) + hf (tn , xn ) + h2 d2 x (ξn ). 2 dt2 If we assume that h is small, then we can neglect the second order term in the equation above. Thus, we get the formula for Euler’s method 123 124 CHAPTER 8. ORDINARY DIFFERENTIAL EQUATIONS xn+1 =xn +hf(tn,xn) (8.3) where xn is the numerical approximation of the exact solution x(tn). Euler’s method is the simplest method but it is not very accurate. Matlab program 8.1 shows how Eulers method can be used to solve dx = −2x dt with the initial condition x(0) = 1.0 for 0 ≤ t ≤ 3. The matlab program compares the numerical solution with the analytical solution, which is x(t) = exp(−2t). The output from this program for h = 0.3 is shown in Fig. 8.1 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 00 0.5 1 1.5 2 2.5 3 t Figure 8.1: The output from Matlab program 8.1 Exercise 8.1: Using Euler’s method, solve dx = e−t dt x 8.1. EULER’S METHOD 125 Matlab Program 8.1 A matlab program illustrating the Eulers method. clear all % Sample program to do Euler integration % Parameters for simulation N=10; h=3/N; % Initial condition x(1)=1; t(1)=0; %Loop for calculating solution with Eulers method for n=1:N dxdt=-2*x(n); x(n+1)=x(n)+dxdt*h; t(n+1)=t(n)+h; end; %Specifying exact solution t_plot=linspace(0,10); x_exact=exp(-2*t_plot); %Plotting numerical solution with analytical solution plot(t_plot,x_exact,’k-’,t,x,’ko’); xlabel(’t’); ylabel(’x’); axis([0 3 0 1]); 126 CHAPTER 8. ORDINARY DIFFERENTIAL EQUATIONS for0 1. Hence, in order to ensure the stability of the numerical method, |σ| < 1. |σ|2 = (1+hλR)2 +(hλI)2 < 1 This is just a circle of radius 1 centred on (-1,0). This plot is called the stability diagram and is shown in Fig. 8.5 2 λIh Figure 8.5: The stability diagram of Euler method. If λ is real and negative, then in order for the numerical method to be stable, λ Rh 8.5. STABILITY AND ERROR ANALYSIS 137 h≤ 2 (8.23) |λ| Exercise 8.3: Solve with dx +2x = 0 dt (8.24) x(t = 0) = 1 Use h = 1.2, 0.8 and 0.3 in the domain 0 ≤ t ≤ 30 Exercise 8.4: Perform a stability analysis on the Crank-Nicholson method and show that it is unconditionally stable, i.e. the method is stable for all values of h. Exercise 8.5: Perform stability analysis for the second order Runge-Kutta method on the model equation dx = λx dt and show that the region of stability can be obtained by solving the following equation λ2 h2 1+λh+ 2 −eiθ =0 (8.25) Show also that for the 4th order Runge-Kutta method, the stability region is obtained by solving λ2h2 λ3h3 λ4h4 λh+ 2 + 6 + 24 +1−eiθ=0 (8.26) The solutions to Eqs. (8.25) and (8.26) are shown in Fig. 8.6. 138 CHAPTER 8. ORDINARY DIFFERENTIAL EQUATIONS 8.6 Systems of Ordinary Differential Equations In many engineering problems, it is essential to solve, not just one, but a set of ordinary differential equations. In general, the set of equations that you might be required to solve can be expressed as dx1 dt dx2 dt dx3 dt dx4 = f1(x1, x2, ...., xN ) = f2(x1, x2, ...., xN ) = f3(x1, x2, ...., xN ) = f4(x1, x2, ...., xN ) .=. dt .=. .=. dxN dt = fN (x1, x2, ...., xN ) If you have a linear system, then the above set of equations can be expressed in matrix-vector notation as d {x} = [A] {x} (8.27) dt where the curly brackets ({}) denotes a vector and the square brackets [] represents a matrix. {x} is the vector of dependent variables and [A] is usually a matrix of constant coefficients. Equation (8.27) can be solved using the methods described above. Applying the explicit Euler method {x}n+1 − {x}n h = [A] {x}n {x}n+1 = {x}n + h [A] {x}n At every time step, one would need to perform a matrix-vector multiplication in order to obtain the values of the vector {x} at the next time step. 8.6. SYSTEMS OF ORDINARY DIFFERENTIAL EQUATIONS 139 If an implicit scheme is required to solve the problem, then one would need to solve an system of linear algebraic equations at every time step. For example, applying the trapezoidal/Crank Nicholson scheme to Eq. (8.27) gives {x}n+1 − {x}n h {x}n+1 − h2 [A] {x}n+1 􏰈 h 􏰉 n+1 1 n 1 n+1 = 2[A]{x} +2[A]{x} = {x}n + h2 [A] {x}n 􏰈 h 􏰉 n I−2[A] {x} Equation (8.28) can now be solved using methods for solving a system of differ- = I+2[A] {x} ential (e.g. LU decomposition, Gauss-Seidel etc.) (8.28) Exercise 8.6: Solve the following second order ordinary differential equation with the initial conditions d2x + ω2x = 0 dt2 x(t=0) = p dx(t=0) = 0. dt (8.29) Outline the numerical algorithm for the Eulers and Trapezoidal method. 8.6.1 Multi-dimensional Runge-Kutta methods In many situations, you might be required to solve a multi-dimensional system using RK-4 and RK-2. In the examples below, it will be assumed that the system you are interested in is autonomous, i.e. the right hand side is not a function of time. The multi-dimensional form or RK-2 to solve the system of 2 equations dx1 dt dx2 dt = f1(t, x1, x2) = f2(t, x1, x2) 140 CHAPTER 8. ORDINARY DIFFERENTIAL EQUATIONS (i.e. extending Eq. (8.18) to a set of 2 equations) can be written as 􏰆1 1􏰇 xn+1,1 = xn,1 + 2k11 + 2k21 h 􏰆1 1􏰇 xn+1,2 = xn,2 + 2k12 + 2k22 h k11, k12, k21 and k22 can be obtained from k11 = f1(tn, xn,1, xn,2) k12 = f2(tn, xn,1, xn,2) k21 = f1 (tn +h,xn,1 +hk11,xn,2 +hk12) k22 = f2 (tn +h,xn,1 +hk11,xn,2 +hk12) Further extending the RK-2 method to solve the system of 4 equations dx1 dt dx2 dt dx3 dt dx4 dt then the solution can be written as xn+1,1 = xn+1,2 = xn+1,3 = xn+1,4 = (8.30) (8.31) = f1(t, x1, x2, x3, x4) = f2(t, x1, x2, x3, x4) = f3(t, x1, x2, x3, x4) = f4(t, x1, x2, x3, x4) xn,1 + xn,2 + xn,3 + xn,4 + 􏰆1 1􏰇 2k11 + 2k21 h 􏰆1 1􏰇 2k12 + 2k22 h 􏰆1 1􏰇 2k13 + 2k23 h 􏰆1 1􏰇 2k14 + 2k24 h (8.32) (8.33) (8.34) (8.35) 8.6. SYSTEMS OF ORDINARY DIFFERENTIAL EQUATIONS 141 where k11, k12, k13, k14 and k21, k22, k23 and k24 can be obtained from k21 = k22 = k23 = k24 = f1 (tn + h, xn,1 + hk11, xn,2 + hk12, xn,3 + hk13, xn,4 + hk14) f2 (tn + h, xn,1 + hk11, xn,2 + hk12, xn,3 + hk13, xn,4 + hk14) f3 (tn + h, xn,1 + hk11, xn,2 + hk12, xn,3 + hk13, xn,4 + hk14) f4 (tn + h, xn,1 + hk11, xn,2 + hk12, xn,3 + hk13, xn,4 + hk14) k11 = k12 = k13 = k14 = f1(tn, xn,1, xn,2, xn,3, xn,4) f2(tn, xn,1, xn,2, xn,3, xn,4) f3(tn, xn,1, xn,2, xn,3, xn,4) f4(tn, xn,1, xn,2, xn,3, xn,4) The RK-4 method can also be easily extended to a multi-dimensional system. Suppose you are asked to solve a system of 4 ordinary differential equations that looks like dx1 dt dx2 dt dx3 dt dx4 dt Equation (8.19) can be written in multi-dimensional form as xn+1,1 = xn+1,2 = xn+1,3 = xn+1,4 = xn,1 + xn,2 + xn,3 + xn,4 + 􏰆11 1􏰇 6k11 + 3 (k21 + k31) + 6k41 h 􏰆11 1􏰇 6k12 + 3 (k22 + k32) + 6k42 h 􏰆11 1􏰇 6k13 + 3 (k23 + k33) + 6k43 h 􏰆11 1􏰇 6k14 + 3 (k24 + k34) + 6k44 h (8.36) (8.37) (8.38) (8.39) = f1(t, x1, x2, x3, x4) = f2(t, x1, x2, x3, x4) = f3(t, x1, x2, x3, x4) = f4(t, x1, x2, x3, x4) 142 CHAPTER 8. ORDINARY DIFFERENTIAL EQUATIONS where the approximated solution to xi at time n + 1 is denoted as xn+1,i. The first subscript refers to the time level and the second subscript of the variable x correspond to the number of dependent variables. In order to approximate xi at various times, kij need to be calculated as follows and k21 = f1 k22 = f2 k23 = f3 k24 = f4 k31 = f1 k32 = f2 k33 = f3 k34 = f4 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k11,xn,2 + 2k12,xn,3 + 2k13,xn,4 + 2k14 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k11,xn,2 + 2k12,xn,3 + 2k13,xn,4 + 2k14 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k11,xn,2 + 2k12,xn,3 + 2k13,xn,4 + 2k14 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k11,xn,2 + 2k12,xn,3 + 2k13,xn,4 + 2k14 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k21,xn,2 + 2k22,xn,3 + 2k23,xn,4 + 2k24 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k21,xn,2 + 2k22,xn,3 + 2k23,xn,4 + 2k24 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k21,xn,2 + 2k22,xn,3 + 2k23,xn,4 + 2k24 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k21,xn,2 + 2k22,xn,3 + 2k23,xn,4 + 2k24 k11 = k12 = k13 = k14 = f1(tn, xn,1, xn,2, xn,3, xn,4) f2(tn, xn,1, xn,2, xn,3, xn,4) f3(tn, xn,1, xn,2, xn,3, xn,4) f4(tn, xn,1, xn,2, xn,3, xn,4) 8.6. SYSTEMS OF ORDINARY DIFFERENTIAL EQUATIONS 143 k41 = f1 k42 = f2 k43 = f3 k44 = f4 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k31,xn,2 + 2k32,xn,3 + 2k33,xn,4 + 2k34 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k31,xn,2 + 2k32,xn,3 + 2k33,xn,4 + 2k34 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k31,xn,2 + 2k32,xn,3 + 2k33,xn,4 + 2k34 􏰆hhhhh􏰇 tn + 2,xn,1 + 2k31,xn,2 + 2k32,xn,3 + 2k33,xn,4 + 2k34 Generalization of RK-4 to a system of N dependent variables should be straight forward. 144 CHAPTER 8. ORDINARY DIFFERENTIAL EQUATIONS Runge−Kutta 3 2 1 0 −1 −2 −3 −5 −4 −3 −2 −1 0 1 2 Figure 8.6: Figure showing the stability region of the 2nd and 4th order Runge-Kutta methods. Solutions to selected exercises 145 146 Solution to Exercise 2.7 10.0 7.5 5.0 2.5 0.0 -3 -2 -1 0 1 2 3 4 -2.5 x Solution to Exercise 2.7 147 4.0 3.5 3.0 2.5 2.0 2.0 2.5 3.0 3.5 x g_1(x) x 4.0 148 Solution to Exercise 2.7 6 4 2 0 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 x -2 -4 -6 g_2(x) x Solution to Exercise 2.7 149 150 Solution to Exercise 2.7 40 30 20 10 0 2 4 6 8 10 x g_3(x) x Solution to Exercise 2.10 151 Maple worksheet to illustrate fixed point iteration scheme Define the function f ( x ) f := x/x2 K 2 x K 3 (NewtonRaphsonExample01.1) Plot f(x) to see how it looks like 10.0 7.5 5.0 2.5 0.0 -3 -2 -1 0 1 2 3 4 x -2.5 From the graph above, it is easy to see that the roots of the function f ( x ) is at x=-1 and x=3. Let's now see one can use the Newton Raphson. First we need to calculate the derivative of f(x). dfdx := x/2 x K 2 The Newton Raphson method is given by the following formula xiC1=xiK f0xi1 df 0xi1 (NewtonRaphsonExample01.2) (NewtonRaphsonExample01.3) Using this formula to iterate with the initial value x := 4 x := 3.166666667 dx (NewtonRaphsonExample01.4) 152 Solution to Exercise 2.10 x := 3.006410256 x := 3.000010240 x := 3.000000000 x := 3.000000000 x := 3.000000000 (NewtonRaphsonExample01.4) As you can clearly see, starting from x=4 will converge to the closest root at x=3. Plot graph close to the root to see if we can find out what is going on. 10.0 7.5 5.0 2.5 0.0 -2.5 2.0 2.5 3.0 3.5 x 4.0 4.5 5.0 Try to find root close to x=-2 x := K2 x := K1.166666667 x := K1.006410256 x := K1.000010240 (NewtonRaphsonExample01.5) (NewtonRaphsonExample01.6) Solution to Exercise 2.10 153 x := K1.000000000 x := K1.000000000 x := K1.000000000 (NewtonRaphsonExample01.6) As you can clearly see, starting from x=-2 will converge to the closest root at x=-1. Plot graph close to the root to see if we can find out what is going on. -3.0 -2.5 -2.0 -1.5 x -1.0 -0.5 0.0 10.0 7.5 5.0 2.5 0.0 -2.5 154 Solution to Exercise 3.3 I1 1K3L JM A := J3 1 4M (1) JM K2 0 1N Doolittle's method for LU decomposition requires that the [L] and [U] matrices be written as below for a 3x3 system Multiply the L and U matrices will give you IJ u11 temp := Jl21 u11 JKl u 31 11 u12 l21 u12 C u22 l u Cl u 31 12 32 22 u13 LM l21 u13 C u23 M (3) l u Cl u Cu MN 31 13 32 23 33 IJ 1 0 0 LM L:= Jl21 1 0 M JKl31 l32 1MN IJu11 u12 u13 LM U:= J 0 u22 u23 M (2) JK0 0 uMN 33 The product of [L] and [U] must be equal to [A]. So equate them and you will get IJ u 1 1 u 1 2 u 1 3 LM IJ 1 1 K 3 LM JluluCu luCuMJ M J2111 2112 22 J JK l u l u C l u 2113 23 M=J314M l u C l u C u M MN J JK 2 0 1 M MN 31 11 31 12 32 22 31 13 32 23 33 By comparing both sides of the above equations, you will have 9 equations with 9 unknowns. In order to solve for the 9 unknowns, you will need to follow the following sequence. Going across the first row will and compare with the [A] matrix will give you u11, u12 and u13 u11 := 1 u12 := 1 u13 := K3 (4) Going down the first column will give you l21 and l31 l21 := 3 l31 := 2 (5) Solution to Exercise 3.3 155 Going across the 2nd row will give you u22 and u23 u22 := K2 Going down the 2nd column will give you l32 Finally, comparing elements of the 3rd row and 3rd column of both sides will give ou u33 u23 := 13 (6) l32 := 1 (7) u33 := K6 (8) We have now found the [L] and [U] matrices using Doolittle's method I1 0 0L JM L= J3 1 0M JM K2 1 1N I1 1K3L JM U=J0 K2 13M (9) JM K0 0K6N Check to see if everything is OK by computing L*U-A. If LU=A, then LU-A=0. I0 0 0L JM LU K A = J0 0 0M (10) JM K0 0 0N 156 Solution to Exercise 3.4 I3L JM J2M JM J K15 M J 2 M J K5M K2N The third column of the inverse of A can be found by solving (3) IJ pK3C3qK2r LM J J J 2qCpK54 rK94K14s M M M b:= J 41617 15 5M (1) J 8 rC 8 K 2 pK 2 qC 8 sM J 1 sC17K1 pK5 qC13 rM K88228N The first column of the inverse of A can be found by solving I1 1 0 2LIaL I1L J MJMJM J2 5 2 J3 8 3 J K5 0 1 0MJbM J0M 1MJcM=J0M MJ M J M 3NKdN K0N In order to make the original equation look like the above, one would need to set p=2, q=2, r = 2 and s=1. Substitutingthesevaluesintobwillgiveyou I1L JM J1M JM J K7 M J 2 M J K1 M K2N The second column of the inverse of A can be found by solving I1 1 0 2LIaL I0L J MJMJM (2) J2 5 2 J3 8 3 J K5 0 1 0MJbM J1M 1MJcM=J0M MJ M J M 3NKdN K0N In order to make the original equation look like the above, one would need to set p=1, q=3, r = 2 and s=1. Substitutingthesevaluesintobwillgiveyou Solution to Exercise 3.4 157 I1 1 0 2LIaL I0L J MJMJM J2 5 2 0MJbM J0M J3 8 3 1MJcM=J1M J MJ M J M K5 0 1 3NKdN K0N In order to make the original equation look like the above, one would need to set p=1, q=2, r = 3 and s=1. Substitutingthesevaluesintobwillgiveyou IJ K 2 LM J K5 M J4M J 41M J 8 M J 13M K8N The fourth column of the inverse of A can be found by solving I1 1 0 2LIaL I0L J MJMJM J2 5 2 0MJbM J0M J3 8 3 1MJcM=J0M J MJ M J M K5 0 1 3NKdN K1N (4) In order to make the original equation look like the above, one would need to set p=1, q=2, r = 2 and s=2. Substitutingthesevaluesintobwillgiveyou IJ 0LM J K1 M J4M J 5M J 8M J 1M K8N Putting all the columns (Eqs. (2) - (5)) will give the inverse of A to be IJ 1 3 K2 0LM J 1 2 K5 K1M (5) J44M Ainv := JK7 K15 41 5M (6) J2 2 8 8M JK1 K5 13 1M K2288N 158 Solution to Exercise 3.4 Check to see if everything is OK by multiplying A*Ainv. This should give you the identity matrix. I1 0 0 0L JM J0 1 0 0M JM J0 0 1 0M J M K0 0 0 1N (7) Solution to Exercise 3.8 159 This worksheet outlines the steps one would take to solve the following system of equation xyz K x2 C y2 K 1.34 = 0 xy K z2 K 0.09 = 0 e x K e y C z K 0.41 = 0 Warning, the protected names norm and trace have been redefined and unprotected Warning, the name GramSchmidt has been rebound Define the functions The Jacobian, [J] is defined to be u ( x, y, z ) = x y z K x2 C y2 K 1.34 v ( x, y, z ) = x y K z2 K 0.09 w(x, y, z) =exKeyCzK0.41 IJvu vu vuLM Jvx vy vzM vyvzM Jvw vw vwM (1) [J]= Jvx Kvx vy vzN For this system [J] can be calculated to be IJyzK2x xzC2y xy LM [J] = J y x K2 zM JKex Key 1MN IJ x 0 LM IJ 1 LM The initial guess is Jy0 M = J1 M JzM JK1MN K0N Set up matrix and iterate. The system to solve at every iteration is of the form (2) Solving IJ vu 0xi,yi,zi1 J vx vu 0xi,yi,zi1 vy vu 0xi,yi,zi1 LMI L vz MJxiC1KxiM I Ju0xi,yi,zi1M vv vv MJy J MJKz L Jvx,y,z M Jvv Jvx0xi,yi,zi1 vy0xi,yi,zi1 vz0xi,yi,zi1MJiC1 KyM iM=KJ 0i i i1M Jvw0x,y,z1 vw0x,y,z1 vw0x, y,z1M iC1 Kvx i i i vy i i i vz i i i N K z MN JKw0x, y, z 1MN i i i i Jvv vv vvM 160 Solution to Exercise 3.8 gives Thus Solving gives Thus Solving K.103738540015165082 LM I J J JK 3.0425 2.4504 K2.9898 IJ x2 K x1 LM IJ Jy2Ky1M,"=", J I J J JK Jz M K2N K.7591 3.0578 JK0.950133771041813735MN 0.99275 L IJ x 3 K x 2 LM I 0 . 0 0 0 1 L I K 1 . J IJ x1 K x0 LM IJ Jy1Ky0M,"=", J J 1. JK2.7183 K2.7183 K10N 3 . 1 . L IJ x 1 K x 0 LM I 0 . 3 4 L M Jy Ky M J M 1. K2.M,J1 0M,"=",J0.09M 1.MN Jz Kz M JKK.59MN 0.0951802085692621258M Jz Kz M JKK0.0492791657229514693MN K10N IJ x1 LM IJ 0.896261459984834862 LM Jy1M,"=", J 1.09518020856926212M Jz M K1N JK0.950720834277048565MN 0.98158 L IJ x 2 K x 1 LM I 0 . 0 1 0 6 L K.7513 1.0952 0.89626 K1.9014 M, J 2 1 M, "=", J 0.01229 M 1.1003 M JyKyM J M 0.90225 K1.9003 M, J 3 2 M, "=", J K0. M M JyKyM J M 1.MN Jz Kz M JKK0.00132MN 0.00515167675034208187M Jz Kz M JKK0.000587063235234869426MN K21N IJ x2 LM IJ 0.902248052534380895 LM Jy2M,"=", J 1.10033188531960424M 2.4651 K3.0051 1.MN Jz Kz M JKK0.00013MN K32N K21N 0.00598659254954604301 LM Solution to Exercise 3.8 161 gives Thus Solving gives Thus Solving gives IJ x3 K x2 LM IJ K0.0000200216157804530296 LM Jy3Ky2M,"=", J 0.0000272899433490323116M Jz Kz M JK 0.00000136429381857177163MN K32N IJ x3 LM IJ 0.902228030918600488 LM Jy3M,"=", J 1.10035917526295335M I K.7590 J Jz M K3N 3.0580 0.90223 K3.0054 JK0.950135135335632319MN 0.99281 L IJx4Kx3LM I K0.0002L M JyKyM J M K1.9003 M, J4 3M,"=",JK0.00004M 1. MN Jz Kz M JK 0.00016MN K43N J 1.1004 JK 2.4651 I K.7590 J Jz M K4N 3.0578 JK0.950122438492582821MN 0.99271 L IJ x 5 K x 4 LM I 0 . 0 0 0 1 L IJ x4 K x3 LM Jy4Ky3M,"=", J K0.0000629366550639356396M IJ K0.00000667436618378504944 LM Jz Kz M JK K0.0000126968430495036220MN K43N IJ x4 LM IJ 0.902221356552416753 LM Jy4M,"=", J 1.10029623860788939M M JyKyM J M K54N IJx5Kx4LM IJ K0.0000104001665921580714LM Jy5Ky4M,"=", J 0.0000307533753822318448M Jz Kz M JKK0.00000194558097252621844MN K54N J 1.1003 0.90222 K1.9002 M, J 5 4 M, "=", J 0.00002 M JK 2.4651 K3.0051 1.MN Jz Kz M JKK0.00012MN 162 Solution to Exercise 3.8 Thus IJ x5 LM IJ 0.902210956385824602 LM Jy5M,"=", J 1.10032699198327166M Jz M JK0.950120492911610270MN K5N Solution to Exercise 5.12 163 Specifying the data points First define all the hi Then get all your ai Need to solve the tridiagonal system This gives Jc2M J JcM K3N IJ0LM J118M J47M J47M 3K0N Substituting into the formula for bi gives b0 := K95 Using for formula for di gives Data := [ [1, 2], [3, 4], [4, 9], [7, 2]] (1) I1 0 0 0LIJc0LM J2610MJc1M I 0L J12M J M, J M, "=", J M (4) JK22M JK0MN (5) (6) J0 1 8 3M JK0001MN JM Jc1M Jc M,"=", J2M JKcMN J M Ic0L h0 := 2 h1 := 1 h2:=3 a0 := 2 a1 := 4 a2 := 9 a3:=2 (2) (3) 141 b1 := 613 141 b2 := 535 141 JK144M 164 Solution to Exercise 5.12 d0:= 59 141 d1 := K262 141 d2 := 16 47 The cubic spline will consist of your piecewise polynomials (7) 1%x and x%3 3%x and x%4 (8) 4%x and x%7 O P 377 K 95 xC 59 (xK1)3 141 141 P 425 613 118 141 P S(x)= QK C R 2 262 3 (xK3) K (xK3) xC P K871 C 535 xK 144 (xK4)2C 16 (xK4)3 Plotting your spline and data points to see that everything is OK. P 47 141 47 141 141 141 47 47 Solution to Exercise 5.12 165 (7) 11 10 9 8 7 y6 5 4 3 2 1 1234567 x Data Cubic Spline 166 Solution to Exercise 5.13 Specifying the data points Data := [ [1, 1], [2, 1], [3, 0]] (1) Defining your piecewise polynomials "So(x)=", 1CP (xK1)CQ (xK1)3 "S1(x)=", 1Cp (xK2)C 34 (xK2)2Cq (xK2)3 (2) The cubic spline will consist of your picewise polynomials. Need to find P, Q, p and q to completely determine the cubic spline, S ( x ). O3 P 1CP (K1Cx)CQ (K1Cx) P 1%x and x%2 2%x and x%3 S(x) = Q3 (3) (4) (5) P1Cp (xK2)C R (xK2)2Cq (xK2)3 SpecifyingthatS00x11=S10x11andthatS10x21=0 gives 1CPCQ=1 74 C p C q = 0 4 Solution to Exercise 5.13 167 Specifying that S0 ' 0 x1 1 = S1 ' 0 x1 1 gives Specifying that S0 '' 0 x1 1 = S1 '' 0 x1 1 gives PC3Q=p (6) 6 Q = 32 Using Eqs. (4), (5), (6) and (7) to solve for P, Q, p and q gives abc := 4Q = 1 , P = K1 , p = 1 , q = K9 5 4424 p : = 12 q := K9 4 P := K1 4 Q : = 14 Plotting the spline and data points to make sure that everything is OK. ( 7 ) (8) ( 9 ) (10) (11) ( 1 2 ) 168 Solution to Exercise 5.13 2.0 1.8 1.6 1.4 1.2 y 1.0 0.8 0.6 0.4 0.2 0.0 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 x 2.6 2.8 3.0 Solution to Exercise 8.6 169 We are required to outline the numerical method to solve Let d2x + ω2x = 0. (8.40) dt2 x1 = x x2 = dx dt Thus Eq. 8.40 can be written as dx2 =−ω2x1. dt So the original problem can be written in terms of the new variables x1 and x2 as a system of ODE i.e. d􏰊x1􏰋 􏰈 0 1􏰉􏰊x1􏰋 = 2 (8.41) dtx2 −ω0x2 Applying Eulers method to Eq. (8.41) gives 1􏰌􏰊x1 􏰋n+1 􏰊x1 􏰋n􏰍 􏰈 0 1􏰉􏰊x1 􏰋n h x2 −=2 x2 −ω 0 x2 􏰊x1 􏰋n+1 􏰈1 0􏰉􏰊x1 􏰋n 􏰈 0 h􏰉􏰊x1 􏰋n x =01x+−ω2h0x 222 􏰊x1􏰋n+1 􏰆􏰈10􏰉􏰈0 h􏰉􏰇􏰊x1􏰋n x = 01+−ω2h0 x 22 􏰊x1􏰋n+1 􏰈 1 h􏰉􏰊x1􏰋n x =−ω2h1x (8.42) Equation (8.42) involves matrix-vector multiplication. It is given that at time t = 0, 22 􏰊 x1 􏰋0 􏰊 p 􏰋 x=0 So, using Eulers method, the estimated solution at at time t = nh can be written as 2 170 Solution to Exercise 8.6 􏰊x1􏰋n+1 􏰈 1 h􏰉n􏰊p􏰋 x =−ω2h1 0 2 If it is desired to solve Eq. (8.40) using Trapezoidal method, then 1􏰌􏰊x1 􏰋n+1 􏰊x1 􏰋n􏰍 1􏰈 0 1􏰉􏰊x1 􏰋n 1􏰈 0 1􏰉􏰊x1 􏰋n+1 􏰈 1 −h/2􏰉􏰊x1 􏰋n+1 ω2h/2 1 h x2 x2 2 −ω 0 x2 2 −ω 0 􏰈 1 h/2􏰉􏰊x1 􏰋n x2 −=2+2 = −ω2h/2 1 x 22 x 􏰊x1 􏰋n+1 1 􏰈 1 h/2􏰉􏰈 1 h/2􏰉􏰊x1 􏰋n x = 1+ω2h2/4 −ω2h/2 1 −ω2h/2 1 x 22 􏰊x1 􏰋n+1 1 􏰈1−ω2h2/2 h 􏰉􏰊x1 􏰋n x = 1+ω2h2/4 −ω2h 1−ω2h2/2 x 22 Bibliography [1] R. Burden and J. Faires. Numerical Analysis. Thomson, 1997. [2] S. Chapra and R. Canale. Numerical Methods for Engineers. McGraw-Hill, 2002. 171