lab 006-007 - [new] matlab data analysis and toolbox simulink · to import text data using the...

13
1 FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER DENC 2532 ECADD LAB SESSION 6/7 MATLAB – DATA ANALYSIS AND TOOLBOX Prepared by: Hamzah Asyrani Sulaiman Computer Engineering Department, FKEKK (Sept. 2013)

Upload: others

Post on 06-Oct-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

1

____________________________________________

UNIVERSITI TEKNIKAL MALAYSIA MELAKA

FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

BENC 2113 ECADD

LAB SESSION 5

PSPICE FOR DC ANALYSIS

Prepared by: Norhashimah Mohd Saad

Computer Engineering Department, FKEKK

FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

 

DENC 2532

ECADD

LAB SESSION 6/7

MATLAB – DATA ANALYSIS AND TOOLBOX

Prepared by: Hamzah Asyrani Sulaiman

Computer Engineering Department, FKEKK (Sept. 2013)

Page 2: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

2

LAB 6: DATA ANALYSIS USING MATLAB 1.0 OBJECTIVES After completing this lab session, you should be able to:

• Read data using Import Wizard • Plot the data • Modeling data files using Matlab

2.0 INTRODUCTION Most data analysis problems that you can solve in Matlab begin with data. Matlab provides a number 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 Matlab Graphical 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.0 ANALYZING DATA USING IMPORT WIZARD TASK 1: Analyzing Fuel Efficiency Data This example uses a set of data that was collected over 34 tanks of fuel for a vehicle. It was entered into a spreadsheet and saved using the (CSV) file format, which is the common text file format. 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 steps:

1. Select File à Import Data. 2. Navigate to the parent directory and select the data file to read.

C:\ML01\Fuelbook.csv Import Wizard After the file is opened, you can specify the character used to separate the individual data items. The Import Wizard separates text data, numerical data, and column headers, and provides a preview of this data.

3. To continue importing your data file, click Next.

Page 3: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

3

Figure 1: Matlab Import Wizard

4. Select the variables that you want to import. Click on the 'Create vectors from each

column using column names button' checkbox.

Figure 2: Select Variables

Click Finish. This completes the process and puts the data into the Workspace. Workspace The Matlab Workspace consists of the set of variables (named arrays) built up during a Matlab session and stored in memory. Variables are created in the workspace.

5. To interact with the variables in the workspace, enable the workspace window by selecting View à Workspace.

Page 4: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

4

Figure 3: Interact the Variables in Workspace

Interact with Variables in the Array Editor The 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 Matlab into Excel.

6. Double click a variable Gallons on the workspace to open it in the Array Editor.

Figure 4: Array Editor

You can change or copy an array in the Array Editor. Right clicking on a selection within the Array Editor allows you to edit information to and from clipboard, and visualize the selected elements with a graphic function. Plot Data The 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 context

menu.

Page 5: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

5

Figure 5: To Plot the Data

8. Matlab provides many types of graph such as bar graph, pie chart, stem and

histogram. 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 the figure window.

Figure 6: Example of Data Plot

Add Data Using Expression

10. Using the data imported earlier, compute miles per gallons (MPG) from the variables TripMeter and Gallons. Because these variables are arrays, the ./ operator is used to divide every element of TripMeter by its corresponding element in Gallons.

>> mpg = TripMeter ./ Gallons

11. Call the function mean to find the average MPG. >> avgmpg = mean (mpg)

avgmpg =

22.3816

12. Create a new figure, and then plot MPG. >> figure(2) ;p1ot(mpg);

0 5 10 15 20 25 30 356

7

8

9

10

11

12

13

14

15

16

Tank Number

Gal

lons

(US)

Gallons of Gas Purchased

Page 6: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

6

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');

Figure 7: Plot data using Expression

Explore Statistics of Data Graphically

14. From the "Miles-per Gallons' figure window (MPG graph), select Toolsàdata statistics tools. The data statistics tool calculates basic statistics on the x-data and y-data 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 and 22.38 respectively. To add a plot of the minimum, maximum and mean value of the TripMeter . /Gallons (y data) to the graph, click in the check box next to the value.

Figure 8: Data Statistic

Fit the Data - Basic Fitting Matlab supports curve fitting through fitting through the Basic Fitting interface. Using this interface, 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 giving the commands below:

>> figure (3); plot(CostPerGallon, ‘r’);

>> title (‘Cost Per Gallon’);

0 5 10 15 20 25 30 3516

18

20

22

24

26

28

30

Tank Number

MPG

Miles per Gallons

TripMeter/Gallons

Page 7: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

7

>> 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 Fitting

18. The plot fits panel allows you to visually explore one or more fits to the current data

set. The numerical results panel allows you to explore the numerical results of a single fit to the current data set without plotting the fit.

19. Fit the figure using ‘Spline Interpolant coefficient’ by clicking the checkbox at the Plot fits panel.

20. Give your comment on the difference between original plot and basic fitting plot.

Export Figure into Another Application

21. To copy the figure into the clipboard, select EditàCopy Figure. You may paste the figure into the Microsoft Word file.

22. To export the figure into another application, select FileàExport. For example, you

can Save As the figure into the jpeg format.

23. To save the figure using Matlab.fig format, select FileàSave As.

6.0 INTERACTIVE DATA PLOTTING IN MATLAB

Page 8: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

8

Matlab can be used to create effective two-dimensional and three-dimensional plots, which is the technique for using data plots that describe the data. This section will describe several data plots which are frequently used to analyze data. Stem, Stairs and Bar Plot Matlab 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 data below. Do your task in the .Matlab Scripting File/Matlab Editor.

Table 1. Task 2 Result A Result B Result C

10 8 6 2 5 8 6 0 9 5 8 7 9 4 2

1. Define a variable for the data as a variable in 5x3 Matrix. Define the variable as y. Do the 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 Y

subplot(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 in the pie chart. Pie (x, explode) offsets a slice from the pie. The syntax for pie chart is:

>> pie (x)

>> pie (x,explode)

>> h = pie( ... )

Page 9: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

9

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(. ..)

Page 10: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

10

LAB 7: MATLAB SIMULINK 1.0 OBJECTIVES After 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 INTRODUCTION Simulink is built on top of Matlab, so you must have Matlab to use Simulink. Simulink provides a graphical user interface that uses a various types of elements called blocks to create a simulation of a dynamic system-that is a system that can be modeled with differential or difference equations whose independent variable is time. For example, one block type is a multiplier, another performs a sum, and another is an integrator. The Simulink graphical interface enables you to position the blocks to describe complicated systems for simulation. 3.0 SIMULATION DIAGRAMS You develop Simulink models by constructing a diagram that shows elements of the problem to 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, which derives from the notation used for Laplace Transform. Thus the equation (2) is represented as:

𝑦 = 10𝑓 !!

(3) The simulation diagram for equation (3) is:

Figure 1: Simulation diagram for  𝑦 = 10𝑓 !

!

f y1s

Integrator

10

Gain

Page 11: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

11

Another element used in simulation diagrams is the summer that is used to subtract as well as to 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:

Figure 2: Simulation diagram for 𝑦 = !

!(𝑓 − 10𝑦)

4.0

f y1s

Integrator

10

Gain

Page 12: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

12

TASK 1: SIMULATION USING SIMULINK Use 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 following steps:

Page 13: Lab 006-007 - [NEW] Matlab Data Analysis and Toolbox Simulink · To import text data using the Matlab Import Wizard, perform these steps: 1. Select File! Import Data. 2. Navigate

13

1. Start Simulink. You can click the icon on the Matlab toolbar or type simulink from the command line.

2. Open a new model window, select New-s Model on the File menu of either the Matlab desktop or the Simulink Library Browser toolbar. You can also click the D button 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 the Amplitude 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 text is visible.

6. Select and place the integrator block from the continuous library, double click on it to obtain the Block Parameters window, and set the initial condition to 0 (this is because y(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 on each block to the output port on the preceding block. To do this, move the cursor to an input port or an output port; the cursor will change to a cross. Hold the mouse button down, and drag the cursor to a port on another block. When you release the mouse button, Simulink will connect them with an arrow pointing at the input port. Your model 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.·