Anda di halaman 1dari 30

Basic Graphic Commands

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.

M-Files/ Script files:


So far, youve seen that MATLAB can accept and

e ecute com m ands interactively through the com m and prom pt


! what happens if youve got A L OT of com m ands to

e ecute"
# ou can do this in Matla$. This %ind of file is called a

script file or M-file &M for MATLAB'(

# ou place all com m ands you want to e ecute in a file with

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

can create your scripts.

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-

e ecuted witho t having to enter them all again'


All you have to do is m odify parts of the script file to give you

the result you want


Lets do an e am ple, Lets m a%e the factorial e am ple into a script file
5

Example #

"

Lab Tas!-0"
The script file is now set to compute -' +hat if .

wanted to do #$, or "%$, or &$ "


'i(t-) st cha(*e the n parameter accor+i(*ly

Basic Graphics Commands


MATLAB provides a variety of sophisticated

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 ,

This is what the graph loo%s

li%e!

To se the /plot0 f (ctio( i( Matlab1

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-

+rite this in the command prompt in MATLAB


# ou can also m a%e a script file too if you want'

16

MATLAB autom atically color codes the different plots

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 @ @'

legend(, , , ) produces a legend,

la$eling what each plot is on the graph

By default, MATLAB ta%es all points and connects

them with a solid line, and its got its own way of 1/ determ ining which colour $elongs to what plot

A++itio(al parameters for ;raph 4latti(*:


.n addition to the x and y points, you specify an

additional param eter, plot(x, y, line_style);

line_style is a character string of A characters


The 1st character specifies the colo r of your plot The And character specifies how your plot will $e plotted on

the graph, or the plot style

A2

Supported colours,
blue, *reen, r ed, cyan, magenta, yellow, $lac!

Supported plot styles,

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

G dependent vectors of interest

custom colors. Also display legend of the graph.

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'

Multiple plots can $e displayed within a single figure

window using theIsu$plotIfunction, which can $e called as follows, =s bplot>rows1 cols1 whichArea5
Bach slot ta%es in a graph

A6

3ow do we use the s!bplot com m and"

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

.n order to properly use s!bplot, you m ust call this

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

row A rows, heres what .d do

?o s!bplot(221) Specify that we want to wor% on the top left

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(

Lab Tas! -0?


y14 A , yA4 - , y54 8 , y-4 = F 4-6,2.1,6F
>lot all these - outputs on a single graph window.

Mention -la$el, y-la$el on each. And display your Koll no, on top of the graph window.

Anda mungkin juga menyukai