Unformatted text preview:

A Brief Introduction to MATLABJanuary 6, 2004, 14:49Professor: Jeffrey A. Fessler (originally by Robert Nickel)MATLAB is a technical computing environment for high-performance numeric computation and visualization.MATLAB integrates numerical analysis, matrix computation, signal processing (via the Signal Processing Toolbox),and graphics into an easy-to-use environment where problems and solutions are expressed just as they are writtenmathematically, without much traditional programming. The name MATLAB stands for matrix laboratory.We will use MATL AB to illustrate concepts with numerical examples. Some or all homework assignments willinclude some problems that require MATLAB solutions. Wise students will quickly realize that MATLAB can often beused to check solutions to other problems as well. This is perfectly legal, in fact, encouraged. But you still must turnin your analytical solutions for the pencil-and-paper problems.MATLAB is available on CAEN on UNIX, PC, and Mac platforms. You start MATLAB by double-clicking on theMATLAB-Icon (MAC, PC) or by typing matlab on the UNIX command line. You should then see the MATLABprompt, denoted “>>”.Some assignments may require you to use MATLAB version 5.0 (or later). When you first start MAT LA B, theversion number is printed. Make sure it is 5.0 or higher. In CAEN you may need to use the swselect command tochoose the latest version of MATLAB if it is not the default already.This document is by no means a complete reference. There are tutorial and reference manuals for MATLAB at theMedia Union. MAT LA B has lots of on-line help. CAEN offers MAT LA B tutorials each semester. The CAEN Hotlinecan help with questions about printing etc.• Getting HelpFor a list of all the available help topics, type>> helpFor help on a particular topic or command, such as plot type:>> help plot• Loading and Saving DataMATLAB’s load and save commands allow reading .mat files from disk into MATLAB, or saving a MATLA Bvariable to disk. The command whos lists the current variables.• Definition of Vectors/SignalsMATLAB provides several commands for generating vectors. We use vectors to represent arrays of samples ofsignals. The following are all equivalent commands for generating a vector.>> x = [4 6 8 10 12 14]>> x = 4 + 2*[0:5]>> x = 4:2:14>> x = linspace(4,14,6)We only use the first one for very short vectors. For more information on the “:” operator type:>> help ops>> help colon• Making long vectorsYou can form long vectors (e.g. signals) by concatenating two shorter vectors.>> x = 4:2:14>> y = [x, x];The comma concatenator works for row vectors. For column vectors use the semicolon concatenator:>> x = [4:2:14]’>> y = [x; x];• How to Suppress Display of ResultsAppend a semicolon “;” to the end of a line to suppress the display of the results. For example:>> x=1:50;1• Plotting a SignalThe easiest way to graphically display a continuous-time signal is with the plot command:>> t = linspace(0, 10, 100);>> x = sin(t);>> plot(t, x)For a discrete-time signal, we usually use the stem command:>> n = 0:20;>> x = cos(pi*n/3);>> stem(n, x)Be sure to label the axes of your graphs using the xlabel, ylabel, and title commands. You can put somemathematical symbols in these labels, for exampletitle(’Frequency response from -\pi to \pi’)produces a plot title that reads “Frequency response from −π to π.” You can adjust the graph axes using the axescommand.• Generating MatricesMATLAB also provides several commands to generate matrices. Try out the following.>> A = [1 2 3; 4 5 6; 7 8 9]>> B = eye(3)>> C = ones(2,3)>> D = zeros(3,2)>> E = rand(1,5)>> F = randn(5,1)The last two commands generate random vectors, i.e. “random signals”.• Matrix ManipulationThe following commands are useful for matrix/vector manipulations. You can transpose a matrix, flip the matrixfrom left to right or up and down, concatenate two matrices and so forth. Use the matrices defined above and type:>> P = A.’>> P = fliplr(A)>> P = flipud(A)>> Q = [A D]>> R = [A; B]• Complex Numbers and ConstantsBoth i and j are defined by default to be√−1, unless you assign them to some other value. Thus MATLAB canhandle complex numbers. It also has many built-in variables:>> x=2+3*j>> y=pi• FunctionsFunctions are evaluated element wise, as in the sin(t) example above. For example, if we type>> t = linspace(0,4pi,9); then t is a vector containing 9 time samples. If we then type>> x = sin(t), then MATLAB creates the vector x with 9 values corresponding to the sin of each of theelements of the vector t, i.e. x = 0 1 0 -1 -0 1 0 -1 0 Think carefully about why I used 9 rather than8 samples in this example. This is known as the “picket fence” problem (ask me why) and is a common error inMATLAB. To get a list of available functions, type:>> help elfun2• OperatorsSince MAT LA B generally deals with matrices, you must be careful when using operators like “*” or “/”. If youwant these operators to operate in an element-by-element fashion you have to denote this by a leading period,e.g.“.*” and “./” ! Try the following examples:>> x=1:5>> y=x+x>> y=x.*x>> y=x-x>> y=x./(x+x)Note: “*” and “/” without “.” are matrix multiplication and “matrix division” (special functions of MATLAB). Forexample:>> A = eye(3) * rand(3,2)A special case applies for scalar multiplication:>> y=2*xSo for scalar multiplication or division, you do not need the extra leading period.• Writing Programs in MATLABYou can also write your own programs in MATLAB using any regular ASCII text editor. Simply open a file withthe extension .m (which is called an m-file) and edit line-by-line the sequence of commands you want to includein your program. Save the file and execute the program by typing the name of the file (without the .m) on yourMATLAB command line.EXAMPLE: Invoke a text editor (e.g. emacs on UNIX or notepad on PCs) and edit the following lines:% This is a program that generates a noisy signalx=linspace(0,10*pi,200);% compute the signaly=sin(x);% compute the noisez=0.3*rand(1,200);y=y+z;% plot the signalplot(y)Save this program in a file named noisy.m in MATLA B’s current working directory. (Use MATLAB’s cd com-mand to change MATLAB’s current working directory. Print MATLAB’s current working directory with MATLAB’spwd command.) To execute your program, type its name at the prompt:>> noisyYou can also write your own functions in MATLAB. Check out:>> help


View Full Document

U-M EECS 451 - A Brief Introduction to MATLAB

Download A Brief Introduction to MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Brief Introduction to MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Brief Introduction to MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?