MATLAB Window Environment and the Base Program

## Getting Help

MATLAB provides hundreds of built-in functions covering various scientific and engineering computations. With numerous built-in functions, it is important to know how to look for functions and how to learn to use them.

For those who want to look around and get a feel for the MATLAB computing environment by clicking and navigating through what catches their attention, a window- based help is a good option. To activate the Help window, type

helpwinorhelpdeskon command prompt or start the Help Browser (Fig. M1.13) by clicking the icon from the desktop toolbar.

**Fig. M1.12 ***Command history window with two commands being deleted *** **

If you know the exact name of a command, type

helpto get detailed task-oriented help. For example, typecommandnamehelp helpwinin the command window to get the help on the commandhelpwin.If you don't know the exact command, but (atleast !) know the keyword related to the task you want to perform, the

lookforcommand may assist you in tracking the exact command. Thehelpcommand searches for an exact command name matching the keyword, whereas thelookforcommand searches for quick summary information in each command related to the keyword. For example, suppose that you were looking for a command to take the inverse of a matrix. MATLAB does not have a command namedinverse; so the commandhelp inversewill not work. In your MATLAB command window try typinglookfor inverseto see the various commands available for the keywordinverse.MATLAB has a wonderful demonstration program that shows its various features through interactive graphical user interface. Type

demoat the MATLAB prompt to invoke the demonstration program (Fig. M1.14) and the program will guide you throughout the tutorials.

**Fig. M1.13 ***Help browser*

**Fig. M1.14 ***Demonstration Window *** **

## Elementary Matrices

Basic data element of MATLAB is a matrix that does not require dimensioning. To create the matrix variable in MATLAB workspace, type the statement (note that any operation that assigns a value to a variable, creates the variable, or overwrites its current value if it already exists).

>>

A=[8 1 6 2;3 5 7 4;4 9 2 6]The blank spaces (or commas) around the elements of the matrix rows separate the elements. Semicolons separate the rows. For the above statement, MATLAB responds with the display

A =

8 1 6 2

3 5 7 4

4 9 2 6Vectors are special class of matrices with a single row or column. To create a column vector variable in MATLAB workspace, type the statement

>> b=[1; 1; 2; 3]

b =

1

1

2

3To enter a row vector, separate the elements by a space or comma '

,'. For example:

>> b=[1,1,2,3]

b =

1 1 2 3We can determine the size of the matrices (number of rows, number of columns) by using the

sizecommand.

>> size(A)

ans =

3 4The command

size, when used with the scalar option, returns the length of the dimension specified by the scalar. For example,size (A,1)returns the number of rows of A andsize(A,2)returns the number of columns ofA.

>> size(A,1)

ans =

3

>> size(A,2)

ans =

4For matrices, the

lengthcommand returns either number of rows or number of columns, whichever is larger. For example,

>> length(A)

ans =

4For vectors,

lengthcommand can be used to determine its number of elements.

>> length(b)

ans =

4The use of colon (

:) operator plays an important role in MATLAB. This operator may be used to generate a row vector containing the numbers from a given starting valuexi, to the final valuexf, with a specified incrementdx, e.g.,x=[xi:dx:xf]

>> x=[0:0.1:1]

x =

Columns 1 through 7

0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000

Columns 8 through 11

0.7000 0.8000 0.9000 1.0000By default, the increment is taken as unity.

To generate linearly equally spaced samples between

x1andx2, use the commandlinspace(x1,x2). By default, 100 samples will be generated. The commandlinspace (x1,x2, N)allows the control over number of samples to be generated. See the example below.

>> x=linspace(0,1,11)

x =

Columns 1 through 6

0 0.1000 0.2000 0.3000 0.4000 0.5000

Columns 7 through 11

0.6000 0.7000 0.8000 0.9000 1.0000Learn how to generate logarithmically spaced vector using the command

logspace.The colon operator can also be used to subscript matrices. For example,

A(:,j)is thej^{th}column ofA, andA(i,:)is thei^{th}row ofA. Observe the following MATLAB session.

>> A=[8 1 6 2;3 5 7 4;4 9 2 6];

>> A(2,:)

ans =

3 5 7 4

>> A(3,2:4)

ans =

9 2 6

>> A(1,3)

ans =

6

>> B=A(1:3,2:3)

B =

1 6

5 7

9 2

>> A(:,3)=[ ]

A =

8 1 2

3 5 4

4 9 6Manipulating matrices is almost as easy as creating them. Try the following operations:

>> A+3

>> A-3

>> A*3

>> A/3When you add/subtract/multiply/divide a vector/matrix by a number (or by a variable with a number assigned to it), MATLAB assumes that all elements of vector/matrix should be individually operated on.

Table M1.3 provides the list of basic operations on any two arbitrary matrices A and B and their dimensional requirements.

**Table M1.3 ***Basic matrix operations *

Operation |
Operator |
Example |
Notes |

Plus | + | A+B |
Must be of same dimensions |

Minus | - | A-B |
Must be of same dimensions |

Multiply | * | A*B |
Must be of compatible dimensions |

Multiply (element-by-element) | .* | A.*B |
Must be of same dimensions; multiplies element a _{ij} with element b_{ij} |

Divide (element-by-element) | ./ | A./B |
Must be of same dimensions; divides element a _{ij} by element b_{ij} |

Divide (element-by-element) | .\ | A.\B |
Must be of same dimensions; divides element b _{ij} by element a_{ij} |

Matrix power | ^ | A^k |
k must be a constant, A must be a square matrix |

Matrix power (element-by-element) | .^ | A.^k |
k is a constant, A can be of any dimensions; |

Example M1.1To find the solution of the following set of linear equations:

we write the equations in the matrix form as

where

is the matrix of coefficients of

xand_{1}, x_{2}x_{3}is the column vector which will contain the solutions

xand_{1}, x_{2}x_{3}is the column vector of values on the right-hand side

The solution vector

where stands for adjoint of matrix and stands for determinant of

The determinant of matrix

is a scalar-valued function of . It is found through the use of minors and cofactors.

The

minor mof the element_{ij}ais the determinant of a matrix of order obtained from by removing the row and column containing_{ij}a. The_{ij}cofactor cof the element_{ij}ais defined by the equation_{ij}

Determinants can be evaluated by an expansion that reduces the evaluation of an determinant down to the evaluation of a string of determinants, namely the cofactors. Selecting an arbitrary row

kof matrix or arbitrary columnlof matrix , we have

or

The adjoint of matrix is found by replacing each element

aof matrix by its cofactor and then transposing._{ij}Following MATLAB commands solve the given set of simultaneous linear equations.

>> A = [2 5 -3; 3 -2 4; 1 6 -4];

>> b = [6; -2; 3];

>> x = inv(A) * b

x =

4.8333

-4.5833

-6.4167

## Exercise M1.4Consider three matrices
Create a vector ## Exercise M1.6Create a vector |

## Flow Control Functions

There are many flow control functions in MATLAB. The

forfunction in MATLAB provides a mechanism for repeatedly executing a series of statements a given number of times. Theforfunction connected to anendstatement sets up a repeating circulation loop. An important point is that eachformust be matched with anend. Thebreakstatement provides exit jump out of loop.The

whilefunction in MATLAB allows a mechanism for repeatedly executing a series of statements an indefinite number of times, under control of a logical condition.The function

ifevaluates a logical expression and executes a group of statements based on the value of the expression. Theelsestatement further conditionalizes theifstatement.