Operations in Matlab

Operations in Matlab

<p>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. </p><p>Scalar: A single number >> a = 5 Real scalar 5 >> b = 8.9883 + 4.234i Complex scalar 8.9883 + i 4.234</p><p>Vector: A row or column of numbers >> vector_A = [ 2 pi 17]  Row vector [ 2 3.14... 17]</p><p>轾62 >> vector_B = [62; 94]  Column vector 犏 臌94</p><p>A row vector can also be created by use of the colon operator:</p><p>>> vector_C= 5:7  [5 6 7] </p><p>>> vector_D=[10:-0.5:7.5]  [10.0 9.5 9.0 8.5 8.0 7.5] </p><p>Matrix: A set of columns and rows of numbers</p><p>轾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</p><p> where Inf and NaN are special terms for ∞ and 0/0</p><p>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</p><p>>> 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] </p><p>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! </p><p>Define G as shown below. >> F =[ 6.0 9.0 10.0 15.0] a vector with 4 elements</p><p>>> G =[ 1.0 5.0 3.4 a matrix with 6 elements -5.2 9.8 3.5]</p><p>Examples of using subscripts to pull out specific elements or subsets of F or G. </p><p>>> F SubsetA = F(4) ______</p><p>>> F_SubsetA = F(1:3) ______</p><p>>> G_SubsetA = G(1,2) ______</p><p>>> G_SubsetB = G(1:2,2:3) ______</p><p>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.</p><p>Operators in order of importance:</p><p>( ) 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]</p><p>Matrix – Matrix Subtraction: >>E = [ 1 2 3] - [ 10 0 -4] ==> [ 1-10 2-0 3-(-4)] ==> [-9 2 1] Multiplication: Scalar * Matrix Multiplication:</p><p>>>F = 5 * [ 1 2 3] ==> [ 5*1 5*2 5*3] ==> [ 5 10 15]</p><p>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</p><p>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]</p><p>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]</p><p>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]</p><p>Element by Element (Matrix ./ Matrix) (Matrix .\ Matrix) Division: requirement: matrixes must match in size</p><p>>> [ 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]</p><p>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]</p><p>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:</p><p>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]</p><p>A * X = B</p><p>Coefficient * Unknown = Constant Matrix Matrix Matrix</p><p>To solve for X</p><p>A = [6 1 3 B = [ 10 3 8 5 20 -1 2 7 ] 30 ]</p><p>Method 1: Use inverse Matrix >> inv(A)*B</p><p>>> 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]</p><p>= [ -0.4452 ==> x 0.0342 ==> y 4.2123] ==> z</p><p>Method 2: Use left division >> A\B</p><p>>> X=A\ B =[ 6 1 3 \ [10 3 8 5 20 -1 2 7] 30]</p><p>= [ -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.</p><p>For instance, you have an undamped natural frequency of </p><p> wn = 2.5 rad/s and are interested in the response when </p><p> z = 0.1, 0.3, 0.5, and 0.7 </p><p>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</p><p>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)</p><p>Results: >> mydemo</p><p> 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</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us