EECS 210 A VERY QUICK INTRODUCTION TO MATLAB Winter 2001 ***************************************************************************** MATLAB (short for MATrix LABoratory) is a "computing environment" for computation. It is used almost universally in control, communications and signal processing. MATLAB is used in EECS 210 primarily for plotting and simple computations. Several problem sets will require MATLAB to plot signals and frequency responses. MATLAB is used more extensively in EECS 316, 451, and most senior and graduate courses in control, communications and signal processing. TO INVOKE MATLAB: Type "matlab" at a UNIX prompt. Double-click the MATLAB icon on a PC or Mac (in any CAEN lab). When you get the MATLAB prompt ">>" you are ready to rumble! **************************************************************************** TO PLOT: To plot the function x(t)=5sin((2/3)pi t)+4, type the following: >>t=[1 2 3 4 5 6 7 8 9 10]; >>x=5*sin(2*pi*t/3)+4; >>plot(t,x) This won't be a good plot since it is only based on 10 values of t. Try replacing the first line with ">>t=1:100;" (then retype the other two lines). This is equivalent to ">>t=[1 2...99 100]" but it is easier to type! TO PUT SEVERAL PLOTS ON A PAGE: Use "subplot" as follows: >>subplot(3,2,1),plot(t1,x1); subplot(3,2,2),plot(t2,x2); subplot(3,2,3),plot(t3,x3) >>subplot(3,2,4),plot(t4,x4); subplot(3,2,5),plot(t5,x5); subplot(3,2,6),plot(t6,x6) COMMENTS: 1. The ";" suppresses display of the results. Try the above without the ";". 2. If you want to plot the functions x(t)=sin(t^2) or sin(1/t), ">>x=sin(t^2)" or ">>x=sin(1/t)" will produce an error message. This is because MATLAB can't square or take the reciprocal of arrays. To avoid this problem, use ">>x=sin(t.^2)" or ">>x=sin(1./t)" The "." means "compute this element by element" for each element of the array. 3. ">>t=3:0.2:8;" is equivalent to ">>t=[3 3.2 3.4...7.8 8];" Try this without the ";" to see for yourself. 4. Remember that t is an array and x defined as a function of t will also be an array. ************************************************************************************ POLYNOMIALS AND ROOTS: To solve x^2-50x+160=0 do the following: >>x=[1 -50 160]; >>r=roots(x) Then MATLAB prints "r=46.5639 3.4361" >>p=poly(r) finds the polynomial with roots r. MATLAB prints "p=1.0000 -50.0000 160.0000" ************************************************************************************ M-FILES: Retyping the same thing over and over gets tiresome after awhile. "m-files" of the form "jerk.m" are MATLAB programs that can be run and rerun. To create an m-file do the following: 1. Use a text editor (such as vi or emacs in UNIX, notepad on PCs) to create a text file of MATLAB commands, one on each line: t=0:0.1:10; x=4+5*sin(2*pi*t/3); plot(t,x) You can do this in MATLAB itself by selecting "new" from the "file" menu. 2. Save the file as "jerk.m" Make sure this is in the current directory. 3. Back in MATLAB, type ">>jerk" (no extension). This executes the commands. 4. You can add comment statements by beginning a line with "%", for example, "% This is my first MATLAB program" Add this anywhere in jerk.m 5. You can use "for-end," "if-else-break-end," and "while-end" programming. 6. To re-edit an m-file, select "open" from the "file" menu. ****************************************************************************** OTHER COMMANDS: The following are useful: ">>clear" This clears (renders undefined) all variables. Once you define a variable, MATLAB remembers it until you clear it. This can cause trouble if you try to redefine it later. ">>clear t" clears ONLY the variable t. ">>help print" Shows you how to label plots, axes, etc. PRINTING: To print out your plot, do EITHER of the following: Select "print" from the "file" menu, OR Type ">>print" This prints out the contents of whichever window is active (in the foreground). On a UNIX workstation, at a UNIX prompt type: "setenv PRINTER printername" to specify which printer you wish to use, BEFORE starting MATLAB. QUITTING MATLAB: You want to STOP? When you're having such fun? Select "quit" from the "file" menu, or type ">>quit" The CAEN Technical Note on MATLAB can be accessed through the course web page, which is at http://www.eecs.umich.edu/~aey/eecs210.html