control engineering lab 2

Upload: johnyrock05

Post on 08-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 control engineering lab 2

    1/15

    MTS-362

    Control Engineering Lab-2

    Introduction to MATLAB its functions and applications

    Plotting Cur!e "itting #Part-II$

    2%& Plotting'

    The simplest graphs to create are plots of points in the cartesian plane. For example:

    >> x = [1;2;3;4;5];

    >> y = [0;.25;3;1.5;2];

    >> plot(x,y)

    The resulting graph is displayed in Figure

    "igure 2%(' A simple Matlab graph

     Notice that, by default, Matlab connects the points with straight line segments. An alternative

    is the following !:

    >> plot(x,y,'o')

    "ontrol #ngineering $ab %&%'

  • 8/19/2019 control engineering lab 2

    2/15

     

    "igure 2%2' Another simple Matlab graph

    All Matlab variables must have numerical values:

    >> x = -10:.1:10;

    The basic plot command:

    >> plot(sin(x))

    "igure 2%3' (raph of )ine

     Note that the hori*ontal axis is mar+ed according to the index, notthe value of x. Fix this as follows:

    "ontrol #ngineering $ab &%'

  • 8/19/2019 control engineering lab 2

    3/15

    >> plot( x, sin(x) )

    "igure 2%)' Another )ine curve

    This is the same thing as plotting -parametric curves.-

    e can plot the -inverse relationship- for example, the s/uaring function and 0&1 s/uare root!easily:

    >> plot( x, x.^2 ) >> plot( x.^2, x )

    "ontrol #ngineering $ab 2&%'

  • 8/19/2019 control engineering lab 2

    4/15

    or a spiral in two or in three dimensions:

    >> t = 0:.1:10; plot( t .*cos(t), t .* sin(t) )

    >> plot3( t .* cos(t), t .*sin(t), t )

    3lot several curves simultaneously with plot(x1, y1, x2,

    y2, ...):

    >> plot( x, cos(x), x, 1 - x.^2./2, x, 1 -x.^2./2 + x.^4./24 )

    "ontrol #ngineering $ab 4&%'

  • 8/19/2019 control engineering lab 2

    5/15

    "igure 2%*'

    2%( Putting se!eral grap+s in one ,indo,

    The subplot command creates several plots in a single window. To be precise,

    subplot(m,n,i)  creates mn plots, arranged in an array with m rows and n columns. 5talso sets the next plot command to go to the ith coordinate system counting across the rows!.6ere is an example

    >> t = (0:.1:2*pi)';

    >> subplot(2,2,1)

    >> plot(t,sin(t))

    >> subplot(2,2,2)

    >> plot(t,os(t))

    >> subplot(2,2,3)

    >> plot(t,!xp(t))

    >> subplot(2,2,4)>> plot(t,1."(1#t.$2))

     

    2%2 o, to plot in .I""E/E0T C1L1/S

    %& you ! + tim! s!!in som! o& t! plots tt you +o in

    mtlb on t! olo -osttions, you soul+ pobbly n!

    t! olos.

    /o &in+ out o- to +o tt, you n typ! t t! mtlb pompt:

    "ontrol #ngineering $ab '&%'

  • 8/19/2019 control engineering lab 2

    6/15

    !lp plot

    %n sot, olo n b! sp!i&i!+ insi+! t! plot ommn+. ou

    n typ!:

    plot(x,y,'-')

    to plot -it! lin!. ou n lso us! ot! olos t!y

    ! ll list!+ in t! plot !lp in&omtion.

    2%3 Soe ore about plotting

    6ere are other ways to graph multiple curves, using matrices plotted by

    columns! and using -hold.-

    "igure 2%6'

    Functions of two variables may be plotted, as well, but some -setup- is re/uired7

    >> [x y] = !s"#$i%(-3:.1:3, -3:.1:3);>> & = x.^2 - y.^2;

    6ere are two options for plotting the surface. $oo+ at the help page for details.

    "ontrol #ngineering $ab 8&%'

  • 8/19/2019 control engineering lab 2

    7/15

      "igure 2%'

    2%) 3. Plots'

    5n order to create a graph of a surface in 21space or a contour plot of a surface!, it isnecessary to evaluate the function on a regular rectangular grid. This can be done using the

    meshgrid command. First, create %9 vectors describing the grids in the  x1 and  y1directions:

    >> x = (0:2*pi"20:2*pi)';

    >> y = (0:4*pi"40:4*pi)';

     Next, spread;; these grids into two dimensions using m!si+:

    >> [,] = m!si+(x,y);

    >> -os

      m! i! 6yt!s 7lss

      41x21 8999 +oubl! y

      41x21 8999 +oubl! y

      x 21x1 189 +oubl! y

      y 41x1 329 +oubl! y

    n+ totl is 194 !l!m!nts usin 1422 byt!sThe effect of m!si+ is to create a vector  with the  x1grid along each row, and a vector

    with the  y1grid along each column. Then, using vectori*ed functions and&or operators, it is

    easy to evaluate a function  z  <  f   x, y! of two variables on the rectangular grid:>> = os().*os(2*);

    6aving created the matrix containing the samples of the function, the surface can be graphed

    using either the m!s or the su& commands :

    "ontrol #ngineering $ab =&%'

  • 8/19/2019 control engineering lab 2

    8/15

    >> m!s(x,y,)

    >> su&(x,y,)

     

    "igure 2%4' >sing the  mesh command

     

    "igure 2%5' >sing the surf  command

    The difference is that su& shades the surface, while m!s does not.! 5n addition, a contour

     plot can be created :

    >> ontou(x,y,)

    "ontrol #ngineering $ab ?&%'

  • 8/19/2019 control engineering lab 2

    9/15

     

    2.' o! o$! ot lottin# n% $p"s

    plot(x,y) !t!s 7t!sin plot o& t! !tos x < y

    plot(y) !t!s plot o& y s. t! num!il lu!s o& t!

    !l!m!nts in t! y!to.

     s!milox(x,y) plots lo(x) s y

     s!miloy(x,y) plots x s lo(y)

     lolo(x,y) plots lo(x) s lo(y)

     i+ !t!s i+ on t! pis plot

     titl!('t!xt') pl!s titl! t top o& pis plot

     xlb!l('t!xt') -it!s 't!xt' b!n!t t! xxis o& plot

     ylb!l('t!xt') -it!s 't!xt' b!si+! t! yxis o& plot

      t!xt(x,y,'t!xt') -it!s 't!xt' t t! lotion (x,y)

    t!xt(x,y,'t!xt','s') -it!s 't!xt' t point x,y ssumin

    lo-! l!&t on! is (0,0) n+ upp! it on! is

    (1,1).

    "ontrol #ngineering $ab @&%'

  • 8/19/2019 control engineering lab 2

    10/15

      pol(t!t,) !t!s pol plot o& t! !tos <

    t!t -!! t!t is in +ins.

      b(x) !t!s b p o& t! !to x. (ot! lso t!

    ommn+ stis(x).)

      b(x,y) !t!s bp o& t! !l!m!nts o& t! !to

    y, lotin t! bs o+in to t! !to

    !l!m!nts o& 'x'. (ot! lso t! ommn+

    stis(x,y).)

    ol$ plots:n#l! = 0:.1*pi:3*pi;$%is = !xp(n#l!/20); pol$(n#l!,$%is),...titl!(n xpl! ol$ lot),...#$i% 

    $ #$p":

    b(x,y) n+ b(y)

    sti (x,y) n+ sti(y)

     ltipl! plots:x1=0:.0'*pi:pi;y1=sin(x1); plot(x1,y1)"ol% 

    y2=cos(x1); plot(x1,y2)

    ou n !t! multipl! ps by usin multipl! um!nts.

    %n ++ition to t! !tos x,y !t!+ !li!, !t! t!

    !tos ,b n+ plot bot !to s!ts simultn!ously s

    &ollo-s.

    = 1 : .1 : 3;  = 10*!xp(-); plot(x,y,,)

    ultipl! plots n b! omplis!+ lso by usin mti!s

    t! tn simpl! !tos in t! um!nt. %& t! um!nts

    o& t! 'plot' ommn+ ! mti!s, t! 7?@ o& y !

    plott!+ on t! o+int! inst t! 7?@ o& x on t!

    bsiss.

    "ontrol #ngineering $ab %&%'

    http://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.7.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.7.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.7.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.5.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.6.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.7.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.7.htmlhttp://web.ew.usna.edu/~mecheng/DESIGN/CAD/MATLAB/matlab/examples/example3.7.html

  • 8/19/2019 control engineering lab 2

    11/15

    2. 56789 76679

      ny osions is! -!n -! ! s!t o& (x,y) pis

    n+ -! +!si! to &in+ n !Aution o &untion tt B&itsB

    t!s! +t. /! po!+u! -! &ollo- n b! !n!lly

    lssi&i!+ in on! o& t-o t!oi!s, int!poltin &untions

    o l!st sAu!s &untions.

     

    /! l!stsAu!s &untion is on! tt obtins t! b!st

    &it, -!! t! m!nin !! o& Bb!stB is bs!+ on minimiin

    t! sum o& t! sAu!s o& t! +i&&!!n!s b!t-!!n t! &untion

    n+ t! +t pis. 7l!ly t! i+! o& Bl!st sAu!sB is to

    i!! som! sot o& !! o m!n lu! &untion -os!

    pil u! +o!s not typilly pss tou ny o& t!

    (x,y) +t pis. %t is

    ppopit! &o !xp!im!ntl +t in -i !!y +t pi is

    obtin!+ un+! on+itions o& un!tinty n+ &o -i ny

    oll!t!+ +t, s ons!Au!n! o& !xp!im!ntl !o, my b!!t! o l!ss tn t! tu! lu!.

    2. 796

  • 8/19/2019 control engineering lab 2

    12/15

    t! unno-n o!&&ii!nts n on!ni!ntly &ollo- 7m!'s

    Dul!. %n !n!l, o-!!, it is ommon to !ly on mtix

    l!b to mnipult! t!s! !Autions into &omt &o -i

    n op!tions o& b substitution -ill obtin t! unno-n

    o!&&ii!nts. E!! -! -ill simply !ly on t! m!to+s o& it

    (o l!&t) +iision in 'mtlb' to obtin t! unno-n

    o!&&ii!nts, l!in t! +!tils o& t! num!il m!to+ &o

    b substitution &o lt! +isussion.

      ot! tt t! s!t o& !Autions n b! on!ni!ntly

    !xp!ss!+ by t! mtix !Aution

      [y] = [][]

    -!!

      y1 1 x1 x12.....x1n#1 1

      y2 1 x2 x22.....x2n#1 2[y] = [] = [] =

      y3 1 x3 x32.....x3n#1 3

      . . . . . .

      . . . . . .

      . . . . . .

      yn#1 1 xn#1 xn#12... xn#1n#1 n#1

    ot! tt t! it si+! o& t! mtix !Aution must b! t!

    po+ut o& 'n#1' x 'n#1' sAu! mtix tim!s 'n#1' x 1

    olumn mtixF /! solution usin uss !limintion lls &o

    l!&t +iision in 'mtlb' o

      [] = []G[y]

      @sin tis m!to+, -! n &in+ t! o!&&ii!nts &o n

    +!!! polynomil tt pss!s !xtly tou 'n#1' +t

    points.

    %& -! ! l! +t s!t, ! +t pi inlu+in

    som! !xp!im!ntl !o, t! n+!!! polynomil is / H I

    7E%7J. Kolynomils o& +!!! l! tn &i! o six o&t!n

    ! t!ibly un!listi b!io 6J/CJJ t! +t points!!n tou t! polynomil u! pss!s tou !!y +t

    point F Hs n !xmpl!, onsi+! t!s! +t.

      x y

      2 4

      3 3

      4 5

    "ontrol #ngineering $ab %&%'

  • 8/19/2019 control engineering lab 2

    13/15

      5 4

      8

      5

      9

      L 10

      10 L

    /!! ! L +t points in tis s!t. %t is l! tt s x

    in!s!s, so lso +o!s y in!s!; o-!!, it pp!s to b!

    +oin so in nonlin! -y. ?!t's s!! -t t! +t loos

    li!. %n 'mtlb', !t! t-o !tos &o t!s! +t.

      xL = [2:1:10];

      yL = [ 4 3 5 4 5 10 L ];

    /o obs!! t!s! +t plott!+ s points, !x!ut!

      plot(xL,yL,'o')

    6!us! -! ! nin! +t pis, it is possibl! to onstut

    n !it+!!! int!poltin polynomil.

      y = 0 # 1x # 2x2 # 3x3 # ....... # 9x9

    /o &in+ t! unno-n o!&&ii!nts, +!&in! t! olumn !to y

      y = yL'

    n+ t! mtix

      =

    [on!s(1,L);xL;xL.$2;xL.$3;xL.$4;xL.$5;xL.$8;xL.$;xL.$9]'

    ot! tt is +!&in!+ usin t! tnspos!, t! on!s()

    &untion n+ t! y op!to ' .$ '. Cit n+ y so

    +!&in!+, t!y stis&y t! !Aution

      [][] = [y]

    ol! &o t! o!&&ii!nts, mtix in t! bo!!Aution, by

    !nt!in t! ommn+

      = Gy

    -i !sults in

    "ontrol #ngineering $ab %2&%'

  • 8/19/2019 control engineering lab 2

    14/15

      = [ 1.0!#003*

      3.9140

      8.8204

      4.931

      1.995L

      .445

      .084L

      .005

      .0003

      .0000 ]

    ot! tt t! nint o!&&ii!nt (9) pp!s to b! !o.

    Htully, it is &init!, it only pp!s to b! !o b!us!

    'mtlb' is pintin only 4 sini&int &iu!s to t! l!&t o&

    t! +!iml point. /o obs!! t! o!&&ii!nts -it mo!

    sini&int &iu!s, !nt! t! ommn+s

    &omt lon

    n+ t! &omt is n!+ to on! -it 15 sini&int +iits.

    7l!ly (9) is not !o, it is smll numb! b!us! it is

    multiplyin numb!, x, is!+ to t! !it po-!.

    o- tt -! ! t! o!&&ii!nts, l!t's !n!t! su&&ii!nt

    numb! o& points to !t! smoot u!. Mo x, &om

    !to o! t! n! 2 N= x N= 10 in in!m!nts o& 0.1.

      x = [ 2:.1:10 ];

    Mo y, lult! t! lu! o& t! !it +!!! polynomil &o

    ! x.

      y =(1)# (2).*x # (3).*x.$2 # (4).*x.$3 #

    (5).*x.$4...

      # (8).*x.$5 # ().*x.$8 # (9).*x.$ # (L).*x.$9;

    o- plot (x,y) n+ t! +t points (xL,yL).

      plot(x,y,xL,yL,'o')

    /! polynomil !sults pp! to pss !xtly tou !!y

    +t point, but l!ly t! polynomil is us!l!ss &o

    !p!s!ntin ou imp!ssion o& t! +t t ny ot! point

    -itin t! n! o& xF /is is ommon b!io &o i

    o+! int!poltin polynomils. Mo tis !son, on! soul+

    n!! tt!mpt to us! i o+! int!poltin polynomils to

    !p!s!nt !xp!im!ntl +t.

    "ontrol #ngineering $ab %4&%'

  • 8/19/2019 control engineering lab 2

    15/15

    ?b /s

    Klot ll tionom!ty, !xpon!ntil n+ ny polynomil &untion

    usin subplot, m!s, su&, plot

    "ontrol #ngineering $ab %'&%'