intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Lecture 2 MATLAB fundamentals

Chia sẻ: Nguyễn Doãn Hùng | Ngày: | Loại File: PPT | Số trang:35

40
lượt xem
4
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating arrays Variables What are variables? You name the variables (as the programmer) and assign them numerical values.

Chủ đề:
Lưu

Nội dung Text: Lecture 2 MATLAB fundamentals

  1. Lecture 2 Lecture 2 MATLAB fundamentals Variables, Naming Rules,  Arrays (numbers, scalars, vectors, matrices),  Arithmetical Operations, Defining and manipulating arrays © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
  2. Variables Variables  What are variables? – You name the variables (as the programmer)  and assign them numerical values.
  3. Variable Naming Rules Variable Naming Rules  Must begin with a LETTER  May only contain letters, numbers and  underscores ( _ )  No spaces or punctuation marks allowed!  Only the first 63 characters are significant;  beyond that the names are truncated.  Case sensitive (e.g. the variables a and A  are not the same)
  4. Which variable names are  Which variable names are  valid? 12oclockRock  tertiarySector  blue cows  Eiffel65  red_bananas  This_Variable_Name_Is_Quite_Possibly_Too_L  ong_To_Be_Considered_Good_Practice_Howev er_It_Will_Work % (the green part is not part of  the recognized name)
  5. Variable Naming Conventions Variable Naming Conventions There are different ways to name variables.  The   following illustrate some of the conventions used: – lowerCamelCase – UpperCamelCase – underscore_convention If a variable is a constant, some programmers use all   caps: – CONSTANT It does not matter which convention you choose to work   with; it is up to you.
  6. Variables as Arrays In MATLAB, a variable is stored as an array of variable numbers. When appropriate, it is interpreted as a scalar, vector or matrix. scalar vector matrix scalar vector matrix 1×1 n × 1 or 1 × n n×m The size of an array is specified by the number of The rows and the number of columns in the array, with the number of rows indicated first. the
  7. Scalars  Scalars are 1×1 arrays.  They contain a single value, for example: r=6 height = 5.3 width = 9.07
  8. Vectors Vectors A vector is a list of numbers expressed as a 1 vector  dimensional array. dimensional A vector can be n×1 or 1×n.  Columns are separated by commas (or spaces): Columns commas  h = [1, 2, 3] Rows are separated by semicolons: Rows semicolons  v = [1; 2; 3]
  9. Matrices Columns 1 2 3 A matrix is a two   1 3.0 1.8 3.6 dimensional array of  numbers. 2 4.6 ­2.0 21.3 Rows 3 0.0 ­6.1 12.8 For example, this is a   4×3 matrix: 4 2.3 0.3 ­6.1 m = [3.0, 1.8, 3.6; 4.6, -2.0, 21.3; 0.0, 2.0, -6.1, 12.8; 2.3, 0.3, -6.1] 6.1,
  10. Indexed­location of numbers in an  array Columns Each item in an array  1 2 3  is located in the  (row, column).  1 3.0 1.8 3.6 m(2,3) m(2,3) 2 4.6 ­2.0 21.3 Rows ans = ans 21.3000 21.3000 3 0.0 ­6.1 12.8 4 2.3 0.3 ­6.1
  11. Examples  Enter the following into MATLAB: – Scalar: a=1 – Vectors: b = [1, 0, 2] c = [1 0 2] – Matrix: d = [5, 4, 3; 0, 2, 8]
  12. Examples Examples Enter (input) the following matrix into MATLAB:  ­7 21 6 2 32 0 whiteRabbit = ­5 0 ­18.5
  13. Scalar Operations Scalar Operations Operation Algebraic  MATLAB  Syntax Syntax a + b a+b Addition a ­ b a–b Subtraction a × b a .* b Multiplication a ÷ b a ./ b Division ab a .^ b Exponentiation
  14. Array Operations Array Operations Arrays of numbers in MATLAB can be interpreted as   vectors and matrices if vector or matrix algebra is to be  applied. Recall that matrices are mathematical objects  that can be multiplied by the rules of matrices. To do  matrix multiplication, you need to use the standard *,  /,  and ^ operators [without the preceding . (dot)]. They are  operators [without the preceding  not for array multiplication, division and exponentiation. To deal with arrays on an element­by­element level we   need to use the following array or dot­operators: .* , ./  .^ .* ./ and and
  15. Array operations & dot­operators Array operations & dot­operators .* ,  ./  .^ ./ and and  Because scalars are equivalent to a 1×1  array, you can either use the standard or  the dot­operators when doing  multiplication, division and exponentiation  of scalars (i.e., of single numbers).  It is okay for you to always use the dot­ operators, unless you intend to perform  vector or matrix multiplication or division.
  16. Array vs. Matrix Operations Example:  x =  [ 2, 1; 3, 4] y =  [ 5, 6; 7, 8] z = x .* y  results in [10, 6; 21, 32]; this is array multiplication z=x*y results in [17, 20; 43, 50]; this is matrix multiplication So, do NOT forget the dot when doing array  operations!  (.*   ./    .^)
  17. Hierarchy of Operations Hierarchy of Operations Just like in mathematics the operations are done in the  following order: Left to right doing what is in  Parentheses & Exponents first, followed by                 Multiplication & Division, and then                       Addition & Subtraction last.  An example: c = 2+3^2+1/(1+2) 1st c = 2+3^2+1/3 c = 2+3^2+1/(1+2) 2nd c = 2+9+1/3 c = 2+3^2+1/(1+2) 3rd c = 2+9+0.33333 c = 2+3^2+1/(1+2) 4th c = 11+0.33333 11 c = 2+3^2+1/(1+2) 5th c = 11.33333 2+3^2 11.33333
  18. Hands­on Hands­on  Enter these two arrays into MATLAB: a= b= 10 5 5 1 0 2 10 2 9 0 0 0 0 6 8 8 1 1 0  Multiply, element­by­element, a × b. – Since this is an array operation, the .*  multiplication operation is implied by the  request.    
  19. Defining & manipulating arrays Defining & manipulating arrays  All variables in MATLAB are arrays! – Single number array & scalar: 1×1 – Row array & row vector: 1×n – Column array & column vector: nx1 – Array of n rows x m columns & Matrix: n×m – Naming rules – Indexed by (row, column) Indexed  Remark: vectors and matrices are special mathematical objects, arrays are lists or tables of numbers. tables
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
6=>0