1 LECTURE 3
OUTLINES Variable names in MATLAB Examples Matrices, Vectors and Scalar Scalar Vectors Entering a vector Colon operator ( : ) Mathematical operations on vectors examples 2
VARIABLE NAMES IN MATLAB Rules for declaring variables are; Are case-sensitive Can contain up to 63 characters (any characters beyond the 63rd are ignored) Must start with a letter, followed by any comb. of letters, numbers, and underscores Cannot contain spaces 3
Examples >> n=4; >> n n=4 >> N Undefined function or variable 'N'. >> 2n=4 Error: Unexpected MATLAB operator. >> frn d=4 Undefined function 'frn' for input arguments of type 'char'. 4
MATLAB, like most programming languages, requires that expressions contain a single variable to the left of the assignment operator (equals sign). Example: X = 2*A + 3*B - 15 Single variable Assignment operator Expression involving variables, functions, numbers, etc Additionally, MATLAB performs the calculations to the right of the assignment operator (=) using existing values of variables and then assigns the result to the variable to the left of the equals sign. So the expression X = X + 2 X new = X old + 2 should be interpreted as 5
Examples: (Order of operations) Try these by hand and then verify in MATLAB. A 5; B 4; C 3; D 2; F1 AB CD F2 A*B C*D F3 B/A *C F4 B/(A *C) F5 B/A/C F6 B^ D*D F7 ( A B)^1/ 2 F8 ( A B)^ (1/ 2) A A 1 A 6*A - A^2 (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) 6
Examples: (Order of operations) Write MATLAB expressions for F1 F5, then test them in MATLAB using A = 5, B = 4, C = 3, and D = 2. F1 F2 F3 F4 F5 A B C D 2 A B C 1 D 3 A 1 A A - B 3/4 1 1 B 3 7
MATRICES, VECTORS AND SCALAR Matrices are the basic elements of the MATLAB environment. A matrix is a two-dimensional array consisting of m rows and n columns. Special cases are; Column vectors (n = 1) and Row vectors (m = 1). However, MATLAB can also be used to work with variables defined by a single value called scalars. 8
SCALAR In the MATLAB environment, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to 1-by-1 matrices, which are called scalars. It is better to think of everything as a matrix. Example: Type of quantity Mathematical notation: MATLAB notation: Vector A= 1 2 3 4 A = [1 2 3 4] Scalar x = 2 x = 2 9
VECTORS A vector is an ordered list of numbers. A vector is a special case of a matrix. As discussed earlier, an array of dimension 1 n is called a row vector. Whereas an array of dimension m 1 is called a column vector. The elements of vectors in MATLAB are enclosed by square brackets and are separated by spaces or by commas. 10
ENTERING A VECTOR For example, to enter a row vector v, type in the command window. >> v = [1 4 7 10 13] v = 1 4 7 10 13 Column vectors are created in a similar way, however, semicolon (;) must separate the components of a column vector, >> w = [1;4;7;10;13] w = 1 4 7 10 13 11
On the other hand, a row vector is converted to a column vector using the transpose operator. The transpose operation is denoted by an apostrophe or a single quote ('). For example >> w = v' w = 1 4 7 10 13 12
Thus, v(1) is the first element of vector v, v(2) its second element, and so forth. For example >> v(4) ans= 10 Furthermore, to access blocks of elements, we use MATLAB's colon notation ( : ). For example, to access the first three elements of v, we write, >> v(1:3) ans = 1 4 7 13
Or, all elements from the third through the last elements, >> v(3:end) ans = 7 10 13 Where end signifies the last element in the vector. If v is a vector, writing >> v(:) Produces a column vector. Whereas writing >> v(1:end) Produces a row vector. 14
COLON OPERATOR ( : ) Suppose you want to create a vector of values running from 1 to 9. Here s how to do it without typing each number: >> X = 1: 9 X= 1 2 3 4 5 6 7 8 9 The notation 1:9 is used to represent a vector of numbers running from 1 to 9 in increments of 1. The increment can be specified as the second of three arguments: >> X = 0: 2: 10 X= 0 2 4 6 8 10 15
You can also use fractional or negative increments, for example. >> A= (0: 0.1: 1) A= 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Or >> A= (10: -1: 0). A= 10 9 8 7 6 5 4 3 2 1 0 16
MATHEMATICAL OPERATIONS ON VECTORS You can perform mathematical operations on vectors. For example, if X= (0: 2: 10); Then to square the elements of the vector X, type >> X.ˆ2 ans = 0 4 16 36 64 100 The period in this expression is very important; it says that the numbers in X should be squared individually, or element-by-element. Typing Xˆ2 would tell MATLAB to use matrix multiplication to multiply X by itself and would produce an error message in this case. 17
Similarly, you must type.* or./ if you want to multiply or divide vectors element-by-element. For example, to multiply the elements of the vector X by the corresponding elements of the vector Y, where Y is >>Y=[4-3 5-2 8 1] Y= 4-3 5-2 8 1 You should type >> X.*Y ans = 0-6 20-12 64 10 18
Most MATLAB operations are, by default, performed element-by-element. For example, you do not type a period for addition and subtraction. And you can type exp(x) to get the exponential of each number in X. But while adding or subtracting two vectors their size should be the same. For example, >> X+Y ans= 4-1 9 4 18 11 19
MORE EXAMPLES ON VECTORS >> V=[1 3, sqrt(9)] V = 1 3 3 >> V2=[3+4 5] V2 = 7 5 >> V3=[3 +4 5] V3 = 3 4 5 >> V+V3 ans = 4 7 9 20
>> V4= 3*V V4 = 3 9 9 >> C= [1; 3; sqrt(5)] "Entering a column vector" C = 1.0000 3.0000 2.2361 >> C2= [3; 4; 5] C2 = 3 4 5 >> C3= 2*C - 3*C2 C3 = -7.0000-6.0000-10.5279 21
22