Beruflich Dokumente
Kultur Dokumente
Lab # 03- SS
Lab Objectives:
To understand M-files principle. To plot multiple plots on a single graph.
execute?
You can do this in Matlab. This kind of file is
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
M-File
Script Files
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
Example #1
Lab Task-01
The script file is now set to compute 4! What if I
techniques for presenting and visualizing data. Also, MATLAB makes it very easy to plot data!
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
10
Example # 02
Heres a basic example: Lets say I wanted to plot the line y = x Heres the syntax:
like
you should first make sure that the matrices/vectors you are trying to use 11 equal dimensions. are of
Lab Task-02
Plot the following: A) y1=2x B) y2=x+3 C) y3=2x+2z
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.
13
Example # 03
Lets do another example: Lets plot the following 5 lines: y1 = 0.1x y2 = 0.5x y3 = 2x y4 = 5x y5 = 10x For now, lets make them all go from 0 to 10 in step sizes of 0.1 These plots dont all have to have the same step size!
14
15
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 youve just seen, or do it through the command prompt
16
Labelling Axes: So lets add a title, add a grid, label the axes and put up a legend.
17
18
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 Dont forget the quotations !
legend(, , , ) produces a
them with a solid line, and its got its own way of 19 determining which colour belongs to what plot
20
Supported colours:
blue, green, red, cyan, magenta, yellow, black
21
Examples:
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
22
Lab Task-03
X = [3 9 27];
Y = [10 8 6];
Z = [4 4 4]; t = [1 2 3]; % independent vector
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: There is also a way to produce multiple graphs in one window
You 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
25
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 bottom
function first After, you code the syntax to plot something normally
26
Heres a small example: If I wanted to make a window that has 4 plots, 2 plots in
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:
To plot rate in the top half of a figure and growth in
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)
Mention x-label, y-label on each. And display your Roll no: on top of the graph window.