You are on page 1of 5

Matlab Commands and Functions

Common Operators
Operator Remark
+ Plus; addition operator; e.g., 5 + 3, a + b, A + B where a and b are variables; A and
B are matrixes of compatible dimensions
- Minus; subtraction operator; e.g., 5 – 3, a – b, A – B where a and b are variables; A
and B are matrixes of compatible dimensions
* Scalar and matrix multiplication operator; e.g., a*b, A*B where a and b are scalar
variables; A and B are matrixes of compatible dimensions (for matrix cross
product)
.* Array multiplication operator; e.g.; a.*b, A.*; a and b are vectors of compatible
lengths; A and B are matrixes of compatible dimensions (for element-by-element
multiplication, or matrix dot product)
^ Scalar and matrix exponentiation operator; e.g., a^5 for a scalar variable raised to
the power 5; A^2 where A is a square matrix and the operation is equivalent to
A*A (cross product)
.* Array exponentiation operator; e.g.; a.^2 where a is a vector and the operation
implies to raise to the power of 2 each of the element in the vector; A.^2 where A is
matrix and the operation implies to raise to the power 2 each of the elements in the
matrix.
/ Right-division operator; e.g.; 5/3, a/b, A/c where a nd b are variables; A is a matrix
and c is a constant
./ Array right-division operator; e.g.; p./q; A./B where p and q are vectors of
compatible lengths; A and B are matrixes of compatible dimensions
: Colon; generates regularly spaced elements and represents an entire row or column;
e.g., v = [1:1:5] = [1 2 3 4 5]
, Comma; separates statements and elements in a row; e.g., v = [1, 2, 3, 4] creates a
row vector
; Semicolon; separates columns and supress display; e.g., v = [1; 2; 3] creates a
column vector; J = 2*3; supress display of answer after the command line is written
in the command window
… Ellipsis; line-continuation operator; e.g., J = 2*x1 + x2^2 + …
X1*x2*x; means that the equation
continues to the second line when written in the command window or in a m-file.
% Percent sign; designates a comment and specifies formatting; e.g., J = a*10; %
multiplied a with the constant 10 means that the after the % sign, the line is
considered a comment, and not for Matlab execution

1
Common Commands used in Command Window
Command Remark
clc To clear Command Window
clear Removes variables from memory (workspace)
exist Checks for existence of a file or variable, e.g., exist(‘CSTR’), will return 1 if
CSTR is a variable, and return 2 if CSTR is a m-file.
quit Stops Matlab
who Lists current variables in the workspace
help Searches for a help topic; e.g., help /; after pressing enter it will show help
topic for the right-division operator

Matrix Commands
Command Remark
det Computes determinant of an array; e.g., A = [2 8; 3 5] write in Command Window
>>det(A) will return the determinant = -14
inv Computes inverse of a matrix; e.g., A = [2 8; 3 5] write in Command Window >>
inv(A) will return an inverse matrix = [-0.3571 0.5714; 0.2143 -0.1429];
rank Computes rank of a matrix; e.g.; A = [2 8; 3 5] write in Command Window
>>rank(A) will return 2
‘ Computes transpose of a matrix; e.g., A = [2 8; 3 5] write in Command Window
>>A’ will return = [2 3; 8 5]

Plotting Commands
Command Remark
plot Generate x-y plot; e.g.; plot(t,y) will create a plot of y against t
axis Creates axes objects; e.g.;
>> pplot(t,y);
>> axis([0 5 0 1];
The commands create a plot of y against t, with x-axis ranges from 0 to 5 and y
axis ranges from 0 to 1
close Closes the current plot
Close all Closes all plots
figure Opens a new figure window; e.g.;
>> figure(1);
>> plot(t1,y1);
>> figure(2);
>> plot(t2,y2);
These commands will create 2 figures; one for y1 versus t1 and another for y2
versus t2

2
grid Displays gridlines
xlabel Adds text label to x-axis; e.g.,
>> xlabel(‘time’);
This will add “time” as a label to the x-axis
ylabel Add text label to y-axis; e.g.,
>> ylabel(‘Temperature’);
This will add “Temperature” as a label to the y-axis
legend Legend placement on a figure by mouse; e.g.,
>> figure(2)
>>plot(t,y)
>> legend(‘TEST2’)
These will create figure 2, with y versus t plot, and creates the legend “TEST2”
upon placing mouse on figure 2
text Places string in figure; e.g., if we want to inset the word “YES” at location (3,7):
>>plot(t,y);
>>text(3,7,’YES’);
These will create a plot of y versus t, and places the word “YES” at the location
(3,7)
subplot Creates plots in subwindows; e.g.;
>>subplot(2,2,1); will create a figure window with 4 subplots (2 rows and 2
columns)
Example of applications:
>> subplot(2,1,1); creates a figure window with 2 subplot (2 rows and 1 column)
>> plot(t1,y1)
>> subplot(2,1,2);
>> plot(t2,y2);
Thse will create a figure with 2 rows of subplots; in the 1st subplot y1 versus t1
and in the second subplot y2 versus t2;

Mathematical Functions
Function Remark
exp(x) Exponential; ex
log(x) Natural logarithm; ln(x)
log10(x) Base 10 logarithm; log10(x)
sqrt(x) Square root; √x
cos(x) Cosine; cos(x)
sin(x) Sine; sin(x)
tan(x) Tangent; tan(x)

3
acos(x) Inverse cosine; arcos x = cos-1(x)
asin(x) Inverse sine; arcsin x = sin-1(x)
atan(x) Inverse tangent; arctan x = tan-1(x)
cosh(x) Hyperbolic cosine; cosh(x)
sinh(x) Hyperbolic sine; sinh(x)
tanh(x) Hyperbolic tangent; sinh(x)/cosh(x)
acosh(x) Inverse hyperbolic cosine; cosh-1(x)
asinh(x) Inverse hyperbolic sine; sinh-1(x)
atanh(x) Inverse hyperbolic tangent; tanh-1(x)
mean Calculates the average
median Calculates the median
std Calculates the standard deviation
ceil Rounds to the nearest integer toward ∞
floor Round to the nearest integer toward -∞
round Rounds towards the nearest integer
sign Signum function
eig Computes the eigenvalues of a matrix
poly Computes polynomial from roots
polyfit Fits a polynomial to data
roots Computes polynomial roots
interp1 Linear and cubic-spline interpolations of a function of one variable
interp2 Linear interpolation of a function of two variables

ODE Solvers
Solver Remarks
ode45 Nonstiff, low-order solver
ode23 Nonstiff, medium-order solver
ode113 Nonstiff, variable-order solver
ode23s Stiff, low-order solver
ode23t Moderately stiff, trapezoidal approximation solver
ode15s Stiff, variable-order solver

4
Common Functions for Control System Toolbox
Function Remark
tf Transfer function model; e.g.; tf(Sys) where Sys is a state-space model,
then the function convert Sys to a transfer function; tf(‘s’) defines s is
Laplace domain
zpk Create zero-pole-gan model
ss State-space mode; e.g., ss(Gp) will convert the transfer function Gp into a
state-space model
pid Create PID controller in parallel form
pidstd Create a PID controller in standard form
pade Padé approximation of model with time delays
feedback Feedback connection of multiple models
series Series connection of two models
parallel Parallel connection of two models
frd Creates frequency-response data model
c2d Converts model from continuous to discrete-time
d2c Converts model from discrete-time to continuous-time
balred Model order reduction
modred Eliminates states from state-space models
step Step response plot of dynamic system
impulse Impulse response plot of dynamic system
bode Bode plot of frequency response
nyquist Nyquist plot of frequency response
nichols Nichols chart of frequency response
sigma Singular values plot of dynamic system
dcgain Low-frequency gain of LTI system
bandwidth Frequency response bandwidth
norm Norm of linear model
db2mag Converts decibels (dB) to magnitude
mag2db Converts magnitude to decibels (dB)
pole Poles of dynamic system
zero Zeros and gain of SISO dynamic system
damp Natural frequency and damping ratio or factor
margin Gain margin, phase margin, and crossover frequencies
rlocus Root locus plot of dynamic system

You might also like