




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Department of Electrical and Computer Engineering
EC 300 Signals and Systems Spring 2009
Lab 2 Mark A. Yoder and Bruce A. Ferguson
The purpose of this lab is to learn to write program files in M ATLAB. While working through this lab you should learn many of the commands we will be using throughout the quarter. In addition, you will learn to create MATLAB programs, which we will use, reuse, and modify throughout the term. Use the help command often and early. Specifically, you should achieve the following:
M ATLAB can be used in two different ways – through the command line interface and using M ATLAB scripts. This second lab focuses on the use of MATLAB programs.
The second method of interfacing is by writing M ATLAB programs called scripts or “m-files”. These are programs written in M ATLAB’s command language, the same as is used in the command window. In the last lab, you typed a sequence of commands in a specific order to achieve some more complex result (such as plotting sinusoids). Each command had a specific purpose, and the order was important. However, one big problem with the method used in that lab was that you could not save the sequence of commands that you used. M ATLAB programs allow you to save a sequence of commands for reuse and modification.
The usual structure of a MATLAB program is similar to that of other programming languages: a main program is written ( filename .m), and this program can call functions saved in other files ( function_name .m). The main program is where you organize your work into a coherent flow. Functions are used to program repeated command sequences, so that your main program can be neater and smaller. Every command you type in the command line interface window is actually a M ATLAB function. Both the main program and functions are called m-files, but they have slightly different structures.
It is suggested that you use the M ATLAB script editor to create and modify your m-files. The editor can be accessed from inside MATLAB (“File → New → M-File”). Be sure to save the file before you run it. If you do not, MATLAB will use the previous version of the file which it has stored in memory. Simply type the filename (without the “.m”) from the command line to run the program. MATLAB references the script (program) only by its filename, so choose a name that has meaning. (Seventeen files named “testx” make for a confusing lab exercise!)
The files you create and save are stored in the directory shown in the “current directory” window just above the command window. Be sure to create a directory for your ECE 300
labwork, and navigate to that directory using the browse button (ellipses - “…”) to the right of the current directory window.
M ATLAB can access any file stored in its path variable. The path is a list of directories that M ATLAB has been told to look into to find commands and files. When you type a command burgerflip in the command window, MATLAB looks through its path to find a file named “burgerflip.m”, and then executes it.
Finally, when you save a function, MATLAB will know the function only by its filename. Even if your function definition in the first line of the m-file has something different listed as the command name, the filename in which you store the function is M ATLAB’s official reference. So when you call the function from either the command window or an m-file, you must use the name of the file the function is stored in. It is obviously good practice to make both the file name and the function definition line use the same name.
(a) Use the M ATLAB editor (“File → New → M-File”) to create a program file called sintest.m containing the following lines:
t = -2:0.05:3; x = sin(2pi0.789*t); % plot a sinusoid plot(t,x), grid on title(‘Test Plot of Sinusoid’) xlabel(‘Time (s)’);
Save the file. (MATLAB cannot access your programs or functions until they are saved from the editor.)
Creating a program file in this fashion allows for easy storage of a number of commands as a M ATLAB program. The program file is also called an M-File or a script. The program will be stored in the current directory indicated in the MATLAB window.
Run your function from MATLAB by typing its name (sintest) at the MATLAB prompt (note that sintest must reside in M ATLAB’s search path). Verify that the program produces the expected results.
(b) You have now created your first MATLAB program! Type type sintest at the command prompt to see the file you have created.
(c) Edit sintest.m to add a line containing the “hold on” function (type help hold for more information), followed by another plot command to add a plot of 0.5cos(2pi0.789t) to the plot created above. Add a final line to your program containing “hold off”. Note that you must save sintest.m in order for M ATLAB to access the updated program.
function even_odd(input) % Insert comments here describing how function is to be used result = mod(???); if (result==??)
Experiment with your new function by typing even_odd(3) etc. at the command prompt. This function has a single argument passed to it, in this case the number 3. Now experiment with different types of arguments to be sure your function is working correctly. While M ATLAB is evaluating the function, the argument value is assigned to the local variable input. The variable input does not have meaning outside of the function. You have now learned how to pass arguments to a function.
Instructor Verification (see last page)
(c) Create a simple function of one argument, returning two values. A third type of function returns a value to the command interface or program calling the function. The function definition line has a different form from the examples shown above. A function definition line is provided below to get your started.
Create a function which accepts a numeric input and evaluates how many times the number two goes into that input. The function should return the number of times 2 goes into the number and the remainder to the main program in the form of two variables: two_fac and remainder. For example, if the input is 33, the result should be 16 and 1. The returned value two_fac is the result of the integer division of input by 2.
You should be able to perform this operation using only two command lines in the function. The function floor will prove helpful.
function [two_fac,remainder] = two_div(input) % Insert comments here describing how function is to be used % Insert function commands
The function definition line contains a number of important elements. First, there is the familiar function definition, two_div, (which should be the same as the filename it is saved in). A single argument (input) is passed to the function from the main program. There are also two defined outputs (or returned values) of the function (two_fac and remainder). These are the variables in the square brackets.
To call this function, enter the the following line at the command prompt:
[two_fac,remainder] = two_div(input) test = two_fac*2 + remainder
The first line tells M ATLAB to call the function two_div with the argument input, and place the returned values into the variables two_fac and remainder. The second line verifies the returned values by calculating a variable test, which should be the same as the value store in
input. Test your function to be sure it is operating properly. Demonstrate this to your instructor.
Instructor Verification (see last page)
Often in lab you will have three types of data that you will want to plot on the same plot, Analytic (from a mathematical analysis), Simulation (from M ATLAB, SIMULINK or PSpice), and Lab Data (measured in lab). Suppose a process is to be modeled, and the model verified. Your analysis says the data should fit:
x(t) = sin(2π440t + π/2)
Next suppose you run a simulation of the process being modeled, and the result of this simulation is the data in the file “simdata.txt” available on the course webpage. Within the file, the first line is a comment line. In the subsequent lines, the first value is x , the second is the time (in seconds) that x was measured. Type help importdata to learn how to read in data from this type of file.
Finally you measure the following data in lab:
Lab Data Time (ms) +1.07 -4. -0.38 -3. -0.82 -2. +0.74 -2. +0.72 -1. -0.72 -1.
(a) Create a single plot showing all of your data using a MATLAB script. Your script should perform the steps listed below. Save your script as dataplot.m.
(b) Read in the data from the simulation results file, and store the data in the array test. Note that the array test contains two vectors. We will access the two vectors by breaking test into x and t arrays using: xxSim = test(:,1); % Be sure you understand this notation! ttSim = test(:,2);
(c) Use the vector ttSim to evaluate the equation at the correct times and store the results in an array called, e.g., xxAna (for Analytic).
(d) Store the lab data in two arrays, e.g. xxLab and ttLab, by typing it in by hand.
Lab 02 Introduction to M ATLAB Instructor Verification Sheet
Turn in this page as well as plot for parts 1(c) and 3(h)
Name ___________________________________________ Date of Lab: __________________
Part 1(c) Present you plot.
Verified: _________________________________________ Date/Time: __________________
Part 2(b) Verified: _________________________________________ Date/Time: __________________
Part 2(c) Verified: _________________________________________ Date/Time: __________________
Part 3(h) Present your plot. Verified: _________________________________________ Date/Time: __________________