Legacy Documents » Matlab Commands
by Ross Blauwkamp
First, the most important command in MATLAB: | ||
>> help command_name | Displays complete information about any command. Take note of any cross references at the end of each help entry. help with no command specified lists all categories of available commands. Note: helpwin brings up a separate gui help window -- useful if you're doing other things in the main window. | |
Interacting with MATLAB: | ||
>> command >> command; |
A semicolon after any command suppresses output from that command to the MATLAB window (but not, for example, to a figure) - especially useful if the output is a very long vector. | |
>> |
Recalls the last command entered. You
can repeat it to go farther back, or press |
|
>> abc |
Recalls the last command entered that starts with "abc". | |
>> more on >> more off |
Turns on and off more, which displays all output (including help information) one screen at a time. | |
>> who | Tells you what variables currently exist. | |
>> clear >> clear variable |
Deletes all variables, or the specified variable. | |
>> format long g >> format compact |
Displays variables (but not systems)
with more digits. Does not affect storage or computation. Gets rid of the extra blank lines in the display. |
|
Commands for making and formatting plots: | ||
>> p1=plot(t1,y1); >> delete(p1) |
Plots y1 vs. t1. Note that setting the plot equal to a name lets you delete it afterwards - useful if you're plotting several things in the same figure and make a mistake. y1 and t1 must be vectors of the same size. | |
>> plot(y(:,1),y(:,2)) | Plots the second column of matrix y vs. the first column. See note at end of section. | |
>> subplot(m,n,p) | Breaks the figure up into mxn separate graphs, and selects the pth one as current; if there are already mxn graphs, leaves graphs as they are and selects the pth one. | |
>> xlabel('label_text') >> ylabel('label_text') >> title('title_text') |
Labels the axes or the entire subplot with specified text (note single quotes). | |
>> g1 =
gtext('plot_text'); >> delete(g1) |
Lets you place the given text (in single quotes) by clicking on the graph. Note that setting the gtext equal to a name lets you delete it afterwards. | |
>> grid | Toggles on and off a grid superimposed over the current graph; you can also type grid on and grid off. | |
>> sgrid | Draws a grid on a plot of the s plane (root locus plot or pole and zero locations), which consists of lines of constant damping coefficient (zeta) and natural frequency (wn). | |
>> hold | Toggles on and off the plot hold, which adds new plots to any already in the graph (without it, new plots delete previous ones). You can also type hold on and hold off. | |
>> axis([xmin
xmax ymin ymax]) >> axis auto |
Sets the limits of the x and y axes manually, or lets them be set automatically. There are also many other options available for axis. | |
>> zoom | Lets you select an area of the plot (using the mouse) to zoom in on. Also, clicking the left mouse button once will zoom in, and clicking the right button will zoom out, by a factor of two. Double clicking the right mouse button returns to the original scale; typing zoom again turns off the zoom function. | |
>> [x, y] = ginput(n) >> [x, y] = ginput |
Lets you input the coordinates of points
on the graph with the mouse; collects n points and stores
them in the vectors x and y, or if n is absent, keeps
collecting points until you press |
|
Important Note: | As of Matlab 5.3, the plot window contains tools to annotate and customize the plots. The commands listed in pink and italics above will still work, but can all be done by using the editting capabilities of the plot window. | |
Matrix and vector manipulation commands: | ||
>> k=linspace(k1,k2) >> k=linspace(k1,k2,n) |
Returns a vector of 100 (or, if specified, n) points equally spaced between x1 and x2. | |
>> max(y) | Returns the largest element in the vector y. | |
>> inv(A) | Returns the inverse of the square, nonsingular matrix A. | |
>> det(A) | Calculates the determinant of the matrix A. | |
>> eig(A) >> [V,D] = eig(A) |
Returns the eigenvalues of A, or sets V to a matrix containing the eigenvectors of A and sets D to a diagonal matrix containing the corresponding eigenvalues. | |
>> rank(A) | Returns the rank of any matrix A. | |
Commands useful in system analysis: | ||
>> roots(f) | Returns the roots of a polynomial, where f is a vector containing the coefficients of the polynomial. | |
>> conv(P1,P2) | Multiplies two polynomials (P1 and P2 are vectors containing the coeffiecients of the polynomials) and returns the resulting coefficients. This is actually a convolution of the two vectors, which also works as coefficient multiplication. | |
>> sys1=tf(num,den) >> sys2=ss(A,B,C,D) >> sys1=tf(sys2) >> sys2=ss(sys1) |
Creates a system, as a transfer function or state-space representation. Also converts between two different representations of a system. The zpk (zero/pole/gain) command works similarly. | |
>> [R,P,K]=residue(num,den) | Finds the partial fraction expansion of
a function H(s), where num is a vector containing the
coefficients of the numerator, and den of the denominator,
of H(s). Returns the numerators (R) and poles (P) of the
partial fractions and the remaining polynomial (K): H(s) = R1/(s-P1) + R2/(s-P2) + ... + Rn/(s-Pn) + K(s). |
|
>> sys3=series(sys1,sys2) | Finds the result of putting Systems 1 and 2 in series, and returns either the resulting transfer function numerator and denominator or the resulting state space matrices. Mixing system descriptions will work. | |
>> sys3=feedback(sys1,sys2) >> sys2=feedback(sys1,tf(1,1)) |
Finds the result of adding System 2 as a feedback loop to System 1, assuming a negative feedback, and returns either the resulting transfer function numerator and denominator or the resulting state space matrices. | |
>> impulse(sys) >> step(sys) |
Plots the impulse response or step response of the given system. Useful trick: if you have a Laplace transform F(s) of a time function f(t), plotting the impulse response of F(s) is the same as plotting f(t). | |
>> [y,t,x]=impulse(sys) >> [y,t,x]=step(sys) |
Giving impusle and step output variables returns the output (y), time (t), and states(x) (if state space) vectors, which you can then plot or manipulate. | |
>> initial(sys,x0) | Plots the behavior of the given state-space system with initial condition x0 and no input. | |
>> lsim(sys,u,t,x0) | Plots the response of the given system to the input signal u(t). The initial condition x0 can be added for state-space systems. | |
>> bode(sys1,sys2,...) >> [mag.phase,w]=bode(sys,w) >> nyquist(sys) >> {re,im,w]=nyquist(sys,w) |
Plots the Bode or Nyquist diagram for the given system(s). A plot is drawn if no return arguments. The frequency points may be specified in the vector w . | |
>> margin(sys) >> [gm,pm,wcg,wcp]=margin(sys) |
Finds the gain margin and phase margin, and the frequencies at which they occur, of the given system. If run with no output arguments, margin also displays the Bode plot, with the margins marked on it and their values displayed in the figure title. | |
>> rlocus(sys,k) | Plots the root locus for the given system, i.e. where den(s) + k*num(s) = 0 (or equivalent for the state space form). The vector of values for k is optional. | |
>> rlocfind(sys) >> [k,poles]=rlocfind(sys) |
Lets you select a pole location from a root locus plot using the mouse, and returns the value of k needed to give such a pole, as well as all the resulting pole locations in the vector poles (if present). rlocfind picks the point on the locus closest to the crosshairs; note that you must already have the root locus graphed to be able to see points you might want to pick. | |
>> sys2=canon(sys1,'form') | Finds a canonical form of the given system; the argument "form" can be either "modal" or "companion" (in single quotes). | |
>> rltool | Opens a tool for designing controllers using the root locus plots. |