Help and Online Documentation

This document provides a basic introduction to matlab and the best way to make good use of it is to run its examples in your computer. After you grasp the basic facts about matlab you can use the help features and online documentation provided by the supplier of matlab:

There are several different ways to access online information about matlab functions:

Type the following and hit the return key

• help

to determine the syntax and behavior of a particular function. For example to see how to use the plot function, type help plot and matlab will give you the syntax of the command and it will describe how to plot graphs

• helpwin

to get a general help menu window about all the topics for which help is available. If you type helpwin plot then you will see a new window with the description of the plot function

• lookfor keyword

to get a listing in the command window for all commands whose descriptions include the keyword. Let us say you want to know what commands matlab has for writing data into disk. Then you may want to type lookfor write

• helpdesk

to display the Help Desk page in a web browser, providing direct access to a comprehensive library of online help, PDF-formatted documentation, trouble-shooting information and the MathWorks web site

Entering Matrices

One of the ways you can enter a into matlab is to list its entries explicitly:

• Start with [ , and stop with ] • To indicate the end of rows, either use a semicolon ; , or hit the enter key • Put commas , or blanks between elements of a row

Now let's define some matrices:

a = [1, 0, 2; 1, 1, 1; 2, 2, 0]

A = [1 2 3

4 4 1

0 9 3 ]

Also note that Matlab is case sensitive, so a and A are different objects. If you do not put a semicolon after a command then Matlab displays the output of that command. In the above examples we did not type ;, hence Matlab displays the matrix that is formed each time. If we type ;, say, after the definition of A, then Matlab stores A, but does not display it.

The result of the last command that is executed is stored in the variable named ans. You can use ans as any other variable.

Arithmetic with Matrices

Multiplication:

A * B a * 2

Addition and Subtraction:

A + B A - B 2 + a (A + B) * (a - 4)

Division: A * (B inverse)

A / B

Division: (A inverse) * B

A \ B

Inverse:

inv (A)

Power:

B ^ 3

Complex Conjugate Transpose: (This is same as Transpose for real matrices)

A' Some Special Matrices

Identity Matrix:

A = eye(5) creates A which is 5x5 .

Zero Matrix:

B = zeros(4,2) creates B which is 4x2 .

Matrix of Ones:

C = ones(3,3) creates C which is 3x3 matrix of ones.

Row Vector Whose Components Increase Arithmetically:

u = -2:10 creates a row vector (of dimension 1x13) whose first element is -2 , last element is 10 and all the elements in between increase one by one.

Row Vector Whose Components Change by Non-unit Steps:

v = 1:-.1:-1 creates a row vector (of dimension 1x21) whose first element is 1 , last element is -1 and all the elements in between decrease by one-tenth.

Row Vector with Linearly Spaced Entries:

w = linspace(0,pi,10) creates a row vector (of dimension 1x10) whose first element is 0 , last element is pi and all the elements in between are equally spaced and, there are 10 total entries. So if you use linspace(p,q,n), then matlab creates a vector of size n whose elements are equally spaced and whose first and last elements are p and q respectively.

Row Vector with Logarithmitically Spaced Entries:

h = logspace(1,100,11)

Diagonal Matrix with a Given Vector on the Diagonal:

D = diag(u) creates a D (of dimension 13x13) with the vector u on the diagonal.

Column Vector of the Diagonal Elements of a Given Matrix:

diag(D) displays the diagonal of the matrix D placed in a column vector. Note that the vector is not stored anywhere other than ans.

Uniformly Distributed Random Elements:

E = rand(5,6)

Normally Distributed Random Elements:

F = randn(100,10) More on Matrices

More on the use of Colon Operator:

F(1:k,j) refers to the first k elements of the column j of F. sum(A(2,:)) calculates the sum of elements in the second row of A. sum(A(1,1:2)) calculates the sum of the first two elements in the first row of A. sum(A(:,end)) calculates the sum of the last column of A. Note that if : is used alone then it refers to the whole row (or column). Hence sum(A(:,:)) statement is equivalent to sum(A) statement. Note that sum(A) produces a row vector in which the sum of each column of A is stored.

Graphics

To plot y against x type plot(x,y)

If you want to graph a function say sin(x), then you may type the following:

x = 0:pi/100:2*pi; y = sin(x); plot(x,y)

Changing Plot Fonts:

plot(x,y,'c+:') plots a cyan dotted line with a plus at each data point;

plot(x,y,'bd') plots blue diamond at each data point but does not draw any line.

See help plot for more options.

Axes and Titles:

figure(n) creates a figure window number n

title(‘your title here’) creates a title -- xlabel(‘your label here’) creates the X-axis label (same for ylabel(‘’) )

Multiple Plots: subplot create axes in tiled positions. subplot(m,n,p) breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for for the current plot. For example

subplot(2,1,1), plot(x,y)

subplot(2,1,2), plot(x,y)

Functions of two variables

Here is how we graph the function z(x,y) = x exp( - x^2 - y^2):

[x,y] = meshdom(-2:.2:2, -2:.2:2); z = x .* exp(-x.^2 - y.^2); mesh(z)

The first command creates a matrix whose entries are the points of a grid in the square -2 <= x <= 2, -2 <= y <= 2. The small squares which make up the grid are 0.2 units wide and 0.2 unit tall. The second command creates a matrix whose entries are the values of the function z(x,y) at the grid points. The third command uses this information to construct the graph