Operations in Matlab

Total Page:16

File Type:pdf, Size:1020Kb

Operations in Matlab

ES205 Understanding Operations and Matrices in Matlab: Matlab is an excellent tool for performing a wide variety of operations on scalar, vector, and matrix values.

Scalar: A single number >> a = 5 Real scalar 5 >> b = 8.9883 + 4.234i Complex scalar 8.9883 + i 4.234

Vector: A row or column of numbers >> vector_A = [ 2 pi 17]  Row vector [ 2 3.14... 17]

轾62 >> vector_B = [62; 94]  Column vector 犏 臌94

A row vector can also be created by use of the colon operator:

>> vector_C= 5:7  [5 6 7]

>> vector_D=[10:-0.5:7.5]  [10.0 9.5 9.0 8.5 8.0 7.5]

Matrix: A set of columns and rows of numbers

轾1 2 >> matrix_A = [1 2; 3 4]  犏 臌3 4 轾14 28 0 >> matrix_B = [14 28 0; Inf NaN i]  犏 臌inf NaN 0+ i 1

where Inf and NaN are special terms for ∞ and 0/0

How would you type in the matrix 1.1 4.4 7.7 as a single line of code? 2.2 5.5 8.8 3.3 6.6 9.9

>> X=[1.1, 3.3, 7.7; 2.2, 5.5, 8.8; 3.3, 6.6, 9.9] or X= [1.1 3.3 7.7 2.2 5.5 8.8 3.3 6.6 9.9]

Vectors and Matrixes use brackets [ and ] to enclose lists of array elements Elements of a row are separated by either blanks or commas , Rows are separated by either "Enter" or semicolons ; Subscripts of Arrays: What if we want to extract or operate on entries within a vector or matrix? We need to employ subscripts to pull out individual elements or subsets!

Define G as shown below. >> F =[ 6.0 9.0 10.0 15.0] a vector with 4 elements

>> G =[ 1.0 5.0 3.4 a matrix with 6 elements -5.2 9.8 3.5]

Examples of using subscripts to pull out specific elements or subsets of F or G.

>> F SubsetA = F(4) ______

>> F_SubsetA = F(1:3) ______

>> G_SubsetA = G(1,2) ______

>> G_SubsetB = G(1:2,2:3) ______

Mathematical Operators in Matlab: In addition to the usually math operators (+ - * / ^) Matlab has a number of mathematical operations that are no what you think of as standard mathematical operations. These are used to be work efficiently with matrices. To be able to use Matlab to its full potential, you need to understand how the different operators work.

Operators in order of importance:

( ) parenthesis Highest : colon operator ‘ .’ transpose operators ^ .^ exponentiation operators * / \ .* ./ .\ multiplication and division + - addition, subtraction, and negation < > <= >= = = ~= logical comparators ~ logical NOT & logical AND | logical OR Lowest Addition: Scalar + Matrix Addition: >> B = 5 + [1 2 3 4 5] ==> [ 1+5 2+5 3 + 5 4 + 5 5+5] ==> [6 7 8 9 10] Matrix + Matrix Addition: >>B = [ 1 2 3] + [8 -2 0] ==> [1+8 2+(-2) 3+0] ==> [ 9 0 3] >> C = [ 1 2 3] + [ 5; 10; -2] =>***error--Matrix dimensions must match*** Subtraction: Scalar – Matrix Subtraction: >> D = 5 - [ 3 2 6] ==> [ 5 - 3 5 - 2 5 - 6] ==> [ 2 3 -1]

Matrix – Matrix Subtraction: >>E = [ 1 2 3] - [ 10 0 -4] ==> [ 1-10 2-0 3-(-4)] ==> [-9 2 1] Multiplication: Scalar * Matrix Multiplication:

>>F = 5 * [ 1 2 3] ==> [ 5*1 5*2 5*3] ==> [ 5 10 15]

Full Matrix (Matrix1 * Matrix2) Multiplication: requirement: # columns of matrix 1 = # rows of matrix 2 1 x 3 3 x 1 1x1 轾4 犏-1 G = [ 1 2 4]*[4; -1; 3] => [1 2 4]* 犏 => [ 1*4+2*-1+4*3] => [14] 臌犏3 3 x 1 1 x 3 3 x 3 轾4 轾4 8 16 犏1 犏-1 - 2 - 4 H= [4; 1; -3]*[1 2 4] ==> 犏 * [ 1 2 4] ==> 犏 臌犏-3 臌犏3 6 12

Element-by-Element (Matrix .* Matrix) requirement: matrices must be same size 1 x 3 1 x 3 1 x 3 J = [1 2 4] .* [8 -2 0] ==> [1*8 2 * (-2) 4*0] ==> [8 -4 0]

2 x 2 2 x 2 2 x 2 轾1 2 轾5 0 轾5 0 K = [1 2; 3 4].*[5 0 ; -2 6]=> 犏 .* 犏 = 犏 臌3 4 臌-2 6 臌-6 - 24 Division: Matrix/ Scalar or Scalar\Matrix Divison: >>L = [ 1 2 3] / 5 ==> [ 1/5 2/5 3/5]==> [ .2000 .4000 .6000] >>M = 4 \ [4 10 -8] ==> [4\4 4\10 4\(-8)] ==>[ 1.00 2.50 -2.0]

Full Matrix (Matrix/Matrix or Matrix\Matrix ) Division: requirement: for right division /, # of columns must be equal requirement: for left division \ , # of rows must be equal A = [1 2 3 B = [ 1 3 5] C = [ 9 3 5 2 -1 0 2 6] 4] >> B/A ==> [1 3 5]/[1 2 3 ==> [0.2500 0.2500 0.6250] 3 5 2 0 2 6] >> A\C ==> [1 2 3 \ [ 9 ==> [ 24.5000 3 5 2 -1 -17.5000 0 2 6] 4] 6.5000]

Element by Element (Matrix ./ Matrix) (Matrix .\ Matrix) Division: requirement: matrixes must match in size

>> [ 1 2 3] ./ [4 -3 0 ] ==> [1/4 2/(-3) 3/0] ==> [0.2500 -0.6667 Inf] >> [1 4 2 . \ [8 3 0 ==> [ 1\8 4\3 2\0 => [0.125 0.75 0.00 0 -1 3] 9 5 1] 0\9 -1\5 3\1] Inf -5.00 0.33]

Exponentiation: Matrix ^ Scalar Exponentiation requirement: matrix must be square (n x n) >> [ 1 2; 3 4]^2 ==> [ 1 2 * [ 1 2 ==> [ 7 10 3 4 ] 3 4] 15 22] Matrix .^ Scalar Exponentiation >> [ 1 2; 3 4] .^2 ==> [ 1 2 .^ 2 ==> [ 1^2 2^2 ==> [ 1 4 3 4 ] 3^2 4^2] 8 16] Scalar ^ Matrix Exponentiation >> 2^[1 2; 3 4] ==> [ 10.4827 14.1519 21.2278 31.7106]

Matrix .^ Matrix (Element-by-Element) Exponentiation requirement: matrix sizes must match > [ 3 4; 5 6] .^ [ 1 2; 3 4] ==> [ 3^1 4^2 == [ 3 16 5^3 6^4] 125 1296] Solution to simultaneous equations:

Given a set of 3 equations: 6 x + 1 y + 3 z = 10 [ 6 1 3 [ x [ 1 0 3 x + 8 y + 5 z = 20 ==> 3 8 5 * y = 20 -1 x + 2 y + 7 z = 30 -1 2 7 ] z ] 30]

A * X = B

Coefficient * Unknown = Constant Matrix Matrix Matrix

To solve for X

A = [6 1 3 B = [ 10 3 8 5 20 -1 2 7 ] 30 ]

Method 1: Use inverse Matrix >> inv(A)*B

>> X=inv(A) * B =[ 0.1575 -0.0034 -0.0651 * [10 -0.0890 0.1541 -0.0719 20 0.0479 -0.0445 0.1541 ] 30]

= [ -0.4452 ==> x 0.0342 ==> y 4.2123] ==> z

Method 2: Use left division >> A\B

>> X=A\ B =[ 6 1 3 \ [10 3 8 5 20 -1 2 7] 30]

= [ -0.4452 ==> x 0.0342 ==> y 4.2123] ==> z Example: You have performed an experiment where you have found the undamped natural frequency for a system and want to see how different damping coefficients affect the system response.

For instance, you have an undamped natural frequency of

wn = 2.5 rad/s and are interested in the response when

z = 0.1, 0.3, 0.5, and 0.7

Knowing these characteristics equations apply to the system: Percent Overshoot Rise Time Settling Time 2 -1 1-z  p - tan 4 2 z T = %OS  100e 1 T = S 2% zw rise 2 n wn 1- z

Write a short m-file in Matlab to calculate each of these for the four values of z ? m-file Code: % mydemo.m -- a short Matlab demo wn=2.5 zeta=[0.1 0.3 0.5 0.7] sqzeta=sqrt(1-zeta.^2) PO=100*exp(-pi.*zeta./sqzeta) Trise=(pi-atan2(sqzeta,zeta))./(wn*sqzeta) Tsettling2=4./(zeta*wn)

Results: >> mydemo

wn = 2.5000 zeta = 0.1000 0.3000 0.5000 0.7000 sqzeta = 0.9950 0.9539 0.8660 0.7141 PO = 72.9248 37.2326 16.3034 4.5988 Trise = 0.6718 0.7864 0.9674 1.3141 Tsettling2 = 16.0000 5.3333 3.2000 2.2857

Recommended publications