Vectors
Functions
Plotting
Polynomials
Matrices
Printing
Using M-files in Matlab
Getting help in Matlab
Matlab is an interactive program for numerical computation and data visualization; it is used extensively by control engineers for analysis and design. There are many different toolboxes available which extend the basic functions of Matlab into different application areas; in these tutorials, we will make extensive use of the Control Systems Toolbox. Matlab is supported on Unix, Macintosh, and Windows environments; a student version of Matlab is available for personal computers. For more information on Matlab, contact the Mathworks.
The idea behind these tutorials is that you can view them in one window while running Matlab in another window. You should be able to re-do all of the plots and calculations in the tutorials by cutting and pasting text from the tutorials into Matlab or an m-file.
Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between brackets, and set it equal to a variable. For example, to create the vector a, enter into the Matlab command window (you can "copy" and "paste" from your browser into Matlab to make it easy):
To make life easier, Matlab includes many standard functions. Each function is a block of code that accomplishes a specific task. Matlab contains all of the standard functions such as sin, cos, log, exp, sqrt, as well as many others. Commonly used constants such as pi, and i or j for the square root of -1, are also incorporated into Matlab.
Matlab even allows you to write your own functions with the function command; follow the link to learn how to write your own functions and see a listing of the functions we created for this tutorial.
It is also easy to create plots in Matlab. Suppose you wanted to plot a sine wave as a function of time. First make a time vector (the semicolon after each statement tells Matlab we don't want to see all the values) and then compute the sin value at each time.
In Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simply enter each coefficient of the polynomial into the vector in descending order. For instance, let's say you have the following polynomial:
To enter this into Matlab, just enter it as a vector in the following manner
would be represented in Matlab as:
You can also extract the roots of a polynomial. This is useful when you have a high-order polynomial such as
Finding the roots would be as easy as entering the following command;
Let's say you want to multiply two polynomials together. The product of two polynomials is found by taking the convolution of their coefficients. Matlab's function conv that will do this for you.
If you want to add two polynomials together which have the same order, a simple z=x+y will work (the vectors x and y must have the same length). In the general case, the user-defined function, polyadd can be used. To use polyadd, copy the function into an m-file, and then use it just as you would any other function in the Matlab toolbox. Assuming you had the polyadd function stored as a m-file, and you wanted to add the two uneven polynomials, x and y, you could accomplish this by entering the command:
Entering matrices into Matlab is the same as entering a vector, except each row of elements is separated by a semicolon (;) or a return:
Now you can multiply the two matrices B and C together. Remember that order matters when multiplying matrices.
Printing in Matlab is pretty easy. Just follow the steps illustrated below:
Macintosh
print -P<printername>If you want to save the plot and print it later, enter the command:
print plot.psSometime later, you could print the plot using the command "lpr -P
To print a m-file, just print it the way you would any other file, using
the command "lpr -P
There are
slightly different things you need to know for each platform.
Macintosh
Windows
Unix
for more information on any given command. You do need to know the name of
the command that you
are looking for; a list of the all the ones used in these tutorials is given in the
command listing; a link to this page can be
found at the bottom of every tutorial and example page.
Here are a few notes to end this tutorial.
You can get the value of a particular variable at any time by typing its
name.
Also, you may have noticed that so long as you don't assign a variable a
specific operation or result, Matlab with store it in a temporary variable
called "ans".
Using M-files in Matlab
There is a built-in editor for m-files; choose "New M-file"
from the File menu. You can
also use any other editor you like (but be sure to save the files
in text format and load them when you start Matlab).
Running Matlab from Windows is very similar to running it on a
Macintosh. However, you need
to know that your m-file will be saved in the clipboard.
Therefore, you must make sure that
it is saved as filename.m
You will need to run an editor separately from Matlab. The best
strategy is to make a
directory for all your m-files, then cd to that
directory before running both
Matlab and the editor. To start Matlab from your Xterm window,
simply type: matlab.
You can either type commands directly into matlab, or put all of the
commands that you will need
together in an m-file, and just run the file. If you put all of your
m-files in the same directory that
you run matlab from, then matlab will always find them.
Getting help in Matlab
Matlab has a fairly good on-line help; type
help commandname
B
B =
1 2 3
4 5 6
7 8 9
You can also have more that one statement on a single line, so long as you
separate them with either a semicolon or comma.
User feedback
Tutorials