You are on page 1of 6

MATLAB BASICS

Basic commands and syntax If you type in a valid expression and press Enter, MATLAB will immediately execute it and return the result, just like a calculator.

Building arrays The simplest way to construct a small array is by enclosing its elements in square brackets.

Matrix information commands.

An especially important array constructor is the colon operator.

The format is first:step:last. The result is always a row vector, or the empty matrix if last < first. Referencing elements It is frequently necessary to access one or more of the elements of a matrix. Each dimension is given a single index or vector of indices. The result is a block extracted from the matrix.

The colon is often a useful way to construct these indices. There are some special syntaxes: end means the largest index in a dimension, and : alone is short for 1:endi.e. everything in that dimension. Note too from the last example that the result need not be a subset of the original array.

Relational operators

Matrix operations The arithmetic operators +,-,*, are interpreted in a matrix sense. When appropriate, scalars are expanded to match a matrix.

The apostrophe produces the complex-conjugate transpose of a matrix. This corresponds to the mathematical adjoint of the linear operator represented by the matrix.

Array operations Array operations simply act identically on each element of an array. We have already seen some array operations, namely + and -. But the operators *, , , and / have particular matrix interpretations. To get element wise behavior appropriate for an array, precede the operator with a dot.

Another kind of array operation works in parallel along one dimension of the array, returning a result that is one dimension smaller.

Reducing functions.

Programming with MATLAB


Conditional Control if, else, switch if, else, and elseif The if statement evaluates a logical expression and executes a group of statements when the expression is true. The optional elseif and else keywords provide for the execution of alternate groups of statements. An end keyword, which matches the if, terminates the last group of statements. The groups of statements are delineated by the four keywordsno braces or brackets are involved.

switch and case The switch statement executes groups of statements based on the value of a variable or expression. The keywords case and otherwise delineate the groups. Only the first matching case is executed. There must always be an end to match the switch.

Note: Unlike the C language switch statement, MATLAB switch does not fall through. If the first case statement is true, the other case statements do not execute. So, break statements are not required.

Loop Control for, while, continue, break for The for loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements:

while The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements.

A break immediately jumps execution to the first statement after the loop.

Graphics
Graphics are represented by objects that are rigidly classified by type. The available types can be found in the online documentation. The most important object types are the figure, which is a window onscreen, the axes, into which data-related objects are drawn, and graphics primitives such as line, surface, text, and others. These types obey a hierarchy. For instance, a figure can be parent to one or more axes, each of which has graphical primitives as children, and so on. Each container type provides a local coordinate system for its children to refer to. Thus, objects drawn within an axes remain correctly rendered if the axes are moved inside its parent figure. 2D plots The most fundamental plotting command is plot. Normally it uses line segments to connect points given by two vectors of x and y coordinates. Here is a simple example.

A line object is drawn in the current axes of the current figure, both of which are created if not yet present. The line may appear to be a smooth, continuous curve. However, its really just a game of connect-the-dots, as you can see clearly by entering

Now a circle is drawn at each of the given data points. Just as t and sin(t) are really vectors, not functions, curves in MATLAB are really joined line segments. If you now say you will get a red curve representing cos(t). The curve you drew earlier (in fact, its parent axes too) is erased. To add curves, rather than replacing them, first enter hold on.

See a bunch more by typing help graph2d.

2D plotting commands

3D plots Plots of surfaces and such for functions f (x, y) also operate on the connect-the-dots principle, but the details are more difficult. The first step is to create a grid of points in the xy plane. These are the points where f is evaluated to get the dots in 3D.

Annotation Graphs of data usually need labels and maybe a title. For example,

You might also like