CHEE 222: PROCESS DYNAMICS AND NUMERICAL METHODS Winter 2017 Implementation of Numerical Methods via MATLAB Instructor: Xiang Li
1 Outline 1. Introduction - Command, script and function - MATLAB function for mathematical functions 2. Programming Flow Control - IF-ELSE statement: Conditional commands - FOR statement: Repetitive commands - WHILE statement: Repetitive commands - Logical expressions: Description of conditions 3. Example Implementations - Direct substitution - Newton s method - Explicit Euler method
1. Introduction 2 Why MATLAB? MATLAB has become a standard tool for almost all scientific and engineering disciplines. MATLAB has become more and more popular in the R&D in industry. For CHEE 222: You don t have to perform tedious calculation by hand, and you can solve complicated problems with the principles learned in the course. Will be used again in: CHEE 311, CHEE 319, CHEE 321, CHEE 434, CHEE 412, CHEE 418
1. Introduction 3 References Moler, C. Numerical Computing with MATLAB, The MathWorks, Inc., Natick, MA (2004). (Electronic version: http://www.mathworks.com/moler) MATLAB help files Hudon, N, DeHaan, D., and Guay, M. Introduction to MATLAB. Department of Chemical Engineering, Queen s University (2006). - This MATLAB guide is available on D2L as HDG2006.pdf - Part of this presentation is prepared based on this guide.
1. Introduction Prompt line command Straightforward way to implement an operation 4 Prompt line Where your type your command Current folder Place the MATLAB file you want to call in the current folder. Workspace Existing variable are stored here.
1. Introduction 5 One to several commands in a single prompt line
1. Introduction 6 Script file A batch of commands
1. Introduction Function file A batch of commands with inputs and outputs 7 Function name should be the file name!
1. Introduction 8 Function file A batch of commands with inputs and outputs
1. Introduction 9 Function file A batch of commands with inputs and outputs
1. Introduction 10 Row Vector, Colum Vector, Matrix Math MATLAB a = ( 1 3 6 ) Approach 1 Approach 2 a = [ 1 3 6 ]; a(1) = 1; a(2) = 3; a(3) = 6; b = 2 5 8 Approach 1 Approach 2 b = [ 1; 3; 6 ]; b(1) = 2; b(2) = 5; b(3) = 8; b = b ; A = 1 2 3 4 5 6 Approach 1 Approach 2 A = [ 1 2 3; 4 5 6 ]; A(1,:) = [ 1 2 3 ]; A(2,:) = [ 4 5 6 ];
1. Introduction 11 MATLAB function as f(x) in algebraic equations (1) absin(x 1 ) + cos(x 2 ) = 0, a ln(x 1 ) + e b2 (x 1 +x ) 2 = 0, a = 1, b = 0. f(x) = 0 where x = x 1 x 2, f(x) = absin(x 1 ) + cos(x 2 ) a ln(x 1 ) + e b2 (x 1 +x 2 ), a = 1, b = 0.
1. Introduction MATLAB function as f(x) in algebraic equations (2) 12 f(x) = absin(x 1 ) + cos(x 2 ) a ln(x 1 ) + e b2 (x 1 +x 2 ) where a = 1, b = 0.5. To solve f(x) = 0 with x 0 = (1.2, 1.4), type in MATLAB prompt (command) line: or
1. Introduction MATLAB function as f(x) in algebraic equations (3) 13 f(x) = absin(x 1 ) + cos(x 2 ) a ln(x 1 ) + e b2 (x 1 +x 2 ) where a = 1, b = 0.5. To solve f(x) = 0 with x 0 = (1.2, 1.4), type in MATLAB prompt (command) line:
1. Introduction MATLAB function for f(t,x) in ODEs x = f(t, x) = absin(x 1 ) + cos(x 2 ) a ln(x 1 ) + e b2 (x 1 +x 2 ) where a = 1, b = 0.5. if MATLAB built-in ode solvers (e.g., ode45) are to be used for solution, the ODE needs to be represented in this form no matter whether time t is present in the right-hand-sides of the equations. 14 To solve x=f(t, x) for time period [0, 0.5] with x(0) = (3, 4), type in prompt line: or
1. Introduction Some MATLAB built-in functions for math functions Function Description sin(x) sine of x; x in radians cos(x) cosine of x; x in radians tan(x) tangent of x; x in radians sec(x) secant of x; x in radian csc(x) cosecant of x; x in radian cot(x) cotangent of x; x in radian asin(x) arc sine of x sinh(x) hyperbolic sine of x asinh(x) inverse hyperbolic sine of x exp(x) exponential of x log(x) natural logarithm of x log10(x) base 10 logarithm of x sqrt(x) square root of x power(x,a) x to the power of a (equivalent to x^a) abs(x) absolute value of x round(x) round x towards nearest integer floor(x) round x towards minus infinity ceil(x) round x towards infinity fix(x) round x towards zero 15
2. Programming Flow Control for Conditional and Repetitive Commands 16 IF-ElSE statements Conditional commands CONDITION is expressed via logical operations. ACTION may involve one or several commands. The math function x can be computed via the following MATLAB function myabs that involves conditional commands:
2. Programming Flow Control for Conditional and Repetitive Commands 17 FOR statements Repetitive commands (1) After the execution of the FOR loop, the value of y becomes 5. After the execution of the FOR loop, the value of vector y becomes [1, 0, 0, 0, 25, 0, 0, 0, 81].
2. Programming Flow Control for Conditional and Repetitive Commands 18 FOR statements Repetitive commands (2) Using FOR statements for computing s = 20 i=1 x i
2. Programming Flow Control for Conditional and Repetitive Commands 19 WHILE statements Repetitive commands (1) Using WHILE statements for computing s = 20 i=1 x i
2. Programming Flow Control for Conditional and Repetitive Commands 20 WHILE statements Repetitive commands (2) Using WHILE statements for computing s = 20 i=1 x i
2. Programming Flow Control for Conditional and Repetitive Commands 21 Logical expressions Description of conditions Logical operators assess conditions and return Boolean values (1/0 or True/False).
3. Nonlinear Equation System 22 Direct Substitution Method (Fixed-Point Method) (1) Initialization: Reformulate equation f (x) = 0 as x=g(x). Give an initial guess x 0 and convergence tolerance ε, set k = 0. (2) Iteration k+1: Compute the next candidate solution x k+1 = g(x k ). (3) Termination Check: If x k+1 x k ε, return x k+1 as the numerical solution; Otherwise, k = k +1, and go to (2).
3. Example Implementations 23 Direct substitution Implementation via script or function To start the solution procedure, type in prompt line: To start the solution procedure, type in prompt line:
3. Example Implementations 24 Newton s method (1) Initialization: Give a guess x 0 and tolerances ε,ε a,ε r, set k = 0. (2) Iteration k+1: Compute f(x k ), J k, and x k+1 = x k J 1 k f(x k ). (3) Termination Check: If f(x k ) ε or x k+1 x k x k ε r +ε a, return x k+1 as the numerical solution; Otherwise, k = k +1, and go to (2). Solution information Algorithm initial condition
3. Example Implementations 25 Newton s method Example of function and Jacobian evaluation f(x) = absin(x 1 ) + cos(x 2 ) a ln(x 1 ) + e b2 (x 1 +x 2 ), where a = 1, b = 0.5. J = a x 1 abcos(x 1 ) sin(x 2 ) + b 2 e b 2 (x 1 +x 2 ) b 2 e b2 (x 1 +x 2 ), where a = 1, b = 0.5.
3. Example Implementations 26 Newton s method MATLAB command for execution If initial guess x 0 = x 1,0 x 2,0 = 0.2 2 then the MATLAB command is:, tolerances: ε = 10 6, ε a = 10 6, ε r = 10 6, Note: MATLAB m-files Newton.m, fun.m, Jfun.m need to be included in the current folder for execution of the command!
3. Example Implementations 27 Newton s method Alternative implementation (1) Initialization: Give a guess x 0 and tolerances ε,ε a,ε r, set k = 0. (2) Iteration k+1: Compute f(x k ), J k, and x k+1 = x k J 1 k f(x k ). (3) Termination Check: If f(x k ) ε or x k+1 x k x k ε r +ε a, return x k+1 as the numerical solution; Otherwise, k = k +1, and go to (2).
3. Example Implementations 28 Explicit Euler - From algorithm to a MATLAB function x = f(x), x(0) = x 0 (1) Initialization: Give t 0 (= 0), t f, Δt, set k = 0. (2) Step k +1: m(k) = f(x(k)), x(k +1) = x(k) + m(k)δt (3) Termination Check : If k +1 = n(= t f / Δt), terminate and return [x(1)... x(n)] T ; Otherwise, set k = k +1, go to (2).
3. Example Implementations 29 Explicit Euler - Example of search direction evaluation x 1 = absin(x 1 ) + cos(x 2 ), x 2 = a ln(x 1 ) + e b2 (x 1 +x 2 ), where a = 1, b = 0.5.
3. Example Implementations 30 Explicit Euler - MATLAB command for execution If time period [t 0, t f ] = [0,1], time step Δt = 0.1, initial condition x(0) = x 1 (0) x 2 (0) = 1 1, then the MATLAB command is: Note: MATLAB m-files Euler.m, odefun.m need to be included in the current folder for execution of the command!
3. Example Implementations 31 Explicit Euler - Alternative implementation x = f(x), x(0) = x 0 (1) Initialization: Give t 0 (= 0), t f, Δt, set k = 0. (2) Step k +1: m(k +1) = f(x(k)), x(k +1) = x(k)+ m(k)δt (3) Termination Check : If k +1 = n(= t f / Δt), terminate and return [x(1)... x(n)] T ; Otherwise, set k = k +1, go to (2).