Numerical Methods for Ordinary Differential Equations
Total Page:16
File Type:pdf, Size:1020Kb
CHAPTER 1 Numerical Methods for Ordinary Differential Equations In this chapter we discuss numerical method for ODE . We will discuss the two basic methods, Euler’s Method and Runge-Kutta Method. 1. Numerical Algorithm and Programming in Mathcad 1.1. Numerical Algorithm. If you look at dictionary, you will the following definition for algorithm, 1. a set of rules for solving a problem in a finite number of steps; 2. a sequence of steps designed for programming a computer to solve a specific problem. A numerical algorithm is a set of rules for solving a problem in finite number of steps that can be easily implemented in computer using any programming language. The following is an algorithm for compute the root of f(x) = 0; Input f, a, N and tol . Output: the approximate solution to f(x) = 0 with initial guess a or failure message. ² Step One: Set x = a ² Step Two: For i=0 to N do Step Three - Four f(x) Step Three: Compute x = x ¡ f 0(x) Step Four: If f(x) · tol return x ² Step Five return ”failure”. In analogy, a numerical algorithm is like a cook recipe that specify the input — cooking material, the output—the cooking product, and steps of carrying computation — cooking steps. In an algorithm, you will see loops (for, while), decision making statements(if, then, else(otherwise)) and return statements. ² for loop: Typically used when specific number of steps need to be carried out. You can break a for loop with return or break statement. 1 2 1. NUMERICAL METHODS FOR ORDINARY DIFFERENTIAL EQUATIONS ² while loop: Typically used when unknown number of steps need to be carried out. You can break a while loop with a break statement or a return statement. ² if – else(otherwise): Used when condition(s) must meet be- fore carrying out some steps. The else(otherwise) provide al- ternative steps if any. 1.2. Programming in Mathcad. To translate a numerical al- gorithm into a program in Mathcad , you need to define a function name with function arguments and function body. The function name can be any sequence of alphabetic letter begin with an English letter, such any ”myFunction”, ”Myfunction1” etc. The function ar- guments specify the input to the function, typically the items specified in the input statement of an algorithm and is an comma(,) separated list enclose by () after function name, such as myFunction(f, a, N, tol), myOne(a,b). Notice the function argument is optional, you can define a function with out argument as myFunction(). The function body implements the step section of an algorithm and the output statement. In Mathcad the function body is on the right side of := with vertical bars as grouping symbol. The entire function body is considered as a group, inside, it might has many subgroups. For the algorithm of finding f(x) = 0, there is an subgroup that contains step three and four, as shown in the following screen shot, the screen shot also shows how to get the programming toolbar from the math toolbar. Figure 1. mySolver and Programming toolbar 1. NUMERICAL ALGORITHM AND PROGRAMMING IN Mathcad 3 To enter code in Mathcad , (1) First enter the function name with arguments list and assignment operator :=, with sequence of typing, mySolver(f,a,N,tol): (2) Click the Add Line button or press ] to get a vertical bar and two (you should add more by press ] several times). (3) enter x and hold [Shift] type ] to get the local assignment à or click on the programming toolbar, at the type a. Notice x à a reads as ”assign a to x.” (4) Click for on the programming menu or [Shift][Ctrl][’] to get for 2 in after for enter i, in after 2, enter ”1;N”, and in under for, press left bracket ] to get a vertical line and several . (5) To get if operator, you need either click on the toolbar or press [Shift][Ctrl][ ] ]. you will get if enter condition in after if and other statements in before if. (6) To get return operator, you need either click on the toolbar or press [Shift][Ctrl][ ], you get return in type any information you want to return. After defining a function, we call it with concrete input data, as shown in the screen shot, that call the mySolver function to find zero for both f(x) = x2 ¡1; with initial guess x = 2 and g(t) = t3 ¡3t2 ¡2t; with initial guess t = ¡2: Notice there is two ways to call a function, one way is to define all arguments before calling the function, another is to define some arguments and call the function with concrete values for the undefined arguments. As in all programming language, Mathcad allows you to call one function from within another. For example, we could create a function called myDerivative which will compute derivative of a given func- tion f(x) at a given number x and step size h > 0 using the forward 0 f(x+h)¡f(x) difference formula: f (s) ¼ h : The following screen shot shows the call of myDerivative inside mySolver, Figure 2. Call another function 4 1. NUMERICAL METHODS FOR ORDINARY DIFFERENTIAL EQUATIONS The algorithm for this function is very simple (here, we break down the computation in three steps), Input f, x, and h . Output: the approximation to f 0(x). ² Step One: Set w = f(x + h) ² Step Two: Set d = f(x) w¡d ² Step Three: return h 2. Euler’s Method 2.1. Euler’s Method. Euler’s method is the simplest method in find approximate solutions to first order equations. From the forward difference formula f(x + h) ¡ f(x) f 0(x) ¼ ; h we have (1) f(x + h) ¼ f(x) + f 0(x)h Now if x0(t) = f(t; x) is a first order differential equations, apply (1), we have x(t + h) ¼ x(t) + f(t; x(t))h: Suppose we want to find approximate solution over interval [a, b] with initial value x(a) = x0, we divided the interval into n subintervals b¡a each with length h = n ; the ends of the subintervals is t0 = a; t1 = a + h; t2 = a + 2h; ¢ ¢ ¢ ; tn = a + nh = b: Start with x0 we can compute x1 = x0 + f(t0; x0)h x2 = x1 + f(t1; x1)h . .xn = xn¡1 + f(tn¡1; xn¡1)h Example 2.1. Find approximate to x(1) if x0(t) = t2¡ex sin(t); x(0) = 1 with h = 0:25: Solution Here the interval is [0, 1], so a = 0; b = 1: Since h = b¡a n = 0:25 we have n = 4 and we need to compute x1; x2; x3; x4 starting with x0 = x(0) = 1: x1 = x0 + f(t0; x0)h = 1 + f(0; 1) ¤ 0:25 = 0:15853 x2 = x1 + f(t1; x1)h = 0:15853 + f(0:25; 0:15853) ¤ 0:25 = 0:01833 x3 = x2 + f(t2; x2)h = 0:01833 + f(0:5; 0:01833) ¤ 0:25 = 0:23811 x4 = x3 + f(t3; x3)h = 0:23811 + f(0:75; 0:15853) ¤ 0:25 = 0:30128 So the approximation to x(1) is x(1) ¼ x4 = 0:30128: Also x(0:25) ¼ x1 = 0:15853; x(0:5) ¼ x2 = 0:01833; x(0:75) ¼ x3 = 0:23811: a 2. EULER’S METHOD 5 2.2. Mathcad implementation of Euler’s Method. First, we have the following simple algorithm for the Euler’s method, Input f, a, b, x0 n: Output: the approximate solution to x0 = f(t; x) with initial guess x0 over interval [a, b]. ² Step One: Initialization b¡a Set h = n Set x0 = x0 Set t0 = a ² Step Two: For i=1 to n do Step Three Step Three: Set xi = xi¡1 ¡ f(ti¡1; xi¡1) ¤ h Set ti = ti¡1 + h ² Step Four return x. Notice, algorithm return an array of values, the ith element of the return array is an approximations of x(t) at t = a + ih: The following screen shot shows Mathcad code for implementing the algorithm. Notice, we also create a function tMesh(a, b, N) to compute the ending of subinterval for graphing purpose. Figure 3. Mathcad code for Euler’s Method Notice the line to line corresponding between the Mathcad and the algorithm. Since Mathcad programming language is a scripting lan- guage, the translation between algorithm and code is straight forward, and you don’t need to worry about the variable type, io, etc. Also, without explicit return statement, the result of last is, by default, will be returned. 6 1. NUMERICAL METHODS FOR ORDINARY DIFFERENTIAL EQUATIONS The next screen shot shows a call to the myEuler and tMesh for the equation x0 = t2x + t2sin(t3). It also shows the graph of approxi- 3 3 mate solution comparing with the exact solution x(t) = ¡ 10 cos(t ) ¡ 1 3 1 3 3 10 t 10 sin(t ) + 10 e Figure 4. Mathcad Euler’s Approximation to x0 = t2x + t2sin(t3) 2.3. Error analysis of Euler’s Method. There are two source of error in every numerical method used to approximate a solution of x0(t) = f(t; x), ² Local Truncation Error ² The roundoff error. The local truncation error is due to the method and roundoff error is due to computer that is used. For Euler’s method, we use xi = xi¡1 + f(ti¡1; xi¡1) ¤ h to approximate the value of x(ti), the difference, assuming no rounding is introduced, is jxi ¡ x(ti)j; and is called the local truncation error.