Lab 5.docx - Department of Electrical Computer Engineering...

This preview shows page 1 out of 3124 pages.

Unformatted text preview: Department of Electrical & Computer Engineering Fall 2017 EEL3002L – ECE Engineering Tools Lab LAB REPORT Date: October 18, 2017 Lab Number: 5 Lab Title: Programming in MATLAB -- Part I & Data Files Section: 002 Student Name: Jalen Munoz (Partner’s Name: Barrett McKinney) Evaluation: Pre-lab Exercises: _____________ (20 points maximum) Lab Report: __________________ (40 points maximum) Total Score: __________________ (60 points maximum) Lab Teaching Assistant: Kenley Tan_________ Supervising Faculty & Instructor: Dr. B. W. Kwan Huawei Yang ____X______ EEL3002L Lab Report Sample ECE Tools Lab Pre-lab Exercise Report Insert the pre-lab exercises and answers in this section. Exercise 5.1 Consider using MATLAB to compute the roots of a quadratic equation of the following form: ax +bx+c=0 ···· (E5.1) 2 a) Write a MATLAB script M-file to compute the roots of the quadratic equation defined in (E5.1). In particular, the coefficients a, b, c are specified via the use of the MATLAB function input. a = input('What is a? '); b = input('What is b? '); c = input('What is c? '); x0 = (­b+sqrt(b^2­4*a*c))/(2*a); x1 = (­b­sqrt(b^2­4*a*c))/(2*a); display('The root is:') disp(x0) disp(x1) >> new_program What is a? 5 What is b? 6 What is c? 1 The root is: ­0.2000 ­1 b) Exploit the script M-file created in (a) to compute the roots of 4 quadratic equations. The coefficients of these equations are defined by the rows of a (4 × 3) array stored in the ASCII data file PE5 1.dat. Store these roots in a (4 × 2) matrix called QE roots. QE_roots = importdata('PE5_1.dat') a = QE_roots(:,1); b = QE_roots(:,2); c = QE_roots(:,3); x0 = (­b+sqrt(b.^2­c.*a.*4))/(a.*2); x1 = (­b­sqrt(b.^2­c.*a.*4))/(a.*2); display('The root is:') QE_roots = zeros(4,2) QE_roots(:,1) = x0(:,2) QE_roots(:,2) = x1(:,2) >> new_program QE_roots = Fall 2017 Instructor: Dr. B.W. Kwan 2 EEL3002L Lab Report Sample ECE Tools Lab 15 6 22 20 ­20 23 ­18 ­11 ­17 20 2 23 The root is: QE_roots = 0 0 0 0 0 0 0 0 QE_roots = ­0.1500 + 0.8958i 0.0000 + 0.0000i 0.5000 + 0.9487i 0.0000 + 0.0000i 0.2750 + 0.8303i 0.0000 + 0.0000i ­0.0500 + 1.0712i 0.0000 + 0.0000i QE_roots = ­0.1500 + 0.8958i ­0.1500 ­ 0.8958i 0.5000 + 0.9487i 0.5000 ­ 0.9487i 0.2750 + 0.8303i 0.2750 ­ 0.8303i ­0.0500 + 1.0712i ­0.0500 ­ 1.0712i c) Display the contents of QE roots obtained in (b). QE_roots = ­0.1500 + 0.8958i ­0.1500 ­ 0.8958i 0.5000 + 0.9487i 0.5000 ­ 0.9487i 0.2750 + 0.8303i 0.2750 ­ 0.8303i ­0.0500 + 1.0712i ­0.0500 ­ 1.0712i Exercise 5.2 Consider two electric point charges Q1 (C) and Q2 (C) placed in air (free space medium). Charge Q1 is located at position P1 defined by rectangular coordinates (x1,y1,z1) and charge Q2 is located at position P2 defined by rectangular coordinates (x2, y2, z2). The vector force F(x1, y1, z1) (N) exerted on Q1 due to Q2 is governed by Coulomb’s law. Specifically, its three components are computed as follows: Q1Q2 x1 −x2 Fx(x1, y1, z1) = Fall 2017 4πε0 (R12)3 (N) Instructor: Dr. B.W. Kwan 3 EEL3002L Lab Report Sample Q1Q2 y1 −y2 Fy(x1, y1, z1) = Q1Q2 z1 −z2 Fz(x1,y1,z1) = 4πε0 (R12)3 4πε0 (R12)3 ECE Tools Lab (N) (N) where R12 = (x1 − x2)2 + (y1 − y2)2 + (z1 − z2)2, ε0 = 8.854187817 × 10 −12 (F/m) (a) Write a MATLAB function script for computing the components of the electric force F acting on Q1 due to Q2. Specifically, the input data and output data of this MATLAB function are as follows: • Input: Q1 (C) at P1(x1, y1, z1), Q2 (C) at P2(x2, y2, z2)• Output: Fx, Fy, Fz at P1(x1, y1, z1) k = 8.99 *10^9; Fx = (Q1*Q2*k)/((X1­X2)^2) Fy = (Q1*Q2*k)/((Y1­Y2)^2) Fz = (Q1*Q2*k)/((Z1­Z2)^2) (b) Exploit the function M-file written in (a) to compute Fx,Fy,Fz exerted on Q1 = 10 (nC) at P1(0,3,1) (m) for the following cases: • Case 1: Q2 = 1 (nC) located at P2(3, 2, −1) (m)• Case 2: Q2 = −2 (nC) located at P2(−1, −1, 4) (m) • Case 3: Q2 = 5 (nC) located at P2(2,0,4) (m) Case 1: Q1 = 10*10^­9; Q2 = 1*10^­9; X1 = 0; Y1 = 3; Z1 = 1; X2 = 3; Y2 = 2; Z2 = ­1; k = 8.99 *10^9; Fx = (Q1*Q2*k)/((X1­X2)^2) Fy = (Q1*Q2*k)/((Y1­Y2)^2) Fz = (Q1*Q2*k)/((Z1­Z2)^2) Fx = 9.9889e­09 Fy = 8.9900e­08 Fz = Fall 2017 Instructor: Dr. B.W. Kwan 4 EEL3002L Lab Report Sample 2.2475e­08 Case 2: Q1 = 10*10^­9; Q2 = ­2*10^­9; X1 = 0; Y1 = 3; Z1 = 1; X2 = ­1; Y2 = ­1; Z2 = 4; k = 8.99 *10^9; Fx = (Q1*Q2*k)/((X1­X2)^2) Fy = (Q1*Q2*k)/((Y1­Y2)^2) Fz = (Q1*Q2*k)/((Z1­Z2)^2) Fx = ­1.7980e­07 Fy = ­1.1238e­08 Fz = ­1.9978e­08 Case 3: Q1 = 10*10^­9; Q2 = 5*10^­9; X1 = 0; Y1 = 3; Z1 = 1; X2 = 2; Y2 = 0; Z2 = 4; k = 8.99 *10^9; Fx = (Q1*Q2*k)/((X1­X2)^2) Fy = (Q1*Q2*k)/((Y1­Y2)^2) Fz = (Q1*Q2*k)/((Z1­Z2)^2) Fx = 1.1238e­07 Fall 2017 Instructor: Dr. B.W. Kwan 5 ECE Tools Lab EEL3002L Lab Report Sample ECE Tools Lab Fy = 4.9944e­08 Fz = 4.9944e­08 (c) Apply the principle of superposition to compute the total electric force Ftotal exerted on Q1 due to all three cases in (b) combined. Fx = ­5.74*10^­8 Fy = 1.28606*10^­7 Fz = 5.2441*10^­8 Exercise 5.3 There are 80 students in the EEL3002L class in a spring semester. The data file midterm sp20xx.dat stores the results of the midterm exam in binary format. (a) Read the binary data from midterm sp20xx.dat and display the midterm results (ASCII format) in an (8 × 10) array. id = fopen('midterm_sp20xx.dat') students = fread(id, [8, 10], 'single') students = 71 105 64 70 53 64 77 56 82 63 86 96 82 77 56 62 77 69 66 75 40 50 81 73 32 69 55 62 48 56 75 99 81 62 81 68 66 77 57 49 69 73 73 68 69 55 51 53 53 49 50 64 51 56 57 65 53 65 91 70 60 73 73 75 80 63 65 71 58 63 69 63 83 52 46 72 82 77 73 63 (b) Search for the highest score and the lowest score, then compute the average of the midterm exam. id = fopen('midterm_sp20xx.dat') students = fread(id, [8, 10], 'single') max(max(students)) ans = Fall 2017 Instructor: Dr. B.W. Kwan 6 EEL3002L Lab Report Sample ECE Tools Lab 105 id = fopen('midterm_sp20xx.dat') students = fread(id, [8, 10], 'single') min(min(students)) ans = 32 Table of Content Abstract (approximately 300 words).............................................................................................................................................8 1. Introduction (section title)....................................................................................................................................................8 2. Experimental Procedure.......................................................................................................................................................8 3. Results................................................................................................................................................................................13 4. Discussion...........................................................................................................................................................................17 5. Conclusion and Recommendation......................................................................................................................................18 References...................................................................................................................................................................................18 Appendices..................................................................................................................................................................................19 Fall 2017 Instructor: Dr. B.W. Kwan 7 EEL3002L Lab Report Sample ECE Tools Lab Abstract This experiment was designed to teach us the tools to become efficient programmers and to learn some data structures. Data structures are very useful and have the ability to create lots of different things. I find them very interesting and I believe that everyone should use them at one point in their life. This experiment taught my lab partner and I to create line, bar, area, and surface graphs. This proves to be a useful skill as graphs are the best and easiest way to represent data, and with no programming needed it certainly is easy. We also learned to use other functions to change the way we use programs and data structures. 1. Introduction Data structures and programming are very important to a computer engineering student. We utilize things like this in all the time in our classes. The main goal of this experiment is to get students to utilize Matlab’s programming strengths and to learn and utilize data structures. The purpose of this lab is to get students to realize the power of programming and data structures. The scope of the experiment is to see how far we can get with programming in Matlab 2. Experimental Procedure Experiment 5.1 (a) Sample the values of vC (t) over the time interval 2 t 5 (ms) according to the scheme described below: For 2 t 0 (ms), sample vC (t) uniformly at intervals of 0.02 msec. For 0 t 5 (ms), sample vC (t) uniformly at intervals of 0.01 msec. Plot the capacitor voltage vC (t) over the sampling interval. Your Procedure and Your codes: t = [-2:.02:0] vc = -9 + t*0 > plot(t,vc) >> hold on t = [0:.01:5] vc2 = -7.2-1.8*exp(-1000.*t) Fall 2017 Instructor: Dr. B.W. Kwan 8 EEL3002L Lab Report Sample ECE Tools Lab plot(t,vc) (b) Create a 2-row matrix M_5_1 to store the sampled data specified in part (a). The first row of M_5_1 contains all the sampling instants over the time interval 2(ms) t 5(ms) , whereas the second row contains the corresponding values of the capacitor voltage vC (t). Your Procedure and Your codes: t1 = [-2:.02:0] t2=[0:.01:5] M_5_1=[vc,vc2;t1,t2] (d) Apply the save function to write the matrix M_5_1 in an ASCII file named export_n_5_1.dat, where n is the number assigned to your lab station. Your Procudure and Your codes: dlmwrite('export_n_5_1.dat', M_5_1, ' ') Experiment 5.2 Download the ASCII file data_5_2.dat from the class Blackboard and place it in your personal MATLAB folder. (b) Apply the importdata function to read the data of data_5_2.dat and store them in the matrix M_5_2. Read the first row of M_5_2 into the array time, and its second row into the array waveform. Code: M_5_2 = importdata('data_5_2.dat') time = M_5_2(1,:) waveform = M_5_2(2,:) (c) Plot waveform versus time. Use ‘Time’ as the x-axis label and ‘Waveform’ as the title of the plot. plot(waveform, time) Code: xlabel('Time') ylabel('Waveform') Fall 2017 Instructor: Dr. B.W. Kwan 9 EEL3002L Lab Report Sample ECE Tools Lab Experiment 5.3 Code: T1 = [-1:.1:0] vc1 = 0 + 0*t t2 = [0:.01:1] vc2 = 8*(1-exp((-1.25.*t2))) t3=[1:0.01:6] vc3 = 5.708.*exp(-1.25.*(t3 - 1)) Create a 2-row matrix M_5_3 to store the sampled data specified in part (a). The first row of M_5_3 contains all the sampling instants over the time interval 1 t 6 , whereas the second row contains the corresponding values of the capacitor voltage vC (t). Code: M_5_3 = [t1,t2,t3;vc1,vc2,vc3] (d) Apply the dlmwrite function using ‘;’ as delimiter to write the matrix M_5_3 in an ASCII file named export_n_5_3.dat, where n is the number assigned to your lab station. Code: dlmwrite('export_n_5_3.dat', M_5_3, ' ') Experiment 5.4 (b) Apply the dlmread function to read the data of data_5_4.dat, which employs ‘,’ as delimiter. Store the data in the matrix M_5_4. Read the first row of M_5_4 into the array time, and its second row into the array waveform. Code: M_5_4 = dlmread('data_5_4.dat', ',') Fall 2017 Instructor: Dr. B.W. Kwan 10 EEL3002L Lab Report Sample time = M_5_4(1,:) waveform = M_5_4(2,:) (c) Plot waveform versus time. Use ‘Time’ as the x-axis label and ‘Waveform’ as the title of the plot. Code: plot(time,waveform) xlabel('time') ylabel('waveform') Experiment 5.5 t1 = [-1:.1:0] i1 = 0.5 + 0*t1 t2 = [0:.01:1.5] i2 = -4.1667.*exp(-t2)+0.6667.*exp(-5.*t2)+4 (b) Create a 2-row matrix M_5_5 to store the sampled data specified in part Code: M_5_5 = [t1,t2;i1,i2] (d) Apply the fprintf function using the floating point format ‘%8.4f’ to write the matrix M_5_5 in an ASCII file named export_n_5_5.dat, where n is the number assigned to your lab station. The data in this ASCII file should have the format such that the sampling time instants and the sampled values of i (t) L are written in separate columns. Code: fid = fopen('export_n_5_5.dat','w'); fprintf(fid,'%8.4f',M_5_5); fclose(fid); Fall 2017 Instructor: Dr. B.W. Kwan 11 ECE Tools Lab EEL3002L Lab Report Sample Experiment 5.6 (b) Apply the fscanf function to read the data of data_5_6.dat, which employs the floating point format ‘%f’. Store the data in the matrix M_5_6, which should be specified to contain only two rows. Read the first row of M_5_6 into the array time, and its second row into the array waveform. Code: ID_r = fopen('data_5_6.dat', 'r') M_5_6 = fscanf(ID_r, '%f', [2,606]) (c) Plot waveform versus time. Use ‘Time’ as the x-axis label and ‘Waveform’ as the title of the plot. Code: time = M_5_6(1,:) waveform = M_5_6(2,:) plot(time, waveform) hold on xlabel('time') ylabel('waveform') Experiment 5.7 b) Read the binary data in data_5_7.dat into a single-column matrix M1_5_7. Find the total number N of data points in the matrix. Code: ID_r = fopen('data_5_7.dat', 'r') M1_5_7 = fread(ID_r, 'single') size(M1_5_7) (c) Reshape M1_5_7 into a new matrix M2_5_7 with size 5 (N 5). N = max(size(M1_5_7)) M2_5_7 = reshape(M1_5_7, [5*(N/5), 1]) (d) Find the smallest and largest values stored in M1_5_7. Then create a 10bin histogram for the data values in M1_5_7. min(M1_5_7) max(M1_5_7) histogram(M1_5_7, 10) Fall 2017 Instructor: Dr. B.W. Kwan 12 ECE Tools Lab EEL3002L Lab Report Sample Experiment 5.8 Import the data in various files provided by the lab TA into MATLAB. Display or process the data in accordance with the TA’s instructions. Code: M_5_2 = dlmread('data_5_2.dat', '') time = M_5_2(1,:) waveform = M_5_2(2,:) plot(time,waveform) xlabel('time') ylabel('waveform') Results Experiment 5.1 Your Results: See “Dat” attachment Experiment 5.2 Your Results: M_5_2 Fall 2017 Instructor: Dr. B.W. Kwan 13 ECE Tools Lab EEL3002L Lab Report Sample Experiment 5.3 Your Results: See “Dat” attachment Experiment 5.4 Your results: M_5_4 Fall 2017 Instructor: Dr. B.W. Kwan 14 ECE Tools Lab EEL3002L Lab Report Sample Experiment 5.5 Your Results: See “Dat” attachment Experiment 5.6 Your Results: M_5_6 Fall 2017 Instructor: Dr. B.W. Kwan 15 ECE Tools Lab EEL3002L Lab Report Sample Experiment 5.7 Your Results: M_5_7 Fall 2017 Instructor: Dr. B.W. Kwan 16 ECE Tools Lab EEL3002L Lab Report Sample ECE Tools Lab Experiment 5.8 Your Results: M_5_8 (d) Discussion This lab as with the last lab was probably the most difficult lab we have done so far this semester. This lab tied in all the previous labs into one as it tested what we had learned so far. All results were as expected and the difficult part was figuring out how to use the functions exactly how they are supposed to be used and getting the correct graphs. My partner and I worked well however, and figured everything out even if it took a couple of tries. Fall 2017 Instructor: Dr. B.W. Kwan 17 EEL3002L Lab Report Sample ECE Tools Lab (e)Conclusion and Recommendation In this lab our goal was to become proficient at utilizing the functions that were given to us and creating graphs to represent our work. We did fine in the end, but I would recommend more concrete instructions as some of the instructions were hard to follow. References [1] L. David Irwin and R. Mark Nelms, Basic Engineering Circuit Analysis, 10th Edition, John Wiley 2011 (ISBN: 978-0-470-63322-9). [2] MATLAB documents and resources available at the MathWorks, Inc. website: Fall 2017 Instructor: Dr. B.W. Kwan 18 EEL3002L Lab Report Sample Appendices diary oft = [-2:.02:0] t= Columns 1 through 8 -2.0000 -1.9800 -1.9600 -1.9400 -1.9200 -1.9000 -1.8800 -1.8600 Columns 9 through 16 -1.8400 -1.8200 -1.8000 -1.7800 -1.7600 -1.7400 -1.7200 -1.7000 Columns 17 through 24 -1.6800 -1.6600 -1.6400 -1.6200 -1.6000 -1.5800 -1.5600 -1.5400 Columns 25 through 32 Fall 2017 Instructor: Dr. B.W. Kwan 19 ECE Tools Lab EEL3002L Lab Report Sample -1.5200 -1.5000 -1.4800 -1.4600 -1.4400 -1.4200 -1.4000 -1.3800 Columns 33 through 40 -1.3600 -1.3400 -1.3200 -1.3000 -1.2800 -1.2600 -1.2400 -1.2200 Columns 41 through 48 -1.2000 -1.1800 -1.1600 -1.1400 -1.1200 -1.1000 -1.0800 -1.0600 Columns 49 through 56 -1.0400 -1.0200 -1.0000 -0.9800 -0.9600 -0.9400 -0.9200 -0.9000 Columns 57 through 64 -0.8800 -0.8600 -0.8400 -0.8200 -0.8000 -0.7800 -0.7600 -0.7400 Fall 2017 Instructor: Dr. B.W. Kwan 20 ECE Tools Lab EEL3002L Lab Report Sample Columns 65 through 72 -0.7200 -0.7000 -0.6800 -0.6600 -0.6400 -0.6200 -0.6000 -0.5800 Columns 73 through 80 -0.5600 -0.5400 -0.5200 -0.5000 -0.4800 -0.4600 -0.4400 -0.4200 Columns 81 through 88 -0.4000 -0.3800 -0.3600 -0.3400 -0.3200 -0.3000 -0.2800 -0.2600 Columns 89 through 96 -0.2400 -0.2200 -0.2000 -0.1800 -0.1600 -0.1400 -0.1200 -0.1000 Columns 97 through 101 Fall 2017 Instructor: Dr. B.W. Kwan 21 ECE Tools Lab EEL3002L -0.0800 -0.0600 -0.0400 -0.0200 Lab Report Sample ECE Tools Lab 0 vc = -9 vc = -9 plot(t,vc) fplot(t,vc) {Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('fcnchk', 'C:\Program Files\MATLAB\R2014b\toolbox\matlab\funfun\fcnchk.m', 106)" style="font-weight:bold">fcnchk</a> (<a href="matlab: opentoline('C:\Program Files\MATLAB\R2014b\toolbox\matlab\funfun\fcnchk.m',106,0)">line 106</a>) FUN must be a function, a valid string expression, or an inline function object. Error in <a href="matlab:matlab.internal.language.introspective.errorDocCallback('fplot', 'C:\Program Files\MATLAB\R2014b\toolbox\matlab\specgraph\fplot.m', 60)" style="font-weight:bold">fplot</a> (<a href="matlab: opentoline('C:\Program Files\MATLAB\R2014b\toolbox\matlab\specgraph\fplot.m',60,0)">line 60</a>) Fall 2017 Instructor: Dr. B.W. Kwan 22 EEL3002L Lab Report Sample ECE Tools Lab fun = fcnchk(fun); } plot(5,3) plot(vc,t) histogram(t,vc) {Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('histogram')" style="font-weight:bold">histogram</a> Expected input number 2, m, to be positive. Error in <a href="matlab:matlab.internal.language.introspective.errorDocCallback('histogram>parseinput', 'C:\Program Files\MATLAB\R2014b\toolbox\matlab\specgraph\histogram.m', 153)" style="fontweight:bold">histogram>parseinput</a> (<a href="matlab: opentoline('C:\Program Files\MATLAB\R2014b\toolbox\matlab\specgraph\histogram.m',153,0)">line 153</a>) validateattributes(in,{'numeric'},{'integer', 'positive'}, ... Error in <a href="matlab:matlab.internal.language.introspective.errorDocCallback('histogram', 'C:\Program Files\MATLAB\R2014b\toolbox\matlab\specgraph\histogram.m', 116)" style="font-weight:bold">histogram</a> (<a href="matlab: opentoline('C:\Program Files\MATLAB\R2014b\toolbox\matlab\specgraph\histogram.m',116,0)">line 116</a>) [opts,args] = parseinput(args); } Fall 2017 Instructor: Dr. B.W. Kwan 23 EEL3002L Lab Report Sample ECE Tools Lab explot('9',[-2,0]) {Undefined function 'explot' for input arguments of type 'char'. } ezplot('9',[-2,0]) plot(t,vc) java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source) at java.awt.Component.getLocationOnScreen(Unknown Source) at javax.swing.JPopupMenu.show(Unknown Source) at com.mathworks.mwswing.MJPopupMenu.show(MJPopupMenu.java:200) at com.mathworks.widgets.DropdownButton.doPostMenu(DropdownButton.java:243) at com.mathworks.widgets.DropdownButton.showMenu(DropdownButton.java:218) at com.mathworks.widgets.PickerButton.showMenu(PickerButton.java:222) at com.mathworks.hg.peer.AbstractSplitButtonPeer.doShowMenu(AbstractSplitButtonPeer.java:296) at com.mathworks.hg.peer.AbstractSplitButtonPeer$5.run(AbstractSplitButtonPeer.java:290) at com.mathworks.hg.util.HGPeerQueue$HGPeerRunnablesRunner.runit(HGPeerQueue.java:294) Fall 2017 Instructor: Dr. B.W. Kwan 24 EEL3002L Lab Report Sample ECE Tools Lab at com.mathworks.hg.util.HGPeerQueue$HGPeerRunnablesRunner.runNotThese(HGPeerQue...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture