Download - Matlab Primer Simulink

Transcript
  • 1

    MATLAB SIMULINK

    2.3.08.6

    2012

  • 2

    1. .......................................................................................... 32. MATLAB ........................................................................... 33. HELP ................................................................................. 34. Workspace ..................................................................... 65. MATLAB ....................................................................... 76. ............................................................................ 107. ........................................... 118. .................................................................. 139. Workspace ....................................................................... 1810. ................................................................. 1911. ........................................................................... 2012. ............................................................................... 2213. .m ............................................................................. 2414. Matlab ............................... 2615. SIMULINK ...................................................................................... 36 ........................................................................................ 57

  • 3

    1. MATLAB, MATrix LABoratory, , . / MATLAB .

    MATLAB SIMULINK. Users Manual HELP 3. FORUMS . FORUMS search Google.

    MATLAB Windows mouse . MATLAB, menu Command Prompt. .

    2. MATLAB MATLAB/ SIMULINK PC Lab Login ( Username: temporal Password: !@#$% ( 12345) ). T Start Menu Windows MATH.

    3. HELP HELP Command Prompt click Help menu Product Help. :

    Toolboxes , .

  • 4

    Product Help.

    (sin):

    HELP Product Help . MATLAB

  • 5

    help Command Prompt:

    click ..: matlab\matfun :

    Command Prompt "help" -- . help ( ). , Help Menu, .

  • 6

    4. Workspace MATLAB. quit:

    exit:

    command prompt enter click File Exit MATLAB:

    . MATLAB save workspace . click File Save Workspace As...

  • 7

    . click Save in:

    . File name.

    click Save. workspace load g f:\_ extension .mat enter.

    5. MATLAB , , ";", :

    Enter:

  • 8

    sqrt(2) (sqrt(2) = 2 )

    7 2( ) 45

    + 3 .

    , (1x1) (1 x n m x 1), :

    1,1 1,2 1,n2,1 2,2 2,n m,1 m,2 m,n

    (2,3) A, :

    V = 1 6 7 9E2 22 (-) W = 1 2 3 4e3 0 (-) ";" .

    :

  • 9

    .

    . . , V(4)=V(1,4) W(4)=W(4,1).

    (:). :

    1 0.5 :

    2 3 :

    MATLAB:

    zeros ones rand eye

    1

    :

  • 10

    (m x n) . size 1x2 m n . :

    / ([ ]). :

    :

    max min mean std sum prod

    HELP.

    6. / MATLAB :

    + - * / \

  • 11

    ^ det inv eig

    - /

    TLAB. / . :

    Z = X\Y Z = inv(X)*Y

    Z = X/Y Z = X*inv(Y).

    , A^2 A*A A . 2^A . (- ) () det, inv, eig ( HELP ).

    7. - - (.) :

    + - .* ./ .\ .^ .

    :

    :

    :

  • 12

    :

    (;) / Command Prompt. . (.. Help atan2):

  • 13

    sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh

    Four quadrant arctangent

    abs angle sqrt real imag conj round fix floor ceil sign rem exp log log10

    . - + 10

    8. MATLAB . , / . MATLAB :

    plot loglog semilogx semilogy polar mesh contour bar stairs

    x, y y, x 3-D

    plot loglog, semilogx, semilogy, polar . ,

    b a.

  • 14

    :

    :

    plot . MATLAB , , .

    :

    0 1 2 3 4 5 6 7 8 9 10-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    0 1 2 3 4 5 6 7 8 9 10-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

  • 15

    . o x + * - : -. --

    y g c r m b w k

    : plot(vector1,vector2,line-type)

    :

    :

    title xlabel ylabel text gtext grid

    x y

    , :

    0 1 2 3 4 5 6 7 8 9 10-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

  • 16

    :

    . '-' subplot. : subplot(mnr). , m n . r . :

    0 1 2 3 4 5 6 7 8 9 10-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    x-axis in radians

    y-ax

    is

    Graph of sin(a) in stars and cos(a) in circles

  • 17

    :

    : axis([x-min x-max y-min y-max]). , x-min x-max y-min y-max . , File Print OK.

    0 5 10-1

    -0.5

    0

    0.5

    1sin(a)

    0 5 10-1

    -0.5

    0

    0.5

    1cos(a)

    0 5 10-1

    -0.5

    0

    0.5

    1sin(a) and cos(a)

    0 5 10-1

    -0.5

    0

    0.5

    1log10(a)

  • 18

    9. Workspace MATLAB :

    Workspace who whos what clc clear clear format ... hold delete -C

    who .M

    Matlab, Z z (-) .

  • 19

    10. MATLAB . MATLAB +1. y=x3+5x-1 y=[1 0 5 -1]. x y 0 x2 .

    MATLAB :

    roots poly polyval conv deconv

    :

    y r. poly(r) y . x=-2 polyval.

    .. w=2x2-x-1, w=[2 -1 -1] :

  • 20

    w y z=2x5-x4+9x3-7x2-4x+1 z w y .

    11. MATLAB . MATLAB - -TRUE 1 -FALSE 0 .

    & | ~

    /

    & | . ~ .

    :

  • 21

    :

    < >= == ~=

    any all. any(A,i), 1 - i. A

  • 22

    . all - 1.

    12. MATLAB . . FOR, WHILE, IF, IF-ELSE :

    FOR

    for v = expression statements end

    for:

    for i. S(i) (i) . i 1 . i 3 .

    WHILE

    while expression statements end

    while .

    while:

  • 23

    IF

    if expression statements end

    if . if:

    IF-ELSE

    if expression statements else statements end

    if-else . . . . if-else:

  • 24

    if else . g .

    13. .m MATLAB .m. .m click File New Script:

    .

    save File Save As

    .m.

  • 25

    .m function : function [output1, ...] = (1, 2, etc.) function (1, 2, etc.) . "1" .. . " " .

    :

    .m witch.m :

    :

  • 26

    (%) compiler . help witch, :

    14. Matlab (.) (.). MATLAB tf ss zpk ( 14.1) .

    , MATLAB . , , . , . . . 14.2.

    14.1

    sys=tf(num,den) sys=ss(A,B,C,D) sys=zpk(Z,P,K)

    --

    14.2

    [num, den]=ss2tf(A,B,C,D)

    -2 -1.5 -1 -0.5 0 0.5 1 1.5 2-1

    0

    1

    2

    3

    4

    5

    6

    7

    8The Witch Curve

  • 27

    [A,B,C,D]=tf2ss(num,den) [Z,P,K]=ss2zp(A,B,C,D) [Z,P,K]=tf2zp(num,den) [num, den]=zp2tf(Z,P,K) [A,B,C,D]=zp2ss(Z,P,K)

    -- -- -- --

    14.1 14.2, num den , , . A,B,C,D Z, P K .

    14.1: G(s)=1/(s2+2s+1).

    14.1.

    14.1. . . . .

    , MATLAB step impulse . lsim. , initial ( .). 14.3.

    14.3

    step(sys) impulse(sys) lsim(sys,u,t)

    initial(sys,x0,t)

    ( .)

    14.3, sys (.) (.) 14.1, t u ( t u ). x0 .

    step step(num,den) step(A,B,C,D). 14.3. step

  • 28

    . , step(*sys). : lsim, :

    [u,t]=gensig(type,tau),

    type (sin,square, pulse) tau .

    14.2: 1 .

    14.2 .

    14.2. .

    . MATLAB 14.4 :

    14.4

    pole(sys) zero(sys)

    pzmap(sys) minreal(sys)

    -

    14.3: :

  • 29

    A = 3 21 0

    ,B = 1

    0

    ,C= 1 1 ,D =0

    14.3. 3.

    , . MATLAB , , ( ) . 14.5.

    14.5

    sys=series(sys1,sys2) sys=parallel(sys1,sys2)

    sys=feedback(sys1,sys2,-1) sys=feedback(sys1,sys2,+1)

    : sys=feedback(sys1,sys2,-1) sys=feedback(sys1,sys2).

    14.4: 14.4.

  • 30

    14.4. PI .

    14.5, . 14.4.

    14.5. . 14.4.

    , Matlab 14.6.

    14.6

    bode(sys) [Gm,Pm,Wcg,Wcp] = margin(sys)

    Bode

  • 31

    H=freqresp(sys,w) nyquist (sys) nichols (sys)

    nyquist nichols

    14.6, sys , 14.1, w rad/s. Gm Pm Wcg Wcp .

    14.5: Bode Nyquist, -1 -5, 2 K = 3. 14.6, .

    14.6. Bode Nyquist

    14.6 Bode Nyquist.

    14.7. Bode 5.

  • 32

    14.8. Nyquist 5.

    , (..). Matlab , , 14.7.

    14.7

    rlocus(sys) [K, poles]=rlocfind(sys,P)

    P . : rlocfind P. ... 14.6: .. :

    G(s)= 2s3 +6 s2 +5s

    6. 14.9 .

  • 33

    14.9. .. .

    .

    14.11. 14.6.

    14.12. , -6.

    , (.) (.). MATLAB tf ss zpk ( 14.8) .

    14.8

    sys=tf(num,den,Ts) sys=ss(A,B,C,D,Ts) sys=zpk(Z,P,K,Ts)

    --

    Ts . Ts = -1.

    14.9.

    14.9

    dstep(num,den) dimpulse(num,den)

  • 34

    dlsim(num,den,u,t) dinitial(A,B,C,D,x0,t)

    ( .)

    14.7: :

    G z( )= 1z0.5

    14.13, :

    14.13. .

    , , (). , Matlab 14.10.

    14.10 .

    sysd = c2d(sys,Ts,'method') [Ad,Bd,Cd,Dd] = c2dm(A,B,C,D,Ts,'method') [numd,dend] = c2dm(num,den,Ts,'method')

    (. .) (. .)

    method (zoh, foh, imp, tustin, prewarp, matched) Ts . : method, (zoh). matched . prewarp, Wc (rad/s) 14.8, :

    sysd=c2d(sys,Ts,'prewarp',Wc)

    ( 14.2), ( 14.4), ( 14.5), ( 14.6) ( 14.7), . , , 14.3. 14.8: :

    G s( )= 1s s+0.5( )

    PD :

  • 35

    Gc s( )=0.5+ 2 s Tustin, =0.1s . 14.14, Matlab:

    14.14. .

    14.14 :

    14.15.

    14.8.

  • 36

    14.16. 14.8.

    15. SIMULINK SIMULINK MATLAB (blocks). SIMULINK . . SIMULINK, .

    15.1: . M=2400kg. B=320kg/s F=1130N. v(t) v(0)=0 m/s.

    , M F -Bv(t). Newton. :

    v+ B

    Mv= F

    M (15.1)

    . (15.1) , :

    v(t)= FB(1e (B/M )t ) (15.2)

    ( ) SIMULINK.

    simulink Command Window >>simulink

  • 37

    . SIMULINK, (.. Continuous, Discrete ..), 15.1:

    15.1. Simulink.

    FileNewModel untitled . , (blocks) . untitled . . 15.2 blocks Continuous untitled.

  • 38

    15.2. .

    15.1 .

    15.1 Continuous

    Transfer Function

    State Space

    Integrator

    Derivative

    Sources

    Step

  • 39

    Ramp

    Clock

    Sinks

    Scope

    To Workspace

    Math operations

    Gain

    Sum

    , . (15.1) Laplace. : G(s)= v(s)

    F(s)= 1Ms+B

    (15.3)

    F t=0 s, :

    F(s)= Fs

    (15.4)

    untitled : ) Sources Step Clock. Step () . Clock .

  • 40

    15.3. Step Clock.

    ) Sinks Scope To Workspace. Scope To Workspace simout (workspace) MATLAB .

    15.4. Scope To Workspace.

    ) Continuous Transfer Fcn. .

    15.5. Transfer Fcn.

    , . .

    15.6. untitled.

  • 41

    , Step Transfer Fcn. (>) Step (>) Transfer Fcn. , 15.7.

    15.7. Step Transfer Fcn.

    15.8. Scope, (>) Scope Transfer Fcn To Workspace.

    15.8. .

    , To Workspace , To Workspace1 Clock.

  • 42

    15.9. To Workspace1 Clock.

    . Step Step time: 0 Final value: 1130 OK.

    15.10. Step.

    Transfer Fcn Denominator: [2400 320] Apply OK.

  • 43

    15.11. Transfer Fcn.

    To Workspace Variable name: v ( To Workspace1 t) Save format Array Apply OK. , v t.

    15.12. To Workspace.

  • 44

    , . , , To Workspace Velocity. , To Workspace1 Time. 15.13:

    15.13. .

    , (.. , , ..). Simulation Configuration Parameters 15.14.

    15.14. .

  • 45

    , Stop Time: 40, Max Step Size: 1.5 Apply OK. (solver), ode45 . (.. stiff problems) (.. ode15s). Simulation Start ( play). Scope, 15.15. , 15.16, plot(t,v) Command Window.

    15.15. Scope.

    15.16. plot.

    File Save As... (.. submarine) untitled. : . , Stop Time: Max Step Size: - . =M/B=7.5 s. 15.2: 1 v(0)=2 m/s.

    . , ,

  • 46

    . , Simulink, . E. (1) :

    v= B

    Mv+ F

    M (15.5)

    . (15.5) Simulink : ) Continuous o Integrator. . v v .

    15.17. Integrator.

    ) Math operations Gain Sum. Gain . . . Sum .

    15.18. Gain Sum.

    ) Step Scope 15.1. , 15.19.

    15.19. .

  • 47

    . Gain Gain: 1/M Multiplication: Element-wise (K.*u). Gain1 Gain: B/M. Apply OK. : B M, .

    15.20. Gain.

    Sum List of signs: +- Apply OK.

    15.21. Sum.

    Integrator. , Initial condition: 2 Apply OK.

  • 48

    22. Integrator.

    15.1, To Workspace t v . Scope. Scope :

    15.23. Scope.

    Parameters 15.23. :

  • 49

    15.24. Scope.

    Data History Save data to workspace Variable name: velocity Format: Array. velocity Scope ( ). Limit data points to last:, , 15.25.

    15.25. Scope.

    15.26. Gain1 Gain1 FormatFlip Block. ( Sum), . (15.5).

  • 50

    15.26. .

    15.1 :

    15.27. Scope.

    Scope, velocity. t v. Command Window:

    >> t=velocity(:,1);

    >> v=velocity(:,2);

    >> plot(t,v)

    :

  • 51

    15.28. plot.

    Word, 15.28 JPEG, Bitmap, Eps .. 15.28. word .

    15.28. Eps.

    15.3: 15.1 Bv2 v(0)=2 m/s. , :

    v+ B

    Mv2 = F

    M (15.6)

    . (15.6) . , Simulink. :

  • 52

    ) User-defined-functions MATLAB Fcn. Matlab. .

    15.29. MATLAB Fcn.

    ) Signal Routing Mux. / . .

    15.30. Mux.

    ) Step, Scope Integrator 15.2. , 15.31.

    15.31. .

    . Step, Integrator Scope 15.2. MATLAB Fcn MATLAB Fcn: submarine_motion(u(1:2)). submarine_motion function ( 15.33).

  • 53

    . 13. u(1:2) MATLAB Fcn ( F v ). , Output Dimensions: 1 MATLAB Fcn ( v ). Apply OK.

    15.32. MATLAB Fcn.

    15.33. function MATLAB Fcn.

    15.34.

  • 54

    15.34. .

    15.1 :

    15.35. Scope.

    ( ) . , .

  • 55

    . 15.4 PI Simulink. 15.4: , Simulink, . (15.3), PI : kP =160, kI =240

    v=1 m/s . 15.36 .

    15.36. PI .

    Step , 15.10 Gain Gain1 kP kI 15.20. Scope 15.37, Scope1 15.38.

    15.37. .

  • 56

    15.38. PI .

  • 57

    word

    Figure 1 15.28: File Export Setup Size, Rendering, Fonts Lines Export (Eps,JPEG ..) Save OK. Word : 15.28.

    /ColorImageDict > /JPEG2000ColorACSImageDict > /JPEG2000ColorImageDict > /AntiAliasGrayImages false /CropGrayImages true /GrayImageMinResolution 36 /GrayImageMinResolutionPolicy /Warning /DownsampleGrayImages true /GrayImageDownsampleType /Bicubic /GrayImageResolution 300 /GrayImageDepth -1 /GrayImageMinDownsampleDepth 2 /GrayImageDownsampleThreshold 2.00333 /EncodeGrayImages true /GrayImageFilter /DCTEncode /AutoFilterGrayImages false /GrayImageAutoFilterStrategy /JPEG /GrayACSImageDict > /GrayImageDict > /JPEG2000GrayACSImageDict > /JPEG2000GrayImageDict > /AntiAliasMonoImages false /CropMonoImages true /MonoImageMinResolution 36 /MonoImageMinResolutionPolicy /Warning /DownsampleMonoImages true /MonoImageDownsampleType /Bicubic /MonoImageResolution 600 /MonoImageDepth -1 /MonoImageDownsampleThreshold 1.00167 /EncodeMonoImages true /MonoImageFilter /CCITTFaxEncode /MonoImageDict > /AllowPSXObjects false /CheckCompliance [ /None ] /PDFX1aCheck false /PDFX3Check false /PDFXCompliantPDFOnly false /PDFXNoTrimBoxError true /PDFXTrimBoxToMediaBoxOffset [ 0.00000 0.00000 0.00000 0.00000 ] /PDFXSetBleedBoxToMediaBox true /PDFXBleedBoxToTrimBoxOffset [ 0.00000 0.00000 0.00000 0.00000 ] /PDFXOutputIntentProfile (None) /PDFXOutputConditionIdentifier () /PDFXOutputCondition () /PDFXRegistryName (http://www.color.org) /PDFXTrapped /False

    /CreateJDFFile false /Description >>> setdistillerparams> setpagedevice