Lab 006-007 - [NEW] Matlab Data Analysis And Toolbox

Transcription

UNIVERSITI TEKNIKAL MALAYSIA MELAKAFAKULTI KEJURUTERAAN ELEKTRONIK DANKEJURUTERAAN KOMPUTERFAKULTI KEJURUTERAAN ELEKTRONIK DANKEJURUTERAAN KOMPUTERBENC 2113ECADDDENC2532ECADDLAB SESSION 6/7LAB SESSION 5MATLAB – DATA ANALYSIS AND TOOLBOXPSPICE FOR DC ANALYSISPrepared by:Norhashimah Mohd SaadComputer Engineering Department, FKEKKPrepared by:Hamzah Asyrani SulaimanComputer Engineering Department, FKEKK(Sept. 2013)1

LAB 6: DATA ANALYSIS USING MATLAB1.0OBJECTIVESAfter completing this lab session, you should be able to: Read data using Import Wizard Plot the data Modeling data files using Matlab2.0INTRODUCTIONMost data analysis problems that you can solve in Matlab begin with data. Matlab provides anumber of functions for generating, recording, and playing data and sound on the computer.After loading the data, you can display it, analyze it, and fit it whether by using MatlabGraphical Interfaces or M-function. Through this section you will exercise the file I/O,graphics, analysis and programming capabilities of Matlab for data analysis purposes.3.0ANALYZING DATA USING IMPORT WIZARDTASK 1: Analyzing Fuel Efficiency DataThis example uses a set of data that was collected over 34 tanks of fuel for a vehicle. It wasentered into a spreadsheet and saved using the (CSV) file format, which is the common textfile format.The first task you might be faced with on a project is to bring some test data into the Matlabenvironment. The easiest way to import data into Matlab is to use the Import wizard, agraphical interface for reading common data file formats. To import text data using theMatlab Import Wizard, perform these steps:1. Select File à Import Data.2. Navigate to the parent directory and select the data file to read.C:\ML01\Fuelbook.csvImport WizardAfter the file is opened, you can specify the character used to separate the individual dataitems. The Import Wizard separates text data, numerical data, and column headers, andprovides a preview of this data.3. To continue importing your data file, click Next.2

Figure 1: Matlab Import Wizard4. Select the variables that you want to import. Click on the 'Create vectors from eachcolumn using column names button' checkbox.Figure 2: Select VariablesClick Finish. This completes the process and puts the data into the Workspace.WorkspaceThe Matlab Workspace consists of the set of variables (named arrays) built up during aMatlab session and stored in memory. Variables are created in the workspace.5. To interact with the variables in the workspace, enable the workspace window byselecting View à Workspace.3

Figure 3: Interact the Variables in WorkspaceInteract with Variables in the Array EditorThe clipboard capability of the Array Editor is compatible with other programs such as Excel.This means that array information can be copied from Excel into Matlab, and from Matlabinto Excel.6.Double click a variable Gallons on the workspace to open it in the Array Editor.Figure 4: Array EditorYou can change or copy an array in the Array Editor. Right clicking on a selection within theArray Editor allows you to edit information to and from clipboard, and visualize the selectedelements with a graphic function.Plot DataThe plot tools environment enables you to- Create various types of graphs- Select variables to plot from Workspace browser- Create and manipulate subplots in the figure- Set properties of graphics objects.7. To plot data, select the data to be displayed and right click it to access the contextmenu.4

Figure 5: To Plot the Data8. Matlab provides many types of graph such as bar graph, pie chart, stem andhistogram. Select Graph à Plot to plot the data as a basic y versus x graphic figure.9. Add a title, x label, y label and legend to the axis by clicking the Insert button at thefigure window.Gallons of Gas Purchased161514Gallons (US)13121110987605101520Tank Number253035Figure 6: Example of Data PlotAdd Data Using Expression10. Using the data imported earlier, compute miles per gallons (MPG) from the variablesTripMeter and Gallons. Because these variables are arrays, the ./ operator is used todivide every element of TripMeter by its corresponding element in Gallons. mpg TripMeter ./ Gallons11. Call the function mean to find the average MPG. avgmpg mean (mpg)avgmpg 22.381612. Create a new figure, and then plot MPG. figure(2) ;p1ot(mpg);5

13. Add a title, x label, y label and legend to the axis by giving the expressions below: xlabel ('Tank Number'); ylabel ('MPG'); title ('Miles per Gallons'); legend ('TripMeter/Gallons');Miles per 0Tank Number253035Figure 7: Plot data using ExpressionExplore Statistics of Data Graphically14. From the "Miles-per Gallons' figure window (MPG graph), select Toolsà datastatistics tools. The data statistics tool calculates basic statistics on the x-data and ydata of the plot in the graph and displays the result in the dialog box.15. We can see that the minimum, maximum and mean value of MPG is 17.94, 29.5 and22.38 respectively. To add a plot of the minimum, maximum and mean value of theTripMeter . /Gallons (y data) to the graph, click in the check box next to the value.Figure 8: Data StatisticFit the Data - Basic FittingMatlab supports curve fitting through fitting through the Basic Fitting interface. Using thisinterface, you can quickly perform many curve fitting tasks within the same environment.16. Open a new figure, create a new graph of just the Cos tPerGallon variable, by givingthe commands below: figure (3); plot(CostPerGallon, ‘r’); title (‘Cost Per Gallon’);6

xlabel (‘Tank Number’); ylabel (‘Cost per Gallon’); legend (‘CostPerGallon’);The character 'r' is used at the plot command to specify the color of the plot into red.17. Select Toolsà Basic Fitting from the figure window. Select show next panel button.Figure 9: Basic Fitting18. The plot fits panel allows you to visually explore one or more fits to the current dataset. The numerical results panel allows you to explore the numerical results of a singlefit to the current data set without plotting the fit.19. Fit the figure using ‘Spline Interpolant coefficient’ by clicking the checkbox at thePlot fits panel.20. Give your comment on the difference between original plot and basic fitting plot.Export Figure into Another Application21. To copy the figure into the clipboard, select Edità Copy Figure. You may paste thefigure into the Microsoft Word file.22. To export the figure into another application, select Fileà Export. For example, youcan Save As the figure into the jpeg format.23. To save the figure using Matlab.fig format, select Fileà Save As.6.0INTERACTIVE DATA PLOTTING IN MATLAB7

Matlab can be used to create effective two-dimensional and three-dimensional plots, which isthe technique for using data plots that describe the data. This section will describe severaldata plots which are frequently used to analyze data.Stem, Stairs and Bar PlotMatlab has several other plot types that are related to xy plots. These include the stem,stairs, and bar plots. Their syntax is: stem (x,y); bar(x,y); stairs (x,y);TASK 2: Create four subplots showing the effects of various bar arguments for the databelow. Do your task in the .Matlab Scripting File/Matlab Editor.Table 1. Task 2Result A Result B Result C10862586095879421. Define a variable for the data as a variable in 5x3 Matrix. Define the variable as y. Dothe sub-plotting as shown in the script file below to obtain various type of bar plot:% Example of bar plot to show the effect of various bar argument.Y [10 8 6; 2 5 8; 6 0 9; 5 8 7; 9 4 2J; %define data Ysubplot(2,2,l)bar (Y, ‘group')title ‘Group'subplot(2,2,2)bar (Y, ‘stack' )title ‘Stack'subplot(2,2,3)bar (Y, ‘stack' )title ‘Stack'subplot(2,2,4)bar (Y, 1.5)title ‘width 1.5’Specialized Plots (Pie Charts and Histograms)A pie plot draws a pie chart using the data in x. Each element in x is represented as a slice inthe pie chart. Pie (x, explode) offsets a slice from the pie. The syntax for pie chart is: pie (x) pie (x,explode) h pie( . )8

A histogram plot shows the distribution of data values.:The syntax for histogram plots:» n hist (Y)»n hist(Y,x)»n hist(Y,nbins)» [n,xout] hist(. .)9

LAB 7: MATLAB SIMULINK1.0 OBJECTIVESAfter completing this lab session, you should be able to: Building the model using Simulink. Design simulation diagrams using Simulink. Export results to Matlab Workspace.2.0 INTRODUCTIONSimulink is built on top of Matlab, so you must have Matlab to use Simulink. Simulinkprovides a graphical user interface that uses a various types of elements called blocks tocreate a simulation of a dynamic system-that is a system that can be modeled with differentialor difference equations whose independent variable is time. For example, one block type is amultiplier, another performs a sum, and another is an integrator. The Simulink graphicalinterface enables you to position the blocks to describe complicated systems for simulation.3.0 SIMULATION DIAGRAMSYou develop Simulink models by constructing a diagram that shows elements of the problemto be solved. Such diagrams are called simulation diagrams or block diagrams.Consider the equation:!"!" 10𝑓 𝑡(1)Its solution can be represented symbolically as:𝑦 𝑡 10𝑓 𝑡 𝑑𝑡(2)The block containing the integral sign f represents the integration process of the equation.The integration symbol in equation (2) can be replaced by the operator symbol 1/s, whichderives from the notation used for Laplace Transform. Thus the equation (2) is representedas:𝑦 10𝑓!(3)!The simulation diagram for equation (3) is:f10Gain1syIntegratorFigure 1: Simulation diagram for  𝑦 10𝑓!!10

Another element used in simulation diagrams is the summer that is used to subtract as well asto sum variables. The summer symbol can be used to represent the equation:!"!" 𝑓 𝑡 10𝑦(4)Equation (4) can be expressed as𝑦 𝑡 𝑓 𝑡 10𝑦 𝑑𝑡(5)Or as!𝑦 ! (𝑓 10𝑦)(6)The simulation diagram for the equation (6) is:fy1sIntegratorGain10!Figure 2: Simulation diagram for 𝑦 ! (𝑓 10𝑦)4.011

TASK 1: SIMULATION USING SIMULINKUse Simulink to solve the following problem for 0 t 13.!"!" 10 sin 𝑡                                      𝑦 0 0(7)The exact solution is 𝑦 𝑡 10(1 cos  𝑡). To construct the simulation, do the followingsteps:12

1. Start Simulink. You can click thefrom the command line.icon on the Matlab toolbar or type simulink2. Open a new model window, select New-s Model on the File menu of either theMatlab desktop or the Simulink Library Browser toolbar. You can also click the Dbutton on the Library Browser toolbar.3. Select and place in the new window the Sine Wave block from the Sources library.Double click on it to open the Block Parameters window, and make sure theAmplitude is set to 1, the frequency to 1, the phase to 0, and the sample time to O.Then click OK.4. Select and place the Gain block from the Math Operations library, double click on it,and set the Gain Value to 10 in the Block Parameter window. Then click OK.5. Note that, the value 10 then appears in the triangle. To make the number more visible,click on the block, and drag one of the corners to expand the block so that all the textis visible.6. Select and place the integrator block from the continuous library, double click on it toobtain the Block Parameters window, and set the initial condition to 0 (this is becausey(O) O). Then click OK.7. Select and place the Scope block from the Sinks library.8. Once the blocks have been placed as shown in Figure 2, connect the input port oneach block to the output port on the preceding block. To do this, move the cursor to aninput port or an output port; the cursor will change to a cross. Hold the mouse buttondown, and drag the cursor to a port on another block. When you release the mousebutton, Simulink will connect them with an arrow pointing at the input port. Yourmodel should now look like that shown in figure 2.9. Click on the Simulation menu, and click the Configuration Parameters item.10. Click on the Solver tab, and enter 13 for Stop time. Make sure the Start time is O.Then click OK.·13

The first task you might be faced with on a project is to bring some test data into the Matlab environment. The easiest way to import data into Matlab is to use the Import wizard, a graphical interface for reading common data file formats. To import text data using the Matlab Import Wizard, perform these s