Professional Documents
Culture Documents
Special Characters
[] forms matrices
() forms subscripts
, separates subscripts or matrix elements
; separates commands or matrix rows
% indicates comments
: generates matrices
+ scalar and array addition
- scalar and array subtraction
* scalar multiplication
.* array multiplication
/ scalar division
./ array division
^ scalar exponentiation
.^ array exponentiation
‘ transpose
NOTE: written like this, it will return every value of “x” as shown below
>> x=0:100
x=
Columns 1 through 23
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22
Columns 24 through 46
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45
Columns 47 through 69
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
62 63 64 65 66 67 68
Columns 70 through 92
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91
Columns 93 through 101
92 93 94 95 96 97 98 99 100
NOTE: by putting a “;” at the end, you prevent it from returning every value of
“x”
>> x=0:100;
>>
-End Example
Definitions
abs computes absolute value or magnitude
-Examples
>> abs(5) returns 5
>> abs(-5) returns 5
>> abs([1,2;6,-5]) returns
1 2
6 5
-End Example
acos computes arccosine
-Example:
NOTE: the “pi” represents π
>> cos(pi/4) returns 0.7071
>> acos(0.7071) returns 0.7854 (which is (π/4) written as a decimal)
-End Example
all determines if all values are true
ans stores expression value
-Example:
“ans” stores the answer to the last operation you did.
>> 27*9
ans =
243
>> ans
ans =
243
>> 7-2
ans =
5
>> ans
ans =
5
-End Example
any determines if any values are true
asin computes arcsine
-Example:
NOTE: “pi” represents π
>> sin(pi/3) returns 0.8660
>> asin(0.8660) returns 1.0471 (which is (π/3) written as a decimal)
-End Example
atan computes 2-quadrant arctangent
-Example:
NOTE: “pi” represents π
>> tan(pi/6) returns 0.5774
>> atan(0.5774) returns 0.5236 (which is (π/6) written as a decimal)
-End Example
atan2 computes 4-quadrant arctangent
axis controls axis scaling
-Example:
The following was gotten by typing “help axis”
AXIS([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes
on the current plot.
AXIS([XMIN XMAX YMIN YMAX ZMIN ZMAX]) sets the scaling for the
x-, y- and z-axes on the current 3-D plot.
AXIS([XMIN XMAX YMIN YMAX ZMIN ZMAX CMIN CMAX]) sets the
scaling for the x-, y-, z-axes and color scaling limits on the current axis (type
“help CAXIS” for more info on “CAXIS”).
V = AXIS returns a row vector containing the scaling for the current plot. If the
current view is 2-D, V has four components; if it is 3-D, V has six components.
AXIS AUTO returns the axis scaling to its default, automatic mode where, for
each dimension, 'nice' limits are chosen based on the extents of all line, surface,
patch, and image children.
AXIS MANUAL freezes the scaling at the current limits, so that if HOLD is
turned on, subsequent plots will use the same limits.
AXIS TIGHT sets the axis limits to the range of the data.
AXIS FILL sets the axis limits and PlotBoxAspectRatio so that the axis fills the
position rectangle. This option only has an effect if PlotBoxAspectRatioMode or
DataAspectRatioMode are manual.
AXIS IJ puts MATLAB into its "matrix" axes mode. The coordinate system
origin is at the upper left corner. The i axis is vertical and is numbered from top
to bottom. The j axis is horizontal and is numbered from left to right.
AXIS XY puts MATLAB into its default "Cartesian" axes mode. The coordinate
system origin is at the lower left corner. The x axis is horizontal and is numbered
from left to right. The y axis is vertical and is numbered from bottom to top.
AXIS EQUAL sets the aspect ratio so that equal tick mark increments on the x-
,y- and z-axis are equal in size. This makes SPHERE(25) look like a sphere,
instead of an ellipsoid.
AXIS IMAGE is the same as AXIS EQUAL except that the plot box fits tightly
around the data.
AXIS SQUARE makes the current axis box square in size.
AXIS NORMAL restores the current axis box to full size and removes any
restrictions on the scaling of the units. This undoes the effects of AXIS SQUARE
and AXIS EQUAL.
AXIS VIS3D freezes aspect ratio properties to enable rotation of 3-D objects and
overrides stretch-to-fill.
AXIS OFF turns off all axis labeling, tick marks and background.
AXIS ON turns axis labeling, tick marks and background back on.
Second in the matlab window, make sure your in the right directory, look at the
command cd to see what directory your in and then use ls to list the files in that
directory)
>> cd
D:\matlab
>> ls
. rigid.m
Third in the matlab window type the following lines of code to execute the ode45.
>> options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
>> [t,y] = ode45('rigid',[0 12],[0 1 1],options);
>> plot(t,y(:,1),'-',t,y(:,2),'-.',t,y(:,3),'.')
-End Example
ones generates matrix of ones
pause temporarily halts a program
pi represents the value π
plot generates a linear xy plot
-Example:
Here is an example to create a simple plot.
First let’s create two variables “x” and “y”.
>> x=-5.0:5
x=
-5 -4 -3 -2 -1 0 1 2 3 4 5
>> y=cos(x)
y=
Columns 1 through 7
0.2837 -0.6536 -0.9900 -0.4161 0.5403 1.0000 0.5403
Columns 8 through 11
-0.4161 -0.9900 -0.6536 0.2837
>> plot(x,y)
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
Now let’s make some labels on the figure and insert grids in the plot.
>>grid
>> title('Simple Plot')
>> xlabel('x values')
>> ylabel('y values')
Simple Plot
1
0.8
0.6
0.4
0.2
y values
-0.2
-0.4
-0.6
-0.8
-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
x values
-End Example
NOTE: (Various line types, plot symbols and colors may be obtained with
PLOT(X,Y,S) where S is a character string made from one element from any or
all the following 3 columns)
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
This was gotten by typing “help plot”
poly2sym converts a vector to a symbolic polynomial
polyfit computes a least-squares polynomial
polyval evaluates a polynomial’
pretty prints a symbolic expression in typeset form
print print the graphics window
prod determines product of values
qr computes the QR factorization of a matrix
quad computes the integral under a curve (Simpson)
quad8 computes the integral under ac curve (Newton-Cote)
quit terminates Matlab (same as the command exit)
rand generates a uniform random number
randn generates a Gaussian random number
rem computes remainder from division
-Example:
NOTE: rem(x,y) = the remainder of “x / y”
>> rem(6,3) returns 0
>> rem(4,3) returns 1
>> rem(5,3) returns 2
-End Example
remez designs an optimal FIR digital filter
residue performs a partial-fraction expansion
rlocus computes the root locus
round rounds to the nearest integer
-Example:
>> round(2.3) returns 2
>> round(2.5) returns 3
>> round(-2.3) returns -2
>> round(-2.5) returns -3
-End Example
save saves variables in a file
semilogx generates a log-linear plot
-Example:
>> x=0:0.5:50;
>> y=5*x.^2;
>> semilogx(x,y)
>> title('Polynomial - log/linear'),xlabel('x'),ylabel('y')
-End Example
semilogy generates a linear-log plot
-Example:
>> x=0:0.5:50;
>> y=5*x.^2;
>> semilogy(x,y)
>> title('Polynomial - linear/log'),xlabel('x'),ylabel('y')
-End Example
sign generates –1,0,1 based of sign
-Example:
>> sign(0) returns 0
>> sign(5) returns 1
>> sign(-5) returns -1
-End Example
simple shortens a symbolic expression
-Example:
NOTE: you need to do “>> syms x” (where “x” is the variable your using in the
simple call) before you can do this command
>> syms x
>> simple(cos(x)^2+sin(x)^2) returns 1
>> simple(2*cos(x)^2-sin(x)^2) returns 3*cos(x)^2-1
>> simple(cos(x)^2-sin(x)^2) returns cos(2*x)
>> simple(cos(x)+(-sin(x)^2)^(1/2)) returns cos(x)+i*sin(x)
>> simple(cos(x)+i*sin(x)) returns exp(i*x)
>> simple( (x+1)*x*(x-1)) returns x^3-x
>> simple(x^3+3*x^2+3*x+1) returns (x+1)^3
>> simple(cos(3*acos(x))) returns 4*x^3-3*x
-End Example
simplify simplifies a symbolic expression
-Example:
NOTE: you need to do “>> syms x c alpha beta” (where “x”, “c”, “alpha”, and
“beta” is the variable your using in the simplify call) before you can do this
command
>> syms x c alpha beta
>> simplify(sin(x)^2 + cos(x)^2) returns 1
>> simplify(exp(c*log(sqrt(alpha+beta)))) returns (alpha+beta)^(1/2*c)
-End Example
sin computes sine of angle
-Example:
NOTE: the “pi” represents π
>> sin(pi/3) returns 0.8660
>> sin(pi/5) returns 0.5878
End Example
size prints row and column dimensions
-Example:
>> size([1,2]) returns 1 2
>> size([1;2]) returns 2 1
>> size([1,2;2,4]) returns 2 2
>> size([1,2,3;2,4,7]) returns 2 3
-End Example
solve solves an equation
-Example:
NOTE: in these examples, what is being returned is what “x” equals.
>> solve('x+1=0') returns -1
>> solve('x^2-1=0') returns
[ 1]
[ -1]
>> solve('x^2+5*x+7=3') returns
[ -4]
[ -1]
You can also use solve to solve multiple equations with multiple unknowns.
>> [x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') returns
x=
[ 1]
[ 3]
y=
[ 1]
[ -3/2]
-End Example
sort sorts values
-Example:
>> sort([5,2,3]) returns 2 3 5
>> sort([5,2,3;4,7,8]) returns
4 2 3
5 7 8
-End Example
sqrt computes square root
-Example:
>> sqrt(4) returns 2
>> sqrt(2) returns 1.4142
-End Example
ss2tf converts state-space to transfer function
ss2zp converts state-space to zero-pole-gain
std computes standard deviation
-Example:
>> std([5,2,7,8,6,10,25,69]) returns 22.3159
-End Example
step computes the unit step response
subplot splits graphics window into subwindows
Subplot is a very useful command. Say you have a value for x and y, and you want to
graph it as a linear, log, or combination of the two. Using the subplot command you can
do it all in one figure, allowing you to compare the graphs.
-Example:
>> x=0:0.5:50;
>> y=5*x.^2;
>> subplot(2,2,1),plot(x,y),...
title('Polynomial - linear/linear'),...
ylabel('y'),grid,...
subplot(2,2,2),semilogx(x,y),...
title('Polynomial - log/linear'),...
ylabel('y'),grid,...
subplot(2,2,3),semilogy(x,y),...
title('Polynomial - linear/log'),...
xlabel('x'),ylabel('y'),grid,...
subplot(2,2,4),loglog(x,y),...
title('Polynomial - log/log'),...
xlabel('x'),ylabel('y'),grid
-End Example
sum determines sum of values
svd converts the SVD factorization of a matrix
sym2poly converts a symbolic expression to a coefficient vector
symadd adds two symbolic expressions
symdiv divides two symbolic expressions
symmul multiplies two symbolic expressions
sympow raises a symbolic expression to a power
symsub subtracts two symbolic expressions
symvar returns independent variable
tan computes tangent of angle
-Example:
NOTE: the “pi” represents π
>> tan(pi/5) returns 0.7265
>> tan(pi/23) returns 0.1374
-End Example
tf2ss converts transfer function to state-space
tf2zp converts transfer function to zero-pole-gain
title adds title to a plot
(see plot for an example)
unwrap removes 2π discontinuities in a phrase angle
what list variables
while generates a loop structure
who lists variables in memory
(lists all the variables you made)
whos lists variables in memory plus sizes
xlabel adds x-axis label to a plot
(see plot for an example)
ylabel adds y-axis label to a plot
(see plot for an example)
yulewalk designs an optimal IIR digital filter
zeros generates matrix of zeros
zp2ss converts zero-pole-gain to state-space
zp2tf converts zero-pole-gain to transfer function