Introduction to MatLab

This section is an introduction to MatLab by examples. Some simple MatLab commands are presented here and you are asked to try them out. The syntax and general use of these commands are described in detail later.

Starting and stopping

To start MatLab, you may do the following. If you are using a computer with DOS or Unix operating system, start MatLab by typing ``matlab'' at the command prompt. If you are using a PC in one of the computer labs on campus, click on CITNOV1 or Connect to CITNOV1. Type Lab as NetWare login name and click on Ok key. Click on CITNOV1 Programs and then click on Matlab icon.

To quit from MatLab, type ``quit'' at the MatLab prompt, i.e.

        >> quit

Using MatLab as a calculator

MatLab can be used as an expression evaluator. To do this you simply type a mathematical expression into the command window. The command window prompt is >>. To enter an expression, type it after the prompt (correct any mistakes by backspacing) and press return. MatLab prints the result back to the command window. For example, to evaluate pi type
	>> pi
MatLab responds with
	ans =
	    3.1416
If you need to read more decimal digits, type format long and then press the return key. Now type pi again. Type format short to return back to 5 decimal digits. pi is a built-in MatLab variable. The preceding command is actually a request for MatLab to print the value of this variable.

Try the following examples

	>> sin(pi/4)
	>> 2^(log2(4))
	>> sqrt(9)

Assigning Values to Variables

MatLab allows you create variables on the fly. To create a variable just use it on the left hand side of an equal sign. The following examples show how to assign values to three variables, x, y and z. It also shows the MatLab response to the assignment statements.
	>> x = 5
	x = 5
	>> y = pi/4
	y = 0.7854
	>> z = y + x^0.25
	z = 2.2807
Note that z exists only as a numerical value, not as the formula with which it was created.

Use the who command to list the currently active variables. For the preceding session this results in

	>> who
	Your variables are:
	ans       x         y         z         
	leaving 7433616 bytes of memory free.
To print the value of a variable that is already defined just type its name at the prompt.
	>> z
	z = 2.2807
Now type what and why to see if you can figure out the uses of them.

Built-in Functions and Variables

MatLab has many very powerful built-in functions. As the preceding example demonstrated these functions include the familiar set found on many hand-held calculators.

You can also create your own functions. To do this you type MatLab commands into a plain text file. The file must have the extension ".m", and because of this MatLab functions are often referred to as "m-files".

MatLab also has some special variables and functions. You have already encountered the ans variable which is automatically created whenever a mathematical expression is not assigned to another variable.

The built-in eps variable is used as a tolerance for internal calculations. Initially eps is equal to machine epsilon. You can reset the value of eps, but this is not recommended unless you are very sure you know what you are doing.

The realmax, realmin, Inf and NaN built-in variables are used to handle floating point exceptions. All numerical variables in MatLab are stored as 32 bit floating point numbers. This corresponds to ``double precision'' on most computers (supercomputers and some high-end workstations being the exception). The Inf and NaN values appear if a floating point exception occurred during the calculations.

On-line Help(!)

MatLab provides on-line help for all built-in functions and commands. To browse a list of help topics type
	>> help
To get help on a specific topic, for example the cosine function, type
	>> help cos
Using help is one way to learn about the variety of built-in functions. The help command is most useful when you know the name of the function, but are unsure how to use it. You can also get help by clicking on help at the top of the MatLab window. The table of contents lists all the functions. The MatLab manuals provide cross-referenced and indexed descriptions of all aspects of using MatLab .

Suppressing Output with Semicolon

In the preceding examples it was useful to have MatLab print the results of the calculations. This is not always the case. MatLab will print the result of every assignment operation unless the expression on the right hand side is terminated with a semicolon. Unlike C the semicolon is not required by MatLab syntax. Rather, it is a convenience that allows multiple statements to be executed without printing the intermediate results. For example, most lines in m-files end with semicolons because only the final results of the calculations are of interest.

To see how the semicolon works enter the following statements exactly as shown, ending each line with a carriage return. Do not enter the prompt character, >>.

	>> x = pi/3;
	>> y = sin(x)/cos(x);
	>> z = y - tan(x);
	>> z
	>> y
The last two lines do not end in semicolons so MatLab prints the results of evaluating z and y. The values of z and y were assigned in the y = and z = statements. The last two lines merely printed the values of the expressions z and y.