30
Lab # 03- SS Basic Graphic Commands

Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Embed Size (px)

Citation preview

Page 1: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Lab # 03- SS

Basic Graphic Commands

Page 2: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Lab Objectives:To understand M-files principle.To plot multiple plots on a single graph.To use different parameters on the graph.To plot multiple graphs in a single graph

window.

Page 3: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

M-Files/ Script files:

3

So far, you’ve seen that MATLAB can accept and execute commands interactively through the command prompt… what happens if you’ve got A LOT of commands

to execute?You can do this in Matlab. This kind of file is

called a script file or M-file (M for MATLAB!)You place all commands you want to execute in a

file with extension .m at the end, and you run the script file

MATLAB will run all of these commands in sequence for you.

MATLAB has a M-file editor that you can use where you can create your scripts.

Page 4: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

M-File

4

Page 5: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Script Files

5

Once a script file has been created, type in the name of the file (without the .m) in the command prompt to execute the script

When you execute a script file, all of the variables created in the script file get stored in the workspace for future use

The great thing about script files is that commands can be re-executed without having to enter them all again!All you have to do is modify parts of the script file to give

you the result you wantLet’s do an example:

Let’s make the factorial example into a script file

Page 6: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Example #1

6

Page 7: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

7

Page 8: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Lab Task-01The script file is now set to compute 4!

What if I wanted to do 9!, or 12!, or 5! ?Hint-Just change the n parameter

accordingly

Page 9: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

9

Basic Graphics Commands MATLAB provides a variety of sophisticated

techniques for presenting and visualizing data. Also, MATLAB makes it very easy to plot data!

Page 10: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

10

2D plotting:

If x and y are arrays of elements that are the same size, you can plot them using this data with the following command:

plot(x, y);

This will bring up a window plotting a graph of y vs. x

Page 11: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

11

Example # 02Here’s a basic example:Let’s say I wanted to

plot the line y = xHere’s the syntax:

This is what the graph looks like…

To use the ‘plot’ function in Matlab, you should first make sure that the matrices/vectors you are trying to use are of equal dimensions.

Page 12: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Lab Task-02

Plot the following:A) y1=2xB) y2=x+3C) y3=2x+2z

Page 13: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

13

Multiple plots on a single Graph:If you want to plot multiple plots on a

single graph, you do the following:plot(x1,y1,x2,y2,…,xN,yN);

N is the number of plots you want to appear on the single graph.

Page 14: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

14

Example # 03Let’s do another example:

Let’s plot the following 5 lines:y1 = 0.1xy2 = 0.5xy3 = 2xy4 = 5xy5 = 10x

For now, let’s make them all go from 0 to 10 in step sizes of 0.1

These plots don’t all have to have the same step size!

Page 15: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

15

Write this in the command prompt in MATLABYou can also make a script file too if you want!

Page 16: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

16

MATLAB automatically color codes the different plots that are on the graph

You can add a title, label the axes, put a grid on and even a legend!

You can add these in the graph GUI that you’ve just seen, or do it through the command prompt

Page 17: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

17

Labelling Axes:So let’s add a title, add a grid, label the axes and put up a

legend.

Page 18: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

18

Page 19: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

19

grid puts a grid on the graphThe spacing for the grid is automatically figured out by

MATLABtitle(‘…’) lets your graph have a titlexlabel(‘…’), ylabel(‘…’) labels the x and y

axes accordinglyPut the labels inside the quotationsDon’t forget the quotations ‘ ‘!

legend(‘…’, ‘…’, …, ‘…’) produces a legend, labeling what each plot is on the graph

By default, MATLAB takes all points and connects them with a solid line, and it’s got its own way of determining which colour belongs to what plot

Page 20: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

20

Additional parameters for Graph Platting:

In addition to the x and y points, you specify an additional parameter:plot(x, y, ‘line_style’);

line_style is a character string of 2 charactersThe 1st character specifies the colour of your

plotThe 2nd character specifies how your plot will be

plotted on the graph, or the plot style

Page 21: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

21

Supported colours:blue, green, red, cyan, magenta, yellow, black

Supported plot styles:

Page 22: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

22

Examples:x = 0:0.1:10;y = x;plot(x,y,’g.’);

This will plot a green line with dots at each pointplot(x,y,’bo’);

This will plot a blue line with circles at each pointplot(x,y,'rx’);

This will plot a red line with crosses at each point

Page 23: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Lab Task-03X = [3 9 27]; % dependent vectors of

interestY = [10 8 6];Z = [4 4 4];t = [1 2 3]; % independent vectorPlot these vectors in a single graph and

embellish with custom colors. Also display legend of the graph.

Page 24: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Multiple plots in different Graph windows:the figure command can be used to display

multiple plots in separate figure windows.

Page 25: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

25

Multiple Graphs in One Window:There is also a way to produce multiple

graphs in one windowYou can have more than 1 graph in one window

at a time!Multiple plots can be displayed within a single

figure window using the subplot function, which can be called as follows:

 subplot(rows, cols, whichArea)

Each slot takes in a graph

Page 26: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

26

How do we use the subplot command?

subplot(m,n,p) or subplot(mnp)

These determine the number of rows (m) and columns (n) for the amount of graphs you want

p determines which location in the window you want the plot to go to

The order is from left to right, top to bottomIn order to properly use subplot, you must call

this function firstAfter, you code the syntax to plot something

normally

Page 27: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

27

Here’s a small example:If I wanted to make a window that has 4 plots, 2

plots in each row 2 rows, here’s what I’d do

Do subplot(221) Specify that we want to work on the top left corner

Next, code the syntax to plot normally. The plot will appear on the top left corner

Do subplot(222) Specify that we want to work on the top right corner

Next, code the syntax to plot normally. The plot will appear on the top right corner

Do subplot(223) Specify that we want to work on the bottom left corner

Next, code the syntax to plot normally. The plot will appear on the bottom left corner

Do subplot(224) Specify that we want to work on the bottom right corner

Next, code the syntax to plot normally. The plot will appear on the bottom right corner

Page 28: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Example:To plot rate in the top half of a figure

and growth in the bottom half,

rate= [3.2 4.1 5.0 5.6]; growth = [2.5 4.0 3.35 4.9]; figure;subplot(2,1,1); plot(rate) subplot(2,1,2); plot(growth)

Page 29: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters
Page 30: Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters

Lab Task -04y1=2x, y2=4x, y3=6x, y4=8x; x= -5:0.1:5;

Plot all these 4 outputs on a single graph window. Mention x-label, y-label on each. And display your Roll no: on top of the graph window.