next up previous contents
Next: Building Matrices Up: The Basics Previous: The Basics

Fundamentals of Use

Your interaction with MATLAB is in the form of you giving MATLAB a statement, possibly followed by MATLAB giving you a result, then back to you giving a statement. A statement is usually of the form of either

.1ex>> variable = expression or just

.1ex>> expression The result (which is almost always a matrix -- see gif Building Matrices) of evaluating expression is immediately displayed. To suppress display, end the line with a semicolon; this will greatly speed up calculations resulting in large matrices. If no variable is given in which to store the result of expression, it is automatically stored in the variable ans.

.1ex>> x = 3 + 2
x =

.1ex>> y = 4 + 6;
.1ex>> y
y =

.1ex>> 5 + 3
ans =

To put more than one statement on a single line, separate them by commas or semicolons. To have a single statement span several lines, end each line before the last with a space followed by three or more periods.

.1ex>> a = 1 + 1, b = 2 + 2
a =

b =

.1ex>> c = 1 + 2 + 3 ...
.1ex>> + 4 + 5 + 6 + ...
.1ex>> 7 + 8 + 9 + 10
c =
A function may have more than one output, and moreover, all the outputs may depend on how many outputs you request. For example, [val vec] = eig(matrix) gives a (diagonal) matrix of eigenvalues of matrix and a matrix of the eigenvectors of matrix; however, v = eig(matrix) gives a vector of eigenvalues -- which is not the same as either of the former two outputs. See the help on a function for details; it is generally wise to look at the help before first using any function.

MATLAB is, by default, case-sensitive to names, so myvar and myVAR are distinct. Typing casesen toggles this sensitivity on and off.

Normal scalar arithmetic works; you can use MATLAB as a basic calculator. The arithmetic functions are +, -, tex2html_wrap_inline2423 , /, and ^ (exponentiation). Use parentheses as usual to specify precedence. Also see help arith.

MATLAB always does complex math, though it will display purely real results intelligently by omitting the imaginary part. Both i and j are equal to the square root of -1. If you happen to overwrite both of these (by using them as variables for something else), you can recreate with, e.g.

.1ex>> myi = sqrt(-1) or do clear i to remove your definitions of i, leaving it with its original meaning.

A complex number is specified in the form 1+2i or 1-2i, where there are no spaces inside the number. Spaces can cause complex numbers in vectors and matrices to seem like two numbers, which will produce errors or at least undesired results.

.1ex>> x = (1+2i)2
x =
+ 4.0000i Mathematical functions will accept complex arguments and, in general, give complex answers (e.g. the trigonometric functions and arcfunctions are defined over the complex plane). Specifically complex manipulations are available in the form of real and imag (giving the real and imaginary parts of their argument), conj for complex conjugation, abs for absolute value, and angle for the phase (in radians; also see help unwrap).

See help elfun for a list of trigonometric, exponential, complex, and numeric elementary scalar functions. For more specialized functions see help specfun.

There are several commands to help with variable assignments; who will list the variables currently in use in this session, and whos lists the variables along with their sizes and whether they are purely real. Use clear variable to remove variable from those currently in use; clear by itself removes all variables.

The MATLAB-defined constant eps gives the machine unit roundoff, typically about tex2html_wrap_inline2429 ; this is useful in specifying tolerances. The constant pi exists. Some other predefined constants are realmax and realmin for largest and smallest (positive) floating point numbers, inf for infinity, NaN for Not-a-Number, and version for the MATLAB version number.

To halt a runaway computation, use Control-C. Use more on, more off, and more(n) to have MATLAB scroll as the UNIX more, turn off such scrolling, and set the number of lines per page (default 23). This is useful with calculations giving long results and with long help entries. Use clc to clear the command window, and home to send the cursor to the upper left without clearing.

Line editing in MATLAB will Do The Right Thing: you can use the up and down arrows to get at old command lines, left and right arrows to move in and edit these lines, etc. For more details see help cedit or type cedit in MATLAB.


next up previous contents
Next: Building Matrices Up: The Basics Previous: The Basics

sepherke
Sat Mar 21 21:42:28 EST 1998