tailieunhanh - Essential MATLAB for Engineers and Scientists-Lecture 3: Creating Mfiles

Basic elements of program Input – Initialize, define or assign numerical values to variables. Set of command expressions – Operations applied to input variables that lead to the desired result. Output – Display (graphically or numerically) example of technical computinAn example of technical computing Let us consider using the hyperbolic tangent to model a downhill section of a snowboard or snow ski facility. Let us first examine the hyperbolic tangent function by executing the command: ezplot( ‘tanh(x)’ ). | Lecture 3 Creating M-files Programming tools: Input/output (assign/graph-&-display) Repetition (for) Decision (if) © 2007 Daniel Valentine. All rights reserved. Published by Elsevier. Review Arrays List of numbers in brackets A comma or space separates numbers (columns) A semicolon separates row Zeros and ones Matrices: zeros() ones() Indexing (row,column) Colon Operator: Range of Data first:last or first:increment:last Manipulating Arrays & Matrices Transpose Input Examples of input to arrays: Single number array & scalar: 1 × 1 >> a = 2 Row array & row vector: 1 × n >> b = [1 3 2 5] Column array & column vector: n x 1 >> b = b’ % This an application of the transpose. Array of n rows x m columns & Matrix: n × m >> c = [1 2 3; 4 5 6; 7 6 9] % This example is 3 x 3. Basic elements of a program Input Initialize, define or assign numerical values to variables. Set of command expressions Operations applied to input variables that lead to the desired result. Output Display (graphically or numerically) result. An example of technical computing Let us consider using the hyperbolic tangent to model a down-hill section of a snowboard or snow ski facility. Let us first examine the hyperbolic tangent function by executing the command: >> ezplot( ‘tanh(x)’ ) We get the following graph: Ezplot(‘tanh(x)’) Hyperbolic tangent: tanh(X) Properties (as illustrated in the figure): As X approaches –Inf, tanh(X) approaches -1. As X approaches Inf, tanh(X) approaches +1. X = 0, tanh(X) = 0. Transition from -1 to 1 is smooth and most rapid (roughly speaking) in the range -23. Thus, for –3 Review Arrays List of numbers in brackets A comma or space separates numbers (columns) A semicolon separates row Zeros and ones Matrices: zeros() ones() Indexing (row,column) Colon Operator: Range of Data first:last or first:increment:last Manipulating Arrays & Matrices Transpose Input Examples of input to arrays: Single number array & scalar: 1 × 1 >> a = 2 Row array & row vector: 1 × n >> b = [1 3 2 5] Column array & column vector: n x 1 >> b = b’ % This an application of the transpose. Array of n rows x m columns & Matrix: n × m >> c = [1 2 3; 4 5 6; 7 6 9] % This example is 3 x 3. Basic elements of a program Input Initialize, define or assign numerical values to variables. Set of command expressions Operations applied to input variables that lead to the desired result. Output Display .