Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Laboratory Report 2 - Matlab Scripts and Functions | ECE 300, Lab Reports of Electrical and Electronics Engineering

Material Type: Lab; Professor: Throne; Class: Continuous-Time Signals & Syst; Subject: Electrical & Computer Eng.; University: Rose-Hulman Institute of Technology; Term: Spring 2009;

Typology: Lab Reports

Pre 2010

Uploaded on 08/13/2009

koofers-user-qhv
koofers-user-qhv 🇺🇸

10 documents

1 / 8

Toggle sidebar

Related documents


Partial preview of the text

Download Laboratory Report 2 - Matlab Scripts and Functions | ECE 300 and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity! ROSE-HULMAN INSTITUTE OF TECHNOLOGY Department of Electrical and Computer Engineering EC 300 Signals and Systems Spring 2009 MATLAB Scripts and Functions Lab 2 Mark A. Yoder and Bruce A. Ferguson Prelab: none Objectives The purpose of this lab is to learn to write program files in MATLAB. 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: • Learn the purpose of MATLAB scripts and functions (aka “m-files”) • Learn to create a script file for performing repeated operations • Learn to properly graph data from various sources Notes on Using MATLAB MATLAB can be used in two different ways – through the command line interface and using MATLAB scripts. This second lab focuses on the use of MATLAB programs. The second method of interfacing is by writing MATLAB programs called scripts or “m-files”. These are programs written in MATLAB’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. MATLAB 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 MATLAB function. Both the main program and functions are called m-files, but they have slightly different structures. It is suggested that you use the MATLAB 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 Page 1 of 8 EC 300 Signals and Systems Spring 2009 labwork, and navigate to that directory using the browse button (ellipses - “…”) to the right of the current directory window. MATLAB can access any file stored in its path variable. The path is a list of directories that MATLAB 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 MATLAB’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. 1. Script file basics (a) Use the MATLAB editor (“File → New → M-File”) to create a program file called sintest.m containing the following lines: t = -2:0.05:3; x = sin(2*pi*0.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 MATLAB 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 MATLAB’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.5*cos(2*pi*0.789*t) 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 MATLAB to access the updated program. Page 2 of 8 EC 300 Signals and Systems Spring 2009 input. Test your function to be sure it is operating properly. Demonstrate this to your instructor. Instructor Verification (see last page) 3. Presenting Data Graphically 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 MATLAB, 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.17 -0.38 -3.55 -0.82 -2.92 +0.74 -2.30 +0.72 -1.67 -0.72 -1.05 (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. Page 5 of 8 EC 300 Signals and Systems Spring 2009 (e) Finally, plot all three on the same plot using: plot(ttSim, xxAna, ttSim, xxSim, '--', ttLab, xxLab, 'x') (f) Add a legend using: legend('Analytic', 'Simulation', 'Lab Data'); (g)Label your plot using xlabel, ylabel, and title. (You can get π by entering “\pi”.) Put your name in the title. (h) Modify your time vectors so that your plot looks exactly like the plot below: Note the time axis is in ms, not in seconds! Print out this plot to turn in you’re your laboratory worksheet. Instructor Verification (see last page) Note: We have shown you the above format because it is an industry standard to plot Analytic data as a solid line, Simulation data as a dashed line, and to plot individual Lab Data as points, not as a continuous line. Page 6 of 8 EC 300 Signals and Systems Spring 2009 Page 7 of 8
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved