Books in black and white
 Books Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

# A Guide to MATLAB for Beginners and Experienced Users - Brian R.H.

Brian R.H., Roland L.L. A Guide to MATLAB for Beginners and Experienced Users - Cambrige, 2001. - 346 p.
Previous << 1 .. 5 6 7 8 9 10 < 11 > 12 13 14 15 16 17 .. 91 >> Next

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-3 5-2 8 1]

Y =

4 -35-281

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 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

1 2 3 4
5 6 7 8
9 10 11 12

A =

It can be entered in MATLAB with the command

>> A

A =

[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]
1 2 3 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 x i), then their product A*B is n x i. 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)ij = ^T AikBkj, 1 < i < n, 1 < j < i.

k=1

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 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 n), i (the complex number i = -J—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
Previous << 1 .. 5 6 7 8 9 10 < 11 > 12 13 14 15 16 17 .. 91 >> Next