IBT LUMHS



| |

|Name: _________________________________ Roll No: ____________________ |

| |

|Score: __________ Signature of Tutor: ______________ Date:________________ |

To plot graphs in Matlab

Lab Objective:

← 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.

Script/M-file: One can 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.

 

Basic Graphic Commands:

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

Example#01: Plot the line y = x

Program:

X=0:0.1:10;

Y=x;

Plot(x,y)

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.

Example # 02: Plot the following 5 lines:

y1 = 0.1x, y2 = 0.5x, y3 = 2x, y4 = 5x, y5 = 10x

Program:

x=0:0.1:10;

y1=0.1.*x; y2=0.5.*x; y3=2.*x;

y4=5.*x; y5=10.*x;

plot(x,y1,x,y2,x,y3,x,y4,x,y5);

Labelling Axes:

• grid puts a grid on the graph. The spacing for the grid is automatically figured out by MATLAB

• title(‘…’) lets your graph have a title.

• xlabel(‘…’), ylabel(‘…’) labels the x and y axes accordingly. Put the labels inside the quotations

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

Example #03:

x=0:0.1:10;

y1=0.1.*x;

y2=0.5.*x;

y3=2.*x;

y4=5.*x;

y5=10.*x;

plot(x,y1,x,y2,x,y3,x,y4,x,y5);

grid; % To grid up the graph

title(‘Multiple Plots example’);

xlabel(‘X points’);

ylabel(‘Y points’);

legend(‘y1=0.1x’,’y2=0.5x’, ‘y3=2x’, ‘y4=5x’, ‘y5=10x’);

Additional parameters for Graph Platting:

line_style is a character string of 2 characters. The 1st character specifies the colour of your plot. The 2nd character specifies how your plot will be plotted on the graph, or the plot style

plot(x, y, ‘line_style’);

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

[pic]

Example#04:

x = 0:0.1:10;

y = x;

plot(x,y,’g.’); %This will plot a green line with dots at each point

plot(x,y,’bo’); %This will plot a blue line with circles at each point

plot(x,y,'rx’); %This will plot a red line with crosses at each point

Multiple plots in different Graph windows: The figure command can be used to display multiple plots in separate figure windows.

Multiple Graphs in One Window: Multiple plots can be displayed within a single figure window using the subplot function, which can be called as follows:

subplot(rows, cols, whichArea)

Example#05: 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

Example#06: 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)

Lab Tasks:

1. Plot the following:

A) y1=2x B) y2=x+3 C)y3=2x+2z

2. Plot following vectors in a single graph and embellish with custom colors. Also display legend of the graph.

X = [3 9 27]; % dependent vectors of interest

Y = [10 8 6]; Z = [4 4 4];

t = [1 2 3]; % independent vector

3. Plot following 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.

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

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery