Matlab: A Brief Manual: I. Running Matlab B. Arrays
Matlab: A Brief Manual: I. Running Matlab B. Arrays
I. RUNNING MATLAB
On first entry the computer opens a window in which information can be entered from the keyboard (this is called the
command window), and displays the following prompt symbol:
>>
From this point on, you type in individual MatLab commands
immediately following the program prompt. They will be processed when you hit the return key. Before you start it is
worth noting that the help facility in MatLab is particularly
good. If ever you dont know what to do you can often find
the answer by typing help functionname at the prompt.
A.
Variables
B.
Arrays
[8 1 6; 3 5 7; 4 9 2]
1 6
5 7
9 2
2
C.
Variable Arithmetic
A. Matrix Transpose
D. Command-line Editing
B. Matrix Addition and Subtraction
C. Matrix Multiplication
transpose
subtraction
\
left division
*
multiplication
/
right division
^
power
If a and b are scalars, the right division a/b results in a divided by b, whereas the left division is equivalent to b/a. In
the case where A and B are matrices, B/A returns the solution
of X*A = B and A\B yields the solution of A*X = B.
A*B, B*A;
B*A, A*B;
E*E, E*E;
E*F, E*F, F*E;
3
D. Rounding Floating Point Numbers to Integers
Functions which do this are round, fix, ceil, and floor. Here
are examples of these.
>>
>>
>>
>>
>>
F = [-.5 .1 .5];
round(F)
fix(F)
ceil(F)
floor(F)
E. Element-by-element Operations
F. Colon Notation
>> 3 < 5
ans =
1
>> a = 3 == 5
a =
0
When logical operators are applied to matrices of the same
size, their relation is a matrix of 0s and 1s giving the value of
the relation between corresponding entries.
>> A = [ 1 2; 3 4 ];
>> B = [ 6 7; 8 9 ];
>> A == B
ans =
0 0
0 0
>> A < B
ans =
1 1
1 1
To see how the other logical operators work, you should
also try
>>
>>
>>
>>
>>
~A
A&B
A & ~B
A|B
A |~A
A. If-end
4
Here the condition is a logical expression that will evaluate
to either true or false (i.e., with values 1 or 0). When it evaluates to 0, the program control moves on to the next part of
the program. You should keep in mind that MatLab regards
expressions like (A DD B) and (A <D B) as functions with
values 0 or 1.
>> a = 1;
>> b = 2;
>> if a < b, c = 3; end;
>> c
c =
3
B.
If-else-end
B.
While Loop
Another variation is
if <condition1>,
<program1>
elseif <condition2>,
<program2>
end
Now if condition1 is 1, then program1 is executed, if condition1 is 0 and if condition2 is 1, then program2 is executed,
and otherwise control is passed on to the next part of the program.
V.
>>while <condition>,
<program>,
end
where condition is a MatLab function, as with the branching
construction. The program program will execute as long as the
value of condition is not 0. WHILE loops carry an implicit
danger in that there is no guarantee in general that you will
exit a while loop.
C. break
LOOPING CONSTRUCTIONS
D. continue
A. FOR Loops
5
F. error
6
VII.
GRAPHICS
B. Three-dimensional Graphics :
Function
Description
plot(x,y)
plot y vs x
plot(x,y1,x,y2,x,y3) plot y1, y2 and y3 vs x on same graph
xlabel(x axis label)
labels x axis
ylabel(y axis label)
labels y axis
title (title of plot)
puts a title on the plot
MatLab graphics windows will contain one plot by default.
The command subplot can be used to partition the screen so
that up to four plots can be viewed simultaneously. For more
information, type
plot3(x,y,z)
mesh(x,y,z)
Two-dimensional Plots
In order to draw more than one plot on the same graph, use
the command hold. The command hold freezes the current
graphics screen so that subsequent plots are superimposed on
it. Entering hold again releases the hold. The commands
hold on and hold off are also available.
You can override the default linetypes and pointtypes. For
example, these commands render a dashed line and dotted line
for the first two graphs while for the third the symbol is placed
at each node.
>>
>>
>>
>>
contour(z)
Description
Plots a line in 3-space through the points
whose coordinates are the elements of x, y
and z, where x, y and z are three vectors of
the same length.
Creates a three-dimensional perspective plot
of the elements of the matrix z. It is drawn like
a wire mesh, defined by the z-coordinates of
points above a rectangular grid in the
x-y plane.
Creates a three-dimensional perspective plot
of the elements of the matrix z. It is redrawn
as a continuous surface, defined by the
z-coordinates of points above a rectangular
grid in the x-y plane.
Creates a contour plot of matrix z treating the
values of z as heights above a rectangular grid
in the x-y plane.
z = 0:pi/50:10*pi;
x = sin(z);
y = cos(z);
plot3(x,y,z)
z = x.^2 - y.^2;
mesh(z)
surf(x,y,z)
contour(x,y,z)
Consult help plot3, help mesh, help surf, help contour, for
more information.