ECE330 - Matlab Basics
ECE330 - Matlab Basics
September 7, 2011
Todays topics 1. Matlab Access 2. Some basic operations 3. Plotting Continuous Time and Discrete Time Sinusoids
Accessing Matlab
Matlab is installed on the machines in the CAE labs. You can also download it through CAE as tethered software.
1.1
CAE Lab
If you already have a CAE account just go to your favorite CAE lab. Matlab should be available on all lab computers. For example, in windows, click Start -> Course Software -> M-S -> Matlab R2011a. If you do not have a CAE account go to http://www.cae.wisc.edu/ newuserhelp to nd instructions on how to create one.
1.2
Tethered Software
Tethered software allows you to locally install and run some CAE owned and controlled software while your computer is connected to the network or Internet. For more info on tethered software visit http://software.cae. wisc.edu/how-use-tethered-software. To install matlab on windows as tethered software go to http://software. cae.wisc.edu/node/587. To install matlab on a Mac as tethered software go to http://software. cae.wisc.edu/node/2.
In general, there are two ways to use Matlab - command line entry, and creating an M-File.
2.1
The rst way is to simply use the command line. In the Command Window, type y = 5 and press enter. Then type x = 12; and again press enter. Notice that the semicolon suppresses the output. Next, type z = x*y and press enter.
2.2
Creating an m-le
We can also create a le with a list of instructions that will be executed sequentially by Matlab. In the Current Folder window, navigate to a location where you want to save your work. Then, right click in the Current
Folder window, and select New file -> Blank M-File. Double click on the le to open it in the Matlab Editor. You can type a sequence of commands to be executed by Matlab. First, dene a vector x and a matrix B by typing x = [2 6 8] B = [1 2 3; 4 5 6; 7 8 9] You can run the m-le by pressing F5, by clicking the green play button on the toolbar, or by typing the name of the le followed by pressing enter in the Command Window. Look at the output in the Command Window to verify your work. Now, add the following lines to your m-le y = x*B z = B*x The single quote symbol () takes the transpose (complex conjugate transpose if the vector is complex) of a vector or a matrix. Mathematically, we would denote these lines as y = xB z = BxT (1) (2)
2.3
Create a new m-le. Type the following - notice that % indicates a comment. clear clc close all %this clears all variables %this clears the Command Window %this closes all graphs %This creates a vector, starting at %-3.14..., with increments of .01 up to +3.14.. %dimension of t (size of t) %evaluate the sin of 2*pi*t %This displays the 12th element of vector c %This displays the first 10 elements of c 3
%Plot t vs. c
Try to understand all the code in the example, and run the example. How big is the vector t?
2.4
Getting Help
You can get help in Matlab by typing doc at the command line, or by clicking Help -> Product Help. Search for help on sin.
2.5
We can dene two dierent type of multiplications (also, other operations division, ect) in Matlab. In a new M-File, type x = [2 6 8] y = [1 2 3] z = x.*y Run the M-le and look at the output. The period symbol, (.) indicates element wise operations. The two vectors must be the same size. If you try this code without the period symbol, you get a matrix dimension error.
3
3.1
In order to plot a continuous time sinusoid in Matlab, we have to evaluate the sinusoid at suciently many points - enough points that the sinusoid looks continuous. Matlab, in general, will draw a straight line between adjacent points in a vector when you type the plot command. To illustrate this, try the following simple example: %% Plotting Continuous time sinusoids clear 4
close all clc t1 = [0:0.3:2]; f = 1; x1 = sin(2*pi*f*t1); plot(t1,x1) %Define a time vector from 0 to 2 by 0.3 %Calculate the sinusoid for all time t1 %Plot the first sinusoid
t2 = [0:.001:2]; %Define a time vector from 0 to 2 by 0.001 x2 = sin(2*pi*f*t2); %Calculate the sinusoid for all time t2 hold on %Hold the current plot plot(t2,x2,r) %Plot the second sinusoid in red title(Continuous Time Sinusoid) The code should plot two sinusoids on the same plot. If we specify enough points, the plot looks continuous. If not, the plot doesnt look like a continuous time sinusoid.
3.2
To plot discrete time sinusoids use the command stem or comb instead of plot. As an example, plot x[n] = sin( 16 n + ) for n = 0, 1, 2...99 as follows: 4 %% Plotting Discrete time sinusoids n = [0:1:99]; %Define n from 0 to 99 by 1, x = sin(pi/16*n +pi/4); %Calculate sinusoid for all n figure %Open a new figure stem(n,x) %Make a stem plot title(Discrete Time Sinusoid)