20 Chapter 2:MATLAB Basics exp(-x)and sin(x) 0.5 0 -0.5 -1 10 Figure 2-3 function data class.This is the type of input fzero expects as its first argument. In current versions of MATLAB.fzero also accepts a string expression with independent variable x,so that we could have run the command above without using inline,but this feature is no longer documented in the help text for fzero and may be removed in future versions. Vectors and Matrices MATLAB was written originally to allow mathematicians,scientists,and engineers to handle the mechanics of linear algebra-that is,vectors and matrices-as effortlessly as possible.In this section we introduce these concepts
20 Chapter 2: MATLAB Basics 0 1 2 3 4 5 6 7 8 9 10 -1 -0.5 0 0.5 1 x exp(-x) and sin(x) Figure 2-3 function data class. This is the type of input fzero expects as its first argument. ✓ In current versions of MATLAB, fzero also accepts a string expression with independent variable x, so that we could have run the command above without using inline, but this feature is no longer documented in the help text for fzero and may be removed in future versions. Vectors and Matrices MATLAB was written originally to allow mathematicians, scientists, and engineers to handle the mechanics of linear algebra — that is, vectors and matrices — as effortlessly as possible. In this section we introduce these concepts
Vectors and Matrices 21 Vectors A vector is an ordered list of numbers.You can enter a vector of any length in MATLAB by typing a list of numbers,separated by commas or spaces,inside square brackets.For example, >>Z=[2,4,6,8] Z= 2 4 6 8 >>Y=[4-35-281] Y= 4 -3 5 -2 8 1 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 23 456789 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 246 810 You can also use fractional or negative increments,for example,0:0.1:1 or 100:-1:0. The elements of the vector x can be extracted as x(1),x(2),etc.For ex- ample, >>X(3) ans
Vectors and Matrices 21 Vectors A vector is an ordered list of numbers. You can enter a vector of any lengthin MATLAB by typing a list of numbers, separated by commas or spaces, inside square brackets. For example, >> Z = [2,4,6,8] Z = 2468 >> Y = [4 -3 5 -2 8 1] Y = 4 -3 5 -2 8 1 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 = 123456789 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 You can also use fractional or negative increments, for example, 0:0.1:1 or 100:-1:0. The elements of the vector X can be extracted as X(1), X(2), etc. For example, >> X(3) ans = 4
22 Chapter 2:MATLAB Basics To change the vector x from a row vector to a column vector,put a prime(') after X: >>X ans 0 2 4 6 8 10 You can perform mathematical operations on vectors.For example,to square the elements of the vector X,type >>X.^2 ans 0 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 x2 would tell MATLAB to use matrix multiplication to multiply x by itself and would produce an error message in this case.(We discuss matrices below and in Chapter 4.)Similarly,you must type or./if you want to multiply or di- vide vectors element-by-element.For example,to multiply the elements of the vector x by the corresponding elements of the vector y,type >>X.*Y ans 0 -6 20 -12 64 10 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 (the matrix ex- ponential function is expm).One of the strengths of MATLAB is its ability to efficiently perform operations on vectors
22 Chapter 2: MATLAB Basics To change the vector X from a row vector to a column vector, put a prime (’) after X: >> X’ ans = 0 2 4 6 8 10 You can perform mathematical operations on vectors. For example, 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. (We discuss matrices below and in Chapter 4.) 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, type >> X.*Y ans = 0 -6 20 -12 64 10 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 (the matrix exponential function is expm). One of the strengths of MATLAB is its ability to efficiently perform operations on vectors
Vectors and Matrices 23 Matrices A matrix is a rectangular array of numbers.Row and column vectors,which we discussed above,are examples of matrices.Consider the 3 x 4 matrix 3 6 8 9 10 11 12 It can be entered in MATLAB with the command >>A=[1,2,3,4:5,6,7,8;9,10,11,12] A 1 2 4 5 6 7 8 9 10 11 12 Note that the matrix elements in any row are separated by commas,and the rows are separated by semicolons.The elements in a row can also be separated by spaces. If two matrices A and B are the same size,their (element-by-element)sum is obtained by typing A B.You can also add a scalar (a single number)to a matrix;A+c adds c to each element in A.Likewise,A -B represents the difference of A and B,and A -c subtracts the number c from each element of A.If A and B are multiplicatively compatible(that is,if A is n x m and B is m xe),then their product A*B is n x e.Recall that the element of A*B in the ith row and jth column is the sum of the products of the elements from the ith row of A times the elements from the jth column of B,that is, (A*B= AiBk,1≤i≤n,1≤j≤E. The product of a number c and the matrix A is given by c*A,and A'represents the conjugate transpose of A.(For more information,see the online help for ctranspose and transpose.) A simple illustration is given by the matrix product of the 3 x 4 matrix A above by the 4 x 1 column vector z': >>A*Z ans 60 140 220
Vectors and Matrices 23 Matrices A matrix is a rectangular array of numbers. Row and column vectors, which we discussed above, are examples of matrices. Consider the 3 × 4 matrix A = 12 3 4 56 7 8 9 10 11 12 . It can be entered in MATLAB withthe command >> A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12] A = 1234 5678 9 10 11 12 Note that the matrix elements in any row are separated by commas, and the rows are separated by semicolons. The elements in a row can also be separated by spaces. If two matrices A and B are the same size, their (element-by-element) sum is obtained by typing A+B. You can also add a scalar (a single number) to a matrix; A+c adds c to eachelement in A. Likewise, A-B represents the difference of A and B, and A-c subtracts the number c from eachelement of A. If A and B are multiplicatively compatible (that is, if A is n× m and B is m× ), then their product A*B is n× . Recall that the element of A*B in the ithrow and jth column is the sum of the products of the elements from the ithrow of A times the elements from the jthcolumn of B, that is, (A ∗ B)i j = m k=1 AikBkj, 1 ≤ i ≤ n, 1 ≤ j ≤ . The product of a number c and the matrix A is given by c*A, and A’ represents the conjugate transpose of A. (For more information, see the online help for ctranspose and transpose.) A simple illustration is given by the matrix product of the 3 × 4 matrix A above by the 4 × 1 column vector Z’: >> A*Z’ ans = 60 140 220
24 Chapter 2:MATLAB Basics The result is a 3 x 1 matrix,in other words,a column vector. MATLAB has many commands for manipulating matrices.You can read about them in the section More about Matrices in Chapter 4 and in the online help;some of them are illustrated in the section Linear Economic Models in Chapter 9. Suppressing Output Typing a semicolon at the end of an input line suppresses printing of the output of the MATLAB command.The semicolon should generally be used when defining large vectors or matrices (such as x =-1:0.1:2;).It can also be used in any other situation where the MATLAB output need not be displayed. Functions In MATLAB you will use both built-in functions as well as functions that you create yourself. Built-in Functions MATLAB has many built-in functions.These include sqrt,cos,sin,tan, log,exp,and atan(for arctan)as well as more specialized mathematical functions such as gamma,erf,and besselj.MATLAB also has several built- in constants,including pi(the number x),i(the complex number i=v-1), and Inf (oo).Here are some examples: >>1og(exp(3)) ans 2 The function log is the natural logarithm,called "In"in many texts.Now consider >>sin(2*pi/3) ans 0.8660
24 Chapter 2: MATLAB Basics The result is a 3 × 1 matrix, in other words, a column vector. ☞ MATLABhas many commands for manipulating matrices. You can read about them in the section More about Matrices in Chapter 4 and in the online help; some of them are illustrated in the section Linear Economic Models in Chapter 9. Suppressing Output Typing a semicolon at the end of an input line suppresses printing of the output of the MATLAB command. The semicolon should generally be used when defining large vectors or matrices (such as X = -1:0.1:2;). It can also be used in any other situation where the MATLAB output need not be displayed. Functions In MATLAB you will use bothbuilt-in functions as well as functions that you create yourself. Built-in Functions MATLAB has many built-in functions. These include sqrt, cos, sin, tan, log, exp, and atan (for arctan) as well as more specialized mathematical functions suchas gamma, erf, and besselj. MATLAB also has several builtin constants, including pi (the number π), i (the complex number i = √−1), and Inf (∞). Here are some examples: >> log(exp(3)) ans = 3 The function log is the natural logarithm, called “ln” in many texts. Now consider >> sin(2*pi/3) ans = 0.8660