L ab # 03- SS
Lab Objectives:
To understand M-files principle. To plot multiple plots on a single graph. To use different param eters on the graph. To plot multiple graphs in a single graph window.
e ecute"
# ou can do this in Matla$. This %ind of file is called a
e tension .m at the end, and you run the script file MATLAB will run all of these com m ands in se)uence for you.
MATLAB has a M-file editor that you can use where you
3
M-File
Script Files
*nce a script file has $een created, type in the nam e of
the file &without the .m ( in the com m and prom pt to e ecute the script +hen you e ecute a script file, all of the varia$les created in the script file get stored in the wor%space for future use
The great thing a$out script files is that comm ands can $e re-
Example #
"
Lab Tas!-0"
The script file is now set to compute -' +hat if .
techni)ues for presenting and visuali0ing data. Also, MATLAB m a%es it very easy to plot data'
%, plotti(*:
.f x and y are arrays of elem ents that are the same si0e, you
can plot them using this data with the following com m and,
plot(x, y);
This will $ring up a window plotting a graph of y vs. x
12
-.ample # 0%
3eres a $asic e am ple, Lets say . wanted to plot the line y 4 3eres the synta ,
li%e!
yo sho l+ first ma!e s re that the matrices2vectors yo are tryi(* to se are of 11e3 al +ime(sio(s.
Lab Tas!-0%
4lot the followi(*:
A5 y"6%. 75 y%6.83 95 y36%.8%:
M ltiple plots o( a si(*le ;raph: .f you want to plot m ltiple plots on a single graph, you do the following, plot(x1,y1,x2,y2,,xN,yN); N is the num $er of plots you want to appear on the single graph.
15
-.ample # 03
Lets do another e am ple, Lets plot the following 6 lines, y1 = 0.1x y2 = 0.5x y3 = 2x y4 = 5x y5 = 10x 7or now, lets m a%e them all go from 2 to 12 in step si0es of 2.1 These plots dont all have to have the sam e step si0e'
1-
16
that are on the graph # ou can add a title, la$el the a es, put a grid on and even a legend' # ou can add these in the graph 9:. that youve ;ust seen, or do it through the comm and prompt
18
L abelli(* A.es: So lets add a title, add a grid, la$el the a es and put up a legend.
1<
1=
grid puts a grid on the graph The spacing for the grid is autom atically figured out $y MATLAB title() lets your graph have a title xlabel(), ylabel() la$els the and y
a es accordingly
>ut the la$els inside the )uotations ?ont forget the )uotations @ @'
them with a solid line, and its got its own way of 1/ determ ining which colour $elongs to what plot
A2
Supported colours,
blue, *reen, r ed, cyan, magenta, yellow, $lac!
A1
B amples,
x = 0:0.1:10; y = x;
plot(x,y,g.);
This will plot a *ree( line with +ots at each point
plot(x,y,bo);
This will plot a bl e line with circles at each point
plot(x,y,' x); This will plot a re+ line with crosses at each point
AA
Lab Tas!-03
C 4D5 / A<EF # 4D12 = 8EF H 4D- - -EF t 4D1 A 5EF G independent vector >lot these vectors in a single graph and em$ellish with
M ltiple plots i( +iffere(t ;raph wi(+ows: theIfi* reIcomm and can $e used to display multiple plots in separate figure windows.
M ltiple ;raphs i( O(e <i(+ow: There is also a way to produce multiple *raphs in one window
# ou can have m ore than 1 graph in one window at a tim e'
window using theIsu$plotIfunction, which can $e called as follows, =s bplot>rows1 cols1 whichArea5
Bach slot ta%es in a graph
A6
subplot(m,n,p) or subplot(mnp)
These determ ine the num $er of rows (") and col m(s (n) for the
am ount of graphs you want p determ ines which location in the window you want the plot to go to The order is from left to right, top to $ottom
function first After, you code the synta to plot som ething norm ally
A8
3eres a sm all e am ple, .f . wanted to m a%e a window that has - plots, A plots in each
corner Je t, code the synta to plot norm ally. The plot will appear on the top left corner ?o s!bplot(222) Specify that we want to wor% on the top right corner Je t, code the synta to plot norm ally. The plot will appear on the top right corner ?o s!bplot(223) Specify that we want to wor% on the $ottom left corner Je t, code the synta to plot norm ally. The plot will appear on the $ottom left corner ?o s!bplot(224) Specify that we want to wor% on the $ottom right corner Je t, code the synta to plot norm ally. The plot will appear on the $ottom right corner
A<
-.ample:
To plotIrateIin the top half of a figure andIgrowthIin the
$ottom half,
rate4D5.A -.1 6.2 6.8EF growth 4DA.6 -.2 5.56 -./EF figureF su$plot&A,1,1(F plot&rate( su$plot&A,1,A(F plot&growth(
Mention -la$el, y-la$el on each. And display your Koll no, on top of the graph window.