CS100J      Lab 12       MATLAB      17-18 April 2007

This lab is designed to give you some familiarity with Matlab. Your lab instructor will give you a few minutes introduction to the pieces of MatLab.

Here, we give you a series of things to do to get you to use Matlab.

THE HELP FACILITY
The first thing to do is to bring up the help window, because you will use it often. Click on the help icon, a question mark ?, in the bar that contains some icons like that. Keep the window open while you are using Matlab. See whether you can find the Contents and Index Tabs, in the middle pane (at least on a Macintosh). Click on Index, because you can use that most often to find what you want. Ask your instructor if you have trouble finding the Index.

GENERAL EXPRESSIONS
Now, in the command window (right side), type in these expressions and write down next to them what answer you get:

2
5+2
5-2
5 / 2
5\2
5*2

[NOTE: As in Java, you can access the previously typed command lines and edit them by hitting the uparrow and downarrow keys appropriately.]
Does division of integers work the way it does in Java?  (yes  or no)

What do you think that 5\2 computes?

Find out what it computes. In the index in the help window, scroll down to and click on “operators”. What does it say? Does what it say help you?

Figure out what “left division” does by typing in these expressions:

10\2
10\3
10\4
10\5
10\10
10\20

The help window for “operators” shows you operation ^. It is supposed to compute a power. So, type these expressions:

2^0
2^2
2^4
2^-1
2^-2
2^15	

The operator help page also tells you that you can use parentheses. So, write a few expressions with parentheses of your own choice below, and write down the values that Matlab gives you for them.

Now, look up “numbers” in the help index and read about numbers. Don’t worry about imaginary numbers.

VARIABLES AND ASSIGNMENTS
You may have noticed that when you type an expression in the command window and hit the enter key, Matlab answers with something like

	ans = 
		          21

Look in the Workspace —the pane in the upper left corner. What does it say the size, bytes, and Class of and is?

ans is a variable, and it is a 1-by-1 double array. ALL numbers are treated as double values, even if they are integers, and all variables that are assigned a double value are treated as 1-by-1 arrays! Since ans looks like a variable (and it is one), it looks like the evaluation of any expression is automatically assigned to variable ans. To test this, make an assignment to ans and then use ans in an expression:

ans= 5
6+ans

What value does ans have now? 

Now, try assignments to other variables –there is no need to declare them—and look in the Workspace after each one to see the variables that are currently being used:

A= ans+1
a= A
a= 20*a + A

Are upper and lowercase letters treated the same or differently when used in identifiers for variables?

Now, look up “variables” in the index and read about them.

FUNCTIONS
Look up “functions” in the index and read about them.

It looks like there is a simple help facility to get the specifications of some functions. For example, try this in the Command window:

help abs

Find out what “elementary” functions exist by typing

help elfun

Below, write some expressions that have calls on some of these functions. Include the functions sqrt, nextpow2, fix, floor, ceil, round, rem, sign.

To find the specification of function nextpow2, type

help nextpow2

Built-in constants
As you can see in the section on functions, Matlab has some built-in constants. Try these:

pi
realmin
realmax
infinity

Defining a variable with the same name as a constant
Try this and write what value pi has after this.

pi= 4

Look in the Workspace. Do you see pi there? What type does it have? 

Type pi in the Command window. What value does pi have?

Do you think that you have really changed the value of constant pi?  (yes or no)

Try this: In the Workspace pane, click on variable pi to highlight it. Then, select menu item Edit->delete. What happens? Delete the variable. Now type pi in the Command window. What value does pi have?

This should tell you that you did not change the constant pi; you just declared a new variable pi, and the scope of that variable shadowed the scope of the constant. It is like a local variable v in a method in Java prohibiting reference to a field v of the class.

USING A SEMICOLON TO END AN EXPRESSION
Type in

5;
5
x= 5
x= 5;

What difference is there in the response by Matlab? Is this the same way that DrJava operates?

ARRAYS
You now have been through the basics of simple expressions in Java. We turn the arrays. Much of this material is covered in the handout for Matlab.

In Matlab, one-dimensional arrays are called vectors, and they may be either row vectors or column vectors. Square brackets delimit arrays, and row vectors are separated by blanks.

Try this in the Command window

x= [10   20   30]

How did Matlab respond?  What is the size, bytes, and class of x (look in the Workspace)
Rows are separated by columns. So, type in this, and see how Matlab responds:

y=  [10;  20;  30]

What is the size, bytes, and class of y?

Sometimes, you want a row vector of values that are in order. Function linspace can do this for you. In the Command window, type

linspace(3,7,2)
linspace(3,7,3)
linspace(3,7,4)
linspace(3,7,5)
linspace(1,2,9)

To read the spec of linspace, type 

help linspace

Catenating row vectors and stacking column vectors
Try these expressions and write down what they do:

x= [1 2 3]
y= [5 6 7]
z= [x y]

xc= [1;2;3]
yc= [4;5;6]
zc= [xc; yc]