Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
CHEE 222: PROCESS DYNAMICS AND NUMERICAL METHODS
Winter 2017
Implementation of Numerical Methods via MATLAB
Instructor: Xiang Li
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
Why MATLAB?
1. Introduction • 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 …
2
References
1. Introduction • 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.
3
Prompt line command – Straightforward way to implement an operation
1. Introduction
Prompt line – Where your type your command
Current folder – Place the MATLAB file you want to call in the current folder.
Workspace – Existing variables are stored here.
4
One to several commands in a single prompt line 1. Introduction
5
Script file – A batch of commands 1. Introduction
6
Function file – A batch of commands with inputs and outputs 1. Introduction
Function name should be the file name!
7
Function file – A batch of commands with inputs and outputs 1. Introduction
8
Function file – A batch of commands with inputs and outputs 1. Introduction
9
Row Vector, Colum Vector, Matrix 1. Introduction
10
Math MATLAB
a = ( 1 3 6 )
b =258
⎛
⎝
⎜⎜⎜
⎞
⎠
⎟⎟⎟
A = 1 2 3
4 5 6⎛
⎝⎜⎞
⎠⎟
a = [ 1 3 6 ];
b = [ 1; 3; 6 ];
a(1) = 1; a(2) = 3; a(3) = 6;
b(1) = 2; b(2) = 5; b(3) = 8; b = ′b ;
A = [ 1 2 3; 4 5 6 ];
A(1,:) = [ 1 2 3 ]; A(2,:) = [ 4 5 6 ];
Approach 1
Approach 2
Approach 1
Approach 2
Approach 1
Approach 2
MATLAB function as “f(x)” in algebraic equations (1) 1. Introduction
11
absin(x1)+ cos(x2 ) = 0,
a ln(x1)+ eb2 (x1+x2 ) = 0,
a = 1,b = 0.
where x =x1
x2
⎛
⎝⎜
⎞
⎠⎟ , f(x) =
absin(x1)+ cos(x2 )
a ln(x1)+ eb2 (x1+x2 )
⎛
⎝⎜⎜
⎞
⎠⎟⎟
,
a = 1, b = 0.
f(x) = 0
MATLAB function as “f(x)” in algebraic equations (2) 1. Introduction f(x) =
absin(x1)+ cos(x2 )
a ln(x1)+ eb2 (x1+x2 )
⎛
⎝⎜⎜
⎞
⎠⎟⎟
where a = 1, b = 0.5.
To solve f(x) = 0 with x0 = (1.2, 1.4), type in MATLAB prompt (command) line:
12
or
MATLAB function as “f(x)” in algebraic equations (3) 1. Introduction f(x) =
absin(x1)+ cos(x2 )
a ln(x1)+ eb2 (x1+x2 )
⎛
⎝⎜⎜
⎞
⎠⎟⎟
where a = 1, b = 0.5.
To solve f(x) = 0 with x0 = (1.2, 1.4), type in MATLAB prompt (command) line:
13
MATLAB function for “f(t,x)” in ODEs 1. Introduction x = f(t,x) =
absin(x1)+ cos(x2 )
a ln(x1)+ eb2 (x1+x2 )
⎛
⎝⎜⎜
⎞
⎠⎟⎟
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.
To solve x=f(t,x) for time period [0, 0.5] with x(0) = (3, 4), type in prompt line:
14
or
Some MATLAB built-in functions for math functions
1. Introduction
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
IF-ElSE statements – Conditional commands 2. Programming Flow Control for Conditional and Repetitive 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:
16
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
17
FOR statements – Repetitive commands (2)
Using FOR statements for computing s = xii=1
20
∑
2. Programming Flow Control for Conditional and Repetitive Commands
18
WHILE statements – Repetitive commands (1) 2. Programming Flow Control for Conditional and Repetitive Commands
19
Using WHILE statements for computing s = xii=1
20
∑
WHILE statements – Repetitive commands (2) 2. Programming Flow Control for Conditional and Repetitive Commands
20
Using WHILE statements for computing s = xii=1
20
∑
Logical expressions – Description of conditions
Logical operators assess conditions and return Boolean values (1/0 or True/False).
2. Programming Flow Control for Conditional and Repetitive Commands
21
Direct Substitution Method (Fixed-Point Method)
3. Nonlinear Equation System (1) Initialization:
Reformulate equation f (x) = 0 as x=g(x).Give an initial guess x0 and convergence tolerance ε , set k = 0.
(2) Iteration k+1: Compute the next candidate solution xk+1 = g(xk ).
(3) Termination Check: If | xk+1 − xk |≤ ε ,
return xk+1 as the numerical solution; Otherwise,
k = k +1, and go to (2).
22
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:
23 3. Example Implementations
Newton’s method 3. Example Implementations (1) Initialization: Give a guess x0 and tolerances ε,εa,εr, set k = 0.
(2) Iteration k+1: Compute f(xk ), Jk, and xk+1 = xk − Jk
−1f(xk ).
(3) Termination Check: If || f(xk ) ||≤ ε or || xk+1 − xk ||≤|| xk ||εr +εa, return xk+1 as the numerical solution; Otherwise, k = k +1, and go to (2).
24
Algorithm initial conditions
Solution information
Newton’s method – Example of function and Jacobian evaluation
f(x) =absin(x1)+ cos(x2 )
a ln(x1)+ eb2 (x1+x2 )
⎛
⎝⎜⎜
⎞
⎠⎟⎟,
where a = 1, b = 0.5.
J =abcos(x1) −sin(x2 )
ax1
+ b2eb2 (x1+x2 ) b2eb
2 (x1+x2 )
⎛
⎝
⎜⎜⎜
⎞
⎠
⎟⎟⎟
, where a = 1, b = 0.5.
3. Example Implementations
25
Newton’s method – MATLAB command for execution
Note: MATLAB m-files “Newton.m”, “fun.m”, “Jfun.m” need to be included in the current folder for execution of the command!
If initial guess x0 =x1,0
x2,0
⎛
⎝⎜⎜
⎞
⎠⎟⎟= 0.2
2⎛⎝⎜
⎞⎠⎟
, tolerances: ε = 10−6, εa = 10−6, ε r = 10−6,
then the MATLAB command is:
3. Example Implementations
26
Newton’s method – Alternative implementation (1) Initialization: Give a guess x0 and tolerances ε,εa,εr, set k = 0.
(2) Iteration k+1: Compute f(xk ), Jk, and xk+1 = xk − Jk
−1f(xk ).
(3) Termination Check: If || f(xk ) ||≤ ε or || xk+1 − xk ||≤|| xk ||εr +εa, return xk+1 as the numerical solution; Otherwise, k = k +1, and go to (2).
3. Example Implementations
27
Explicit Euler - From algorithm to a MATLAB function
(1) Initialization: Give t0 (= 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).
x = f(x), x(0) = x0
3. Example Implementations
28
Explicit Euler - Example of search direction evaluation
x1 = absin(x1)+ cos(x2 ),
x2 = a ln(x1)+ eb2 (x1+x2 ),
where a = 1, b = 0.5.
3. Example Implementations
29
Explicit Euler - MATLAB command for execution
Note: MATLAB m-files “Euler.m”, “odefun.m” need to be included in the current folder for execution of the command!
If time period [t0, t f ]= [0,1], time step Δt = 0.1,
initial condition x(0) =x1(0)x2 (0)
⎛
⎝⎜
⎞
⎠⎟ =
11
⎛⎝⎜
⎞⎠⎟
,
then the MATLAB command is:
3. Example Implementations
30
Explicit Euler - Alternative implementation
(1) Initialization: Give t0 (= 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).
x = f(x), x(0) = x0
3. Example Implementations
31