Numerical Analysis Chapter 1 Numerical Analysis and Programming 1.1 What is Numerical Analysis? Knowledge of numerical analysis is required to solve mathematical problem efficiently by computer. The main subjects of Numerical Analysis • Computation of equations and functions • Mathematical model (differential equations, etc.) solution techniques 1. Computation of Numerical Expressions Ex: y = x2+1 and y = sin x Problem: to find y at the given x point. First equation is easy to solve by standard algebraic methods Second one should be expanded in Taylor series: Here n is infinitely large, but we need to limit it by finite number to use in practice. 2. Mathematical model solution techniques To solve a technical problem, first of all we must describe the problem mathematically using mathematical equations, i. e. formulate it The model mathematically formulated becomes a mathematical model • may include algebraic equation, system of ordinary algebraic equations, ordinary differential equation, partial differential equations, integral equations etc. Sometimes a mathematical model can be solved analytically, but sometimes not f(x) = x2 + 3x + 1 = 0 : can easily be solved by standard algebraic analytical methods f(x) = x5 + 3x4 + x3 + x2 –5x +1 : the solution cannot be found analytically. In such cases the problem can and must be solved by numerical methods. 1.2 General Idea of Numerical Calculation Numerical calculation is carried out using consecutive series of calculations - algorithm. In algorithm of numerical calculation the following is required: • Calculation accuracy • Calculation efficiency • Numerical stabilization • Correspondence to all the predicted possibilities • Simple programming • Storage capacity of usual calculator is much less 1.2.1 Successive Approximation Successive approximation replaces a continuous matter with discrete quantity. The analysis in the neighborhood of a limit is an important basic concept of numerical analysis. Ex. 1.1 The integral of the function y = f(x): (Δxi →0) Analytical solution either does not exist, or is too complicated Rectangular area of function f(xi) with approximate heights Δxk = (b-a)/k for big enough k: Figure 1.1 Integral calculus concept by successive approximation Ex. 1.2 ordinary derivative of the function y=f(x) is defined as Its approximation around enough small Δxk can be calculated as Δxk decreases simply; discrete series Dk (k=1,2..) is the approximation of continuous series dy/dx. Figure 1.2 Differentiation concept by successive approximation There can be some difficult situations because of numerical uncertainty when looking for exact differential or integral solution series convergence becomes important issue Cauchy's Convergence Test If for any ε there are some numbers n, m such that n>k, m>k with the proper choice of k, so that |an – am|<ε, then the infinite sequence converges. Taylor Series Function f(x) can be differentiated n times at x=x0. The convergent infinite series of this function: (1.6) The coefficients ak (k=1,2..) can be found by differentiating over and over again as follows: Substituting x=x0 …… Taylor Series (cont.) After substituting ak , eq. (1.6) becomes : Taylor expansion of the function f(x) in the neighborhood of x=x0 cutting to n terms : truncated Taylor series (nth Taylor polynomial for f about x0) : truncation error (remainder term associated with Sn(x)) (x*€(x,x0) ) lim Sn(x) with n→∞ is called the Taylor series for f(x) about x0. x0 = 0 : Maclaurin polynomial (Maclariun series). The term truncation error refers to the error involved in using a truncated, or finite, summation to approximate the sum of an infinite series. 1.2.2 Iteration Method {fk} = {f0,f1,…,fn}; f0 is the initial term When each following term fk is expressed through already known values which are depending on the initial term, this is called a recursive formula. The use of iteration method improves calculation efficiency and decreases the computation time. Ex. 1.3 Calculate the sum of {ak} series The nth sum: Relation between Sk and Sk-1 can be calculated in the following iterative way: Starting from S0=a0 and repeating the iterations n times, Sn is calculated (Fig. 1.3) Figure 1.3 The flowchart for finding the sum of series by iteration method Ex. 1.4 Calculate n-dimensional polynomial e nth sum: To find fn(x0), its value is sought in the following sequence: To apply iteration method, a polynomial is expressed as follows: By applying the above recursive formula, fn can be calculated through n multiplications and n additions, which increases the efficiency of computation. This method is called Horner or Nesting method. 1.3 Numerical analysis sequence When a problem should be solved numerically, it is generally handled in the following order: • Presentation of problem and formulation of mathematical model • Discovering general equations and solving method. • Construction of algorithm • Construction of computation flowchart • Writing a code of computation program • Computation execution • Investigation of computation results Let’s consider the above on the example of finding the inverse of 2-dimensional matrix. (1) Presentation of a problem and formulation of mathematical model Problem: Real matrix 2x2 A = {aij} is given. Find the inverse. Using the matrix equation (2) Discovering general equations and solving method. In this case, the equation for 2-dimensional reverse matrix is (3) Construction of algorithm The sequence of computations should be written very clearly in order to avoid vagueness. When det A = 0, A-1 does not exist the computation is impossible countermeasure is necessary <Algorithm of finding the inverse of 2x2 matrix> Step 1. Data input • Read element of matrix A: a11, a12, a21, a22 Step 2. Calculation of the determinant • d ← a11a33 – a12a21 Step 3. Error management • If d=0, output the message "The inverse is 0" and break the execution. Step 4. Calculation of the inverse • b11 ← a22/d b12 ← -a12/d b21 ← -a21/d b22 ← a11/d Step 5. Results output • Output of elements of matrix A-1: b11, b12, b21, b22 (4) Construction of computation flowchart Flowchart: when the computation sequence is drawn schematically Flowchart helps to grasp the general flow of the program, and helps exterior users to understand the structure of the program See Fig. 1.4 and 1.5 symbol meaning data (inputs/outputs) data which is not parameters (materials or components); denotes either input or output operation process denotes different processes to be carried out (such as operations with data) decision checks a condition before carrying on; denotes a branch the program should continue along after checking the condition. Figure 1.4 Symbols of computational flowchart preparation denotes order or corresponding equation in order to affect the following action. predefined process denotes order management or operation of more than one order defined in a different subroutine or module line denotes data flow or general flow of the program. connector denotes entrance to different parts or exit from different parts in the identical flowcharts; also is used as a connector to another place if placed in the middle of the line terminator denotes entrance from the outside or exit to the outside environment display denotes any kind of data needed by user to express some information Figure 1.5 Flowchart of the procedure for finding inverse (5) Writing a code of computing program Programming: writing a code of the program basing upon flowchart Many different programming languages (here: FORTRAN and C) 4 main parts composing a program: 1. Variables definition part 2. Data input part 3. Computing part 4. Results output part Modules: • FORTRAN: subroutine/external function • C: function (6) Computation execution For input data: a11=1.0, a12=2.0, a21=2.0, a22=3.0, the result is : Figure 1.6 Results of program execution (7) Investigation of computational results If the desirable results were not obtained, the program sequence should be carefully reconsidered; after finding out the error reason it should be modified and the program should be executed again 1.4 Programming A numerical program is called good, if 1. Number of arithmetic operations (comparing with run-time) is small 2. The computation is accurate 3. Every step in the program is small enough 4. It is easy to read 5. It can be used easily in any computational unit 1.4.1 Program That is Easy to Understand There are two ways of making a program easy to understand (Fig. 1.7) 1. Splitting to separated functions (modules) 2. Layer structurization Figure 1.7 Splitting the program into modules 1.4.2 Structural Programming Figure 1.8 General control structures