You are on page 1of 439

C s MATLAB

Vector Cc hm V a thc Ma trn In n S dng M-files trong MATLAB Tm tr gip trong MATLAB
Cc lnh chnh ca MATLAB c dng trong ti liu ny l: plot, polyval, roots, conv, deconv, inv, eig v poly.

ch yu trong phn tch v thit k h thng iu khin t ng. C nhiu hp cng c (Toolbox) m rng ng dng cc hm c bn ca MATLAB vo cc lnh vc khc nhau. Trong ti liu ny, chng ti tp trung vo s dng cc hp cng c iu khin (Control Systems Toolbox). MATLAB c cc phin bn chy trong mi trng Unix, Macintosh v Windows. Hin c mt phin bn MATLAB dng cho o to, chy trn PC. Mun c thng tin chi tit v MATLAB hy lin h vi MathWorks. tng ca chng ti khi vit ti liu ny l bn c th hin bi hc trong mt ca s trong khi chy MATLAB trong ca s khc. Bn c th chy cc lnh tnh ton v v trong ti liu bng cch ct vn bn v dn vo MATLAB hoc vo mt M-file.

MATLAB l mt chng trnh tnh ton bng s v hin th s liu theo kiu tng tc. N c dng

Vector
Chng ta bt u t vic to mt i tng n gin, nh mt vector. Nhp cc phn t ca vector (cch nhau mt du trng) gia 2 ngoc vung v gn n vo mt bin. V d, to vector a, hy nhp dng sau vo ca s lnh ca MATLAB (bn c th "copy" v "paste" t browser vo MATLAB cho d):
a = [1 2 3 4 5 6 9 8 7]

MATLAB tr v:
a = 1 2 3 4 5 6 9 8 7

Nu bn mun to mt vector c cc phn t t 0 n 20, mi phn t cch nhau 2 (phng php ny thng c dng to vector thi gian):

t = 0:2:20 t = 0 2 4 6 8 10 12 14 16 18 20

S dng cc vector cng d nh to ra chng. Trc ht, nu mun cng thm 2 vo mi phn t ca vector 'a', bn hy nhp biu thc sau:
b = a + 2 b = 3 4 5 6 7 8 11 10 9

By gi, gi s bn mun cng 2 vector vi nhau. Nu chng c di bng nhau th tht l d, ch cn g vo biu thc:
c = a + b c = 4 6 8 10 12 14 20 18 16

Tr cc vector c cng chiu di c thc hin hon ton ging nh cng chng.

Cc hm (Functions)
cho thun tin, MATLAB c tch hp nhiu hm chun. Mi hm l mt khi lnh thc hin mt chc nng nht nh. MATLAB c tt c cc hm ton hc chun, nh sin, cos, log, exp, sqrt,... Cc hng thng dng, nh pi hay i, j trong cn bc 2 ca -1, cng c trong MATLAB.
sin(pi/4) ans = 0.7071

xem cch s dng hm, g help [tn_hm] trong ca s lnh ca MATLAB. MATLAB cn cho php bn to hm ring ca mnh vi lnh function.Hy bm vo y hc cch to hm v xem danh sch chng ti to ra trong ti liu ny.

V (Plotting)
Vic to mt th trong MATLAB cng d dng. Gi s bn mun v th hm sin theo thi gian. Trc ht, to mt vector thi gian (du chm phy (;) sau mi dng lnh ngn MATLAB a ra ngay gi tr), sau tnh gi tr hm sin cho mi gi tr t.
t=0:0.25:7; y = sin(t); plot(t,y)

th cha khong mt chu k ca hm sin. Chc nng v c bn ca MATLAB rt d dng, ngoi ra lnh plot cn thm cc kh nng b sung khc. Bn hy link n trang plotting bit thm v kh nng ny.

a thc (Polynomials)
Trong MATLAB, mt a thc c biu din thng qua mt vector. To mt a thc trong MATLAB bng cch nhp cc h s ca n theo th t gim dn ca s m. V d a thc sau

c biu din trong MATLAB di dng vector:


x = [1 3 -15 -2 9] x = 1 3 -15 -2 9

MATLAB hiu mt vector c n+1 phn t l mt a thc bc n. V vy, nu a thc b khuyt mt s h s th ta phi in thm s 0 vo v tr tng ng. V d a thc

s c biu din trong MATLAB nh sau:


y = [1 0 0 0 1]

Mun tm gi tr ca a thc ta dng hm polyval. V d, tm gi tr ca a thc trn ti s=2 nh sau


z = polyval([1 0 0 0 1],2) z = 17

Ta cng c th tm nghim ca mt a thc. iu rt thun tin khi c a thc bc cao. V d tm nghim ca a thc

nh lnh sau:
roots([1 3 -15 -2 9]) ans = -5.5745 2.5836 -0.7951 0.7860

Mun nhn 2 a thc vi nhau, ta dng hm conv (convolution) nhn ln lt cc h s ca chng. V d


x = [1 2]; y = [1 4 8]; z = conv(x,y) z = 1 6 16 16

chia hai a thc, ta dng hm deconv.Hm ny tr v c thng s v phn d. V d, cn chia z cho y v kim tra xem c nhn li c x hay khng, ta lm nh sau:
[xx, R] = deconv(z,y) xx = R = 1 0 2 0 0 0

Ta thy li ng a thc (vector) x to trn. Nu php chia khng ht th phn d s khc khng.

Ma trn (Matrices)
Trong MATLAB, ma trn c biu din tng t nh vector, ch khc l cc hng c phn cch bi du chm phy (;) hoc chng c vit thnh tng hng ring bit:
B = [1 2 3 4;5 6 7 8;9 10 11 12] B = 1 5 9 2 6 10 3 7 11 4 8 12

B = [ 1 2 3 4 5 6 7 8 9 10 11 12] B = 1 5 9 2 6 10 3 7 11 4 8 12

Cc ma trn c x l trong MATLAB theo nhiu cch. Trc ht, tm ma trn chuyn v C ca ma trn B, ta dng du lc ('):
C = B' 4

C =

1 2 3 4

5 6 7 8

9 10 11 12

Nu C c dng phc th du lc s cho kt qu phc lin hp. nhn c chuyn v hy dng .' (hai lnh ny cho kt qu nh nhau nu C khng c dng phc). Php nhn 2 ma trn B v C c biu din nh bnh thng, nhng phi ch th t ca chng.
D = B * C D = 30 70 110 70 174 278 110 278 446

= C * B D = 107 122 137 152 122 140 158 176 137 158 179 200 152 176 200 224

Khi cn nhn cc phn t tng ng ca 2 ma trn, ta dng ton t .*. Khi 2 ma trn phi c cng kch thc.
E = [1 2;3 4] F = [2 3;4 5] G = E .* F E =

1 3 2 4 2 12

2 4 3 5 6 20

F =

G =

Nu c mt ma trn vung, bn c th nhn n vi chnh n bao nhiu ln tu bng php nng ly tha:
E^3 ans = 37 81 54 118

tnh lp phng ca mi phn t trong ma trn, ta lm nh sau:


E.^3

ans =

1 27

8 64

Nghch o ca mt ma trn c tm nh hm inv:


X = inv(E) X = -2.0000 1.5000 1.0000 -0.5000

hoc gi tr ring (eigenvalues):


eig(E) ans = -0.3723 5.3723

MATLAB cn c hm tnh cc h s trong a thc c trng ca ma trn. Hm poly to ra mt vector cha cc h s ca a thc c trng.
p = poly(E) p = 1.0000 -5.0000 -2.0000

Ch rng cc gi tr ring (eigenvalues) ca mt ma trn chnh l cc nghim ca a thc c trng ca ma trn ::


roots(p) ans = 5.3723 -0.3723

In (Printing)
in trong MATLAB cn thc hin cc bc sau: Trong Macintosh Mun in mt th hoc m-file trong Macintosh, ch cn kch chut vo th hoc m-file, chn Print trong menu File v n Return. Trong Windows Mun in mt th hoc m-file trong Windows, chn Print trong menu File trong ca s ca th hoc m-file v n Return. Trong Unix Mun in mt th hoc m-file trong Unix th nhp lnh:
6

print -P<printername>

Nu mun lu th in sau th nhp lnh:


print plot.ps -d plot.ps". m-file>.m".

Khi cn in th nhp lnh "lpr -P plot.ps". Nu bn ang dng my in HP th dng lnh "lpr Mun in mt m-file th cng thao tc tng t nh cc file khc, dng lnh "lpr -P <tn Nu dng my in HP th dng lnh "lpr -d plot.ps<name of m-file>.m".

Dng M-files trong MATLAB


S dng M-file trong mi h iu hnh c hi khc nhau. Trong Macintosh C mt b son tho chuyn cho m-files. Chn "New M-file" trong menu File. Bn c th dng mt b son tho bt k nhng phi nh ghi cc file di dng text v np chng mi khi khi ng MATLAB. Trong Windows Chy MATLAB trong Windows tng t nh trong Macintosh. Tuy nhin, m-file c np vo b nh cn ghi n di dng file tn_file.m Trong Unix Bn cn chy mt b son tho c lp t MATLAB. Tt nht l to mt th mc cha tt c cc m-files, sau cd ti th mc trc khi khi ng MATLAB v b son tho. khi ng MATLAB t ca s Xterm ca bn ch cn g: matlab. Bn c th g cc trc tip g cc lnh trong ca s lnh ca MATLAB hoc a tt c cc lnh cn thit trong mt m-file v chy file . Nu bn cha tt c cc m-files ca bn trong cng th mc m t bn khi ng MATLAB th MATLAB s lun lun tm chng.

Tm tr gip (Help) trong MATLAB


MATLAB c mt h tr gip trc tuyn tng i tt. tm tr gip v mt lnh no , g:
help tn_lnh

Bn rt cn bit tn lnh m bn tm kim tr gip. Danh sch cc lnh dng trong ti liu ny c cho trong danh sch lnh. Phm MatLab Commands cui mi trang cho php link n trang danh sch ny. Here are a few notes to end this tutorial. Bn c th nhn c gi tr ca mi bin bng cch g tn bin:
B B =

1 4 7

2 5 8

3 6 9

Bn c th nhp nhiu lnh trn cng mt dng, cc lnh cch nhau bng du chm phy (;) hoc du phy (,). Nu khng gn php tnh hoc kt qu cho mt bin no th MATLAB s gn n vo mt bin tm thi, tn l "ans".

Tutorials

MATLAB C s | MATLAB - To m hnh | PID | Qu tch nghim | p ng tn s | Khng gian trng thi | iu khin s | Simulink C s | Simulink - To m hnh | Cc v d

Simulink Examples Index


Example Description Tutorial Cruise Control Motor Speed Control Motor Position Control Bus Suspension Inverted Pendulum Pitch Control Ball and Beam Descriptions of the MATLAB tutorial examples are available here.

Cruise Control

This is a simple example of the modeling and control of a first order system. This model takes inertia and damping into account. Newton's laws are modeled directly in this example, where forces are summed up to provide the acceleration of the vehicle. A simple PI controller is implemented.

Motor Speed Control

A DC motor has second order speed dynamics when mechanical properties such as inertia and damping as well as electrical properties such as inductance and resistance are taken into account. Newton's law and Kirchoff's law are modeled directly by summing forces and summing voltages to provide the motor's acceleration and armature current, respectively. A lag compensator is implemented.

Motor Position Control

The model of the position dynamics of a DC motor is third order, because measuring position is equivalent to integrating speed, which adds an order to the motor speed example. In this example, however, the motor parameters are taken from an actual DC motor used in an undergraduate controls course. This motor has very small inductance, which effectively reduces the example to second order. This uses the same model as the motor speed example with an additional integrator to provide position from the velocity signal. In this example, a discrete-time model extraction and a discrete-time controller are implemented around the continuous plant model.

Bus Suspension

This example looks at the active control of the vertical motion of a bus suspension. It takes into account both the inertia of the bus and the inertia of the suspension/tires, as well as springs and dampers. An actuator is added between the suspension and the bus. Newton's law is modeled directly by summing forces acting on each of the two inertias. A full-state feedback controller is implemented by extracting a set of states directly from the model.

Inverted Pendulum

The inverted pendulum is a classic controls demonstration where a pole is balanced vertically on a motorized cart. It is interesting because without control, the system is unstable. This is a fourth order nonlinear system. This is a particularly difficult system to model in Simulink because of the algebraic constraint. While Newton's laws are still modeled directly, some calculations must be done in advance to derive the form of the algebraic constraint. A PID controller is implemented using Simulink's built-in PID block.

Pitch Control

The pitch angle of an airplane is controlled by adjusting the angle (and therefore the lift force) of the rear elevator. The aerodynamic forces (lift and drag) as well as the airplane's inertia are taken into account. This is a third order, nonlinear system which is linearized about the operating point. The Simulink model is based on the State-Space model developed in the MATLAB tutorials, and the state equations are implemented directly. Because of this, the state vector is available for use in a full-state-feedback controller.

Ball and Beam

This is another classic controls demo. A ball is placed on a straight beam and rolls back and forth as one end of the beam is raised and lowered by a cam. The position of the ball is controlled by changing the angular position of the cam. This is a second order system, since only the inertia of the ball is taken into account, and not that of the cam or the beam. Rather than modeling forces and accelerations, the Lagrangian equations of motion are implemented is Simulink, eliminating the need to express the algebraic constraint explicitly as was done in the inverted pendulum example.

Tutorials

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital
6

Control | Simulink Basics | Simulink Modeling | Examples

MATLAB - To m hnh
H thng xe la M hnh tch v nh lut Newton Bin trng thi v cc phng trnh ra Biu din trong MATLAB
MATLAB c th c dng m t h thng hoc m hnh vt l. Trong ti liu ny chng ta s hc cch s dng phng trnh vi phn to m hnh trong MATLAB. By gi chng ta s biu din h thng vt l nh tp hp cc phng trnh vi phn.

H thng xe la
Gi thit h thng ch c mt u my v mt toa xe v chy theo mt hng. Chng ta cn iu khin xe sao cho n khi ng v dng m v chy vi tc khng i. K hiu khi lng ca u my v ca toa ln lt l M1 v M2, cng ca l xo lin kt gia chng l k, lc ko ca u my l F v h s ma st ln ti cc bnh xe l .

M hnh tch v nh lut Newton


Sau khi tch lin kt, m hnh h thng c biu din nh sau:

T nh lut Newton, chng ta bit rng tng cc lc tc dng ln vt bng tch khi lng ca vt vi gia tc ca n. Trong trng hp ny, h lc tc dng ln M1 gm lc l xo, lc ma st v lc ko ca ng c. H lc tc dng ln M2 gm lc l xo v lc ma st. Cc lc tc dng theo phng thng ng, gm trng lc v phn lc vung gc vi mt ng, cn bng. V vy khng c gia tc theo phng thng ng. Cc phng trnh chuyn ng theo phng nm ngang nh sau:

Bin trng thi v cc phng trnh ra


H phng trnh trng thi c xy dng vi cc bin trng thi, gm cc to X1, X2; vn tc V1, V2 v thng s u vo l lc ko F. H phng trnh c dng sau:

Gi s thng s ra ca h thng l vn tc ca xe. V vy phng trnh ra s l:

1. Hm truyn
tm hm truyn ca h thng, trc ht phi biu din cc phng trnh vi phn di dng Laplace.

Thng s ra Y(s) = V2(s) = s X2(s). Kh X1 xut hin t s Y(s)/F(s). Khi tm hm truyn, gi thit rng iu kin u bng 0. Hm truyn c dng sau:

2. Khng gian trng thi


Phng php khc gii bi ton l s dng khng gian trng thi. Bn ma trn A, B, C, D c trng cho h thng s c dng gii bn ton. Dng khng gian trng thi c tm t cc bin trng thi v phng trnh thng s ra c biu din nh sau:

Biu din trong MATLAB


By gi chng ta s a cc phng trnh trn vo m-file ca MATLAB. V MATLAB khng th dng cc bin k t, chng ta gn gi tr cho cc bin nh sau: M1 = 1 kg M2 = 0.5 kg k = 1 N/m F= 1 N u = 0.002 sec/m g = 9.8 m/s^2 Chng ta to mt m-file vi cc bin sau:
M1=1; M2=0.5; k=1; F=1; u=0.002; g=9.8;

gii bi ton, chng ta c 2 phng n: 1) Dng hm truyn, hoc 2) Dng khng gian trng thi. Nu dng hm truyn th hy thm cc dng sau vo cui m-file va to ra:
num=[M2 M2*u*g 1]; den=[M1*M2 2*M1*M2*u*g M1*k+M1*M2*u*u*g*g+M2*k M1*k*u*g+M2*k*u*g]; train=tf(num,den)

Nu dng khng gian trng thi th thm cc lnh sau, thay cho cc ma trn num v den:
A=[ 0 1 0 0; -k/M1 -u*g k/M1 0; 0 0 0 1; k/M2 0 -k/M2 -u*g]; B=[ 0; 1/M1; 0; 0]; C=[0 1 0 0]; 3

D=[0]; train=ss(A,B,C,D)

Hy xem cch nhp ma trn trong MATLAB c s.

Tip tc gii bi ton


Khi h cc phng trnh vi phn m t h thng c nhp vo MATLAB di dng hm truyn hoc khng gian trng thi th c th nghin cu v h thng iu khin kn hoc h. Hu ht bi ton c th gii bng cch s dng hm truyn hoc m hnh khng gian trng thi. Hn na, c th chuyn i gia dng ny sang dng khc. Phng php chuyn i c trnh by trong trang Chuyn i. Sau y l danh sch 7 trang trnh by cc v d v to m hnh, bn c th link n nghin cu.

Modeling Examples Tutorials

Cruise Control | Motor Speed | Motor Position | B gim xc xe but | Inverted Pendulum | Pitch Controller | Ball and Beam

MATLAB c s | MATLAB - To m hnh | PID | Qu tch nghim | p ng tn s | Khng gian trng thi | iu khin s | Simulink C s | Simulink - To m hnh | Cc v d

V d: M hnh h iu khin xe
M t h thng Cc ch tiu thit k M hnh MATLAB p ng h kn Hm truyn mch kn

M t h thng
M hnh iu khin xe tng i n gin. Nu b qua qun tnh ca cc bnh xe v gi thit rng lc ma st (t l thun vi vn tc ca xe) tc dng ngc chiu chuyn ng ca xe, th m hnh tr thnh m hnh khi lng v gim chn nh di y.

Theo nh lut Newton, nhn c h phng trnh m t h thng nh sau: (1) trong u l lc pht ng ca ng c. Trong v d ny, chng ta dng cc gi tr

m = 1000 kg b = 50N sec/m u = 500 N

Cc ch tiu thit k
H cn phi tho mn cc ch tiu thit k sau:. Khi lc ko ca ng c bng 500 N, xe s t vn

tc ln nht l 10 m/s (22 mph) trong khong thi gian khng qu 5 giy. Cho php lng qu iu khin tc ti 10% v sai s tnh ln nht l 2%. Thi gian gia tc < 5 sec Qu iu khin tc < 10% Sai s tnh < 2%

M hnh M ATLAB
1. Hm truyn
tm hm truyn cn bin i h phng trnh (1) sang Laplace. Khi xc nh hm truyn, cn gi thit iu kin u bng 0. Dng chuyn i Laplace ca 2 phng trnh nh sau:

V vn tc l thng s ra, ta thay V(s) bng Y(s)

Vy hm truyn ca h bng

gii bi ton trong MATLAB, bn copy on lnh sau vo m-file mi:


m=1000; b=50; u=500; num=[1]; den=[m b]; cruise=tf(num,den)

Lnh ny s cn c dng li tm p ng h kn ca h thng trc tn hiu vo dng bc thang n v. Nhng trc khi lm iu , ta cn xem xt m hnh khng gian trng thi.

2. Khng gian trng thi


Chng ta vit li h phng trnh ban u (1) di dng m hnh khng gian trng thi.

Mun dng MATLAB gii bi ton, cn to m-file nh sau:


m = 1000; b = 50;

u = 500; A = [-b/m]; B = [1/m]; C = [1]; D = 0; cruise=ss(A,B,C,D)

Ghi ch: C th chuyn i bi ton t dng khng gian trng thi sang dng hm truyn v ngc li trong MATLAB. Chi tit v chuyn i, xem Chuyn i

p ng h kn
Chng ta hy xem phn ng h kn trc thng s vo l bc thang n v (step input). Thm lnh sau vo cui m-file v chy trong ca s lnh ca MATLAB:
step(u*cruise)

Bn s nhn c th sau:

T th, ta thy thi gian xe t vn tc 10 m/s vt qu 100 giy. iu khng t yu cu v thi gian tng tc (khng qu 5 giy).

Hm truyn mch kn
ci thin cht lng iu khin, ta to h iu khin kn vi mch phn hi n v, c s khi nh hnh sau.

Hm truyn ca i tng (plant) chnh l hm truyn xc nh c trn {Y(s)/U(s)=1/ms+b}. B iu khin cn c thit k tho mn cc iu kin t ra ban u. C 4 phng php thit k b iu khin c lit k cui trang m bn c th chn, gm PID (t l - tch phn - vi phn),
3

Root-locus (qu tch nghim), Frequency response (p ng tn s), hoc State-space (khng gian trng thi).

Cc v d v m hnh ho Cc v d iu khin xe Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB c s | MATLAB - To m hnh | PID | Qu tch nghim | p ng tn s | Khng gian trng thi | iu khin s | Simulink C s | Simulink - To m hnh | Cc v d

V d: M hnh iu khin tc ng c in mt chiu


M hnh vt l v h phng trnh Yu cu thit k Biu din trong MATLAB v p ng mch kn

M hnh vt l v h phng trnh


ng c in mt chiu (DC Motor) l thit b chp hnh c dng ph bin trong iu khin. N cung cp chuyn ng quay, v cng vi cc c cu truyn ng c kh khc, nh bnh rng, tang trng, cp,... c th cho chuyn ng tnh tin. Mch in phn ng v s tch ca rotor nh trong hnh sau:

Gi thit cc thng s vt l ca h thng nh sau. * Moment qun tnh ca rotor (J) = 0.01 kg.m^2/s^2 * H s gim chn ca h thng c kh (b) = 0.1 Nms * Hng s in c (K=Ke=Kt) = 0.01 Nm/Amp * in tr (R) = 1 ohm * in cm (L) = 0.5 H * Input (V): in p ngun * Output (theta): v tr ca trc * Gi thit rotor v trc cng vng tuyt i M men trn trc, T, c quan h vi dng in phn ng, i, thng qua hng s Kt. Sut in ng phn ng, e, quan h vi tc quay qua cc phng trnh:

Trong h n v SI (m chng ta s dng), Kt (hng s phn ng) bng Ke (hng s motor) v c k hiu chung l K. T hnh v trn, chng ta c th vit cc phng trnh da trn nh lut Newton, kt hp vi nh lut Kirchhoff:

1. Hm truyn
Sau bin i Laplace, m hnh h thng c dng.

Bng cch kh I(s) chng ta nhn c hm truyn mch h, trong , in p ngun l thng s vo v tc quay ca trc l thng s ra.

2. Khng gian trng thi


Di dng khng gian trng thi, c th biu din cc phng trnh trn bng cch chn tc quay v dng in lm cc bin h thng, in p ngun lm input v tc quay lm output.

Yu cu thit k
Thot u ng c ch c th quay vi tc 0.1 rad/sec vi in p vo l 1 V (ta s thy khi m phng p ng mch h). Yu cu c bn i vi ng c l quay vi vn tc yu cu v sai s tnh di 1%. Yu cu khc v nng sut l thi gian ng c phi t ti vn tc n nh sau khi khi ng l khng qu 2 giy. trnh s c do tc ng c qu cao, cn hn ch lng qu iu khin nh hn 5%. Nu chng ta cp in p vo di dng bc thang n v th tc quay (output) phi t cc yu cu: Thi gian t vn tc n nh di 2 giy Qu iu khin di 5%

Sai s tnh di 1%

Biu din trong MATLAB v p ng mch h


1. Hm truyn
Chng ta c th biu din hm truyn ni trn trong MATLAB bng cch nh ngha cc ma trn t s v mu s nh sau:

To m-file mi v nhp cc lnh:


J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; motor=tf(num,den);

By gi chng ta s xem h thng h ban u c hnh thnh nh th no. Thm cc lnh sau vo cui m-file v chy n trong ca s lnh ca MATLAB:
step(motor,0:0.1:3); title('Step Response for the Open Loop System');

Ta s nhn c th sau:

T th, ta nhn thy khi t in p 1 V vo ng c th n ch t vn tc ti a l 0.1 rad/s, nh hn 10 ln so vi vn tc yu cu. Hn na, thi gian ng c t vn tc n nh ko di ti 3 s.

iu cng khng tho mn yu cu v thi gian gia tc.

2. Khng gian trng thi


Chng ta cng c th biu din h thng nh m hnh khng gian trng thi. Hy chy th on lnh trong m-file sau:
J=0.01; b=0.1; K=0.01; R=1; L=0.5; A=[-b/J K/J -K/L -R/L]; B=[0 1/L]; C=[1 0]; D=0; motor_ss=ss(A,B,C,D); step(motor_ss)

Chy m-file trong ca s lnh ca MATLAB, bn s nhn c ng kt qu nh trn.

Modeling Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Speed Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control: PID | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Physical Setup System Equations Design Requirements MATLAB Representation and Open-Loop Response

Example: Modeling DC Motor Position

Physical Setup
A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide transitional motion. The electric circuit of the armature and the free body diagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab. * moment of inertia of the rotor (J) = 3.2284E-6 kg.m^2/s^2 * damping ratio of the mechanical system (b) = 3.5077E-6 Nms * electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp * electric resistance (R) = 4 ohm * electric inductance (L) = 2.75E-6 H * input (V): Source Voltage * output (theta): position of shaft * The rotor and shaft are assumed to be rigid

System Equations
The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf, e, is related to the rotational velocity by the following equations:

In SI units (which we will use), Kt (armature constant) is equal to Ke (motor constant). From the figure above we can write the following equations based on Newton's law combined with Kirchhoff's law:

1. Transfer Function
Using Laplace Transforms the above equations can be expressed in terms of s.

By eliminating I(s) we can get the following transfer function, where the rotating speed is the output and the voltage is an input.

However during this example we will be looking at the position, as being the output. We can obtain the position by integrating Theta Dot, therefore we just need to divide the transfer function by s.

2. State Space
These equations can also be represented in state-space form. If we choose motor position, motor speed, and armature current as our state variab, we can write the equations as follows:

Design requirements
We will want to be able to position the motor very precisely, thus the steady-state error of the motor position should be zero. We will also want the steady-state error due to a disturbance, to be zero as
2

well. The other performance requirement is that the motor reaches its final position very quickly. In this case, we want it to have a settling time of 40ms. We also want to have an overshoot smaller than 16%. If we simulate the reference input (R) by a unit step input, then the motor speed output should have: Settling time less than 40 milliseconds Overshoot less than 16% No steady-state error No steady-state error due to a disturbance

MATLAB representation and open-loop response


1. Transfer Function
We can put the transfer function into MATLAB by defining the numerator and denominator as vectors: Create a new m-file and enter the following commands:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; motor=tf(num,den);

Now let's see how the original open-loop system performs. Add the following command onto the end of the m-file and run it in the MATLAB command window:
step(motor,0:0.001:0.2)

You should get the following plot:

From the plot we see that when 1 volt is applied to the system, the motor position changes by 6

radians, six times greater than our desired position. For a 1 volt step input the motor should spin through 1 radian. Also, the motor doesn't reach a steady state which does not satisfy our design criteria

2. State Space
We can put the state space equations into MATLAB by defining the system's matrices as follows:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; A=[0 1 0 0 -b/J K/J 0 -K/L -R/L]; B=[0 ; 0 ; 1/L]; C=[1 0 0]; D=[0]; motor=ss(A,B,C,D);

The step response is obtained using the command


step(motor)

Modeling Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Position Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control:RL| Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Physical setup Design requirements Equations of motion Transfer function equation Entering equations into MATLAB Open-loop response Control Block Diagram State Space Method

Example: Modeling a Bus Suspension System using Transfer Functions

Physical setup

Designing an automatic suspension system for a bus turns out to be an interesting control problem. When the suspension system is designed, a 1/4 bus model (one of the four wheels) is used to simplify the problem to a one dimensional spring-damper system. A diagram of this system is shown below:

Where: * body mass (m1) = 2500 kg, * suspension mass (m2) = 320 kg, * spring constant of suspension system(k1) = 80,000 N/m, * spring constant of wheel and tire(k2) = 500,000 N/m, * damping constant of suspension system(b1) = 350 Ns/m. * damping constant of wheel and tire(b2) = 15,020 Ns/m. * control force (u) = force from the controller we are going to design.

Design requirements:
A good bus suspension system should have satisfactory road holding ability, while still providing comfort when
1

riding over bumps and holes in the road. When the bus is experiencing any road disturbance (i.e. pot holes, cracks, and uneven pavement),the bus body should not have large oscillations, and the oscillations should dissipate quickly. Since the distance X1-W is very difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the distance X1-X2 instead of X1-W as the output in our problem. Keep in mind that this is an estimation. The road disturbance (W) in this problem will be simulated by a step input. This step could represent the bus coming out of a pothole. We want to design a feedback controller so that the output (X1-X2) has an overshoot less than 5% and a settling time shorter than 5 seconds. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and return to a smooth ride within 5 seconds.

Equations of motion:
From the picture above and Newton's law, we can obtain the dynamic equations as the following:

Transfer Function Equation:


Assume that all of the initial condition are zeroes, so these equations represent the situation when the bus wheel goes up a bump. The dynamic equations above can be expressed in a form of transfer functions by taking Laplace Transform of the above equations. The derivation from above equations of the Transfer Functions G1(s) and G2(s) of output,X1-X2, and two inputs,U and W, is as follows.

Find the inverse of matrix A and then multiple with inputs U(s)and W(s) on the right hand side as the following:

When we want to consider the control input U(s) only, we set W(s) = 0. Thus we get the transfer function G1(s) as the following:

When we want to consider the disturbance input W(s) only, we set U(s) = 0. Thus we get the transfer function G2(s) as the following:

Entering equations into MATLAB


We can put the above Transfer Function equations into MATLAB by defining the numerator and denominator of Transfer Functions in the form, nump/denp for actuated force input and num1/den1 for disturbance input, of the standard transfer function G1(s) and G2(s): G1(s) = nump/denp G2(s) = num1/den1 Now, let's create a new m-file and enter the following code:
m1=2500; m2=320; k1=80000; k2=500000; b1 = 350; b2 = 15020; nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; P=tf(nump,denp); num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; G2=tf(0.1*num1,den1);

Open-loop response
We can use MATLAB to display how the original open-loop system performs (without any feedback control). Add the following commands into the m-file and run it in the MATLAB command window to see the response of unit step actuated force input and unit step disturbance input. Note that the step command will generate the unit step inputs for each input.
step(P)

From this graph of the open-loop response for a unit step actuated force, we can see that the system is underdamped. People sitting in the bus will feel very small amount of oscillation and the steady-state error is about 0.013 mm. Moreover, the bus takes an unacceptably long time to reach the steady state (the settling time is very large). The solution to this problem is to add a feedback controller to the system.

step(G2)

To see some details, you can change the axis:


axis([0 10 -.1 .1])

From this graph of open-loop response for 0.1 m step disturbance, we can see that when the bus passes a 10 cm high bump on the road, the bus body will oscillate for an unacceptably long time(100 seconds) with larger amplitude, 13 cm, than the initial impact. People sitting in the bus will not be comfortable with such an oscillation. The big overshoot (from the impact itself) and the slow settling time will cause damage to the suspension system. The solution to this problem is to add a feedback controller into the system to improve the performance. The schematic of the closed-loop system is the following:

From the above transfer functions and schematic, we can draw the bus-system block diagram as the following:

From the schematic above we see that: Plant = nump/denp F * Plant=num1/den1 so that F=num1/(den1*Plant)

Modeling Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Modeling an Inverted Pendulum


Problem setup and design requirements Force analysis and system equations MATLAB representation and the open-loop response

Problem setup and design requirements


The cart with an inverted pendulum, shown below, is "bumped" with an impulse force, F. Determine the dynamic equations of motion for the system, and linearize about the pendulum's angle, theta = Pi (in other words, assume that pendulum does not move more than a few degrees away from the vertical, chosen to be at an angle of Pi). Find a controller to satisfy all of the design requirements given below.

For this example, let's assume that M mass of the cart 0.5 kg m mass of the pendulum 0.2 kg b friction of the cart 0.1 N/m/sec l length to pendulum center of mass 0.3 m I inertia of the pendulum 0.006 kg*m^2 F force applied to the cart x cart position coordinate theta pendulum angle from vertical For the PID, root locus, and frequency response sections of this problem we will be only interested in the control of the pendulum's position. This is because the techniques used in these tutorials can only be applied for a single-input-single-output (SISO) system. Therefore, none of the design criteria deal with the cart's position. For these sections we will assume that the system starts at equilibrium, and experiences an impulse force of 1N. The pendulum should return to its upright position within 5 seconds, and never move more than 0.05 radians away from the vertical.
1

The design requirements for this system are: Settling time of less than 5 seconds. Pendulum angle never more than 0.05 radians from the vertical. However, with the state-space method we are more readily able to deal with a multi-output system. Therefore, for this section of the Inverted Pendulum example we will attempt to control both the pendulum's angle and the cart's position. To make the design more challenging we will be applying a step input to the cart. The cart should achieve its desired position within 5 seconds and have a rise time under 0.5 seconds. We will also limit the pendulum's overshoot to 20 degrees (0.35 radians), and it should also settle in under 5 seconds. The design requirements for the Inverted Pendulum state-space example are: Settling time for x and theta of less than 5 seconds. Rise time for x of less than 0.5 seconds. Overshoot of theta less than 20 degrees (0.35 radians).

Force analysis and system equations


Below are the two Free Body Diagrams of the system.

Summing the forces in the Free Body Diagram of the cart in the horizontal direction, you get the following equation of motion:

Note that you could also sum the forces in the vertical direction, but no useful information would be gained. Summing the forces in the Free Body Diagram of the pendulum in the horizontal direction, you can get an equation for N:

If you substitute this equation into the first equation, you get the first equation of motion for this system:

(1) To get the second equation of motion, sum the forces perpendicular to the pendulum. Solving the system along this axis ends up saving you a lot of algebra. You should get the following equation:

To get rid of the P and N terms in the equation above, sum the moments around the centroid of the pendulum to get the following equation:

Combining these last two equations, you get the second dynamic equation: (2) Since MATLAB can only work with linear functions, this set of equations should be linearized about theta = Pi. Assume that theta = Pi + ( represents a small angle from the vertical upward direction). Therefore, cos(theta) = -1, sin(theta) = - , and (d(theta)/dt)^2 = 0. After linearization the two equations of motion become (where u represents the input):

1. Transfer Function
To obtain the transfer function of the linearized system equations analytically, we must first take the Laplace transform of the system equations. The Laplace transforms are:

Since we will be looking at the angle Phi as the output of interest, solve the first equation for X(s),

then substitute into the second equation, and re-arrange. The transfer function is:

where,

From the transfer function above it can be seen that there is both a pole and a zero at the origin. These can be canceled and the transfer function becomes:

2. State-Space
After a little algebra, the linearized system equations equations can also be represented in state-space form:

The C matrix is 2 by 4, because both the cart's position and the pendulum's position are part of the output. For the state-space design problem we will be controlling a multi-output system so we will be observing the cart's position from the first row of output and the pendulum's with the second row.

MATLAB representation and the open-loop response


1. Transfer Function

The transfer function found from the Laplace transforms can be set up using MATLAB by inputting the numerator and denominator as vectors. Create an m-file and copy the following text to model the transfer function:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q];

q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q pend=tf(num,den)

-(M+m)*m*g*l/q

Your output should be:


Transfer function: 4.545 s ---------------------------------s^3 + 0.1818 s^2 - 31.18 s - 4.455

To observe the system's velocity response to an impulse force applied to the cart add the following lines at the end of your m-file:
t=0:0.01:5; impulse(pend,t) axis([0 1 0 60])

You should get the following velocity response plot:

As you can see from the plot, the response is entirely unsatisfactory. It is not stable in open loop. You can change the axis to see more of the response if you need to convince yourself that the system is unstable. Although the output amplitude increases past 60 radians (10 revolutions), the model is only valid for small . In actuality, the pendulum will stop rotating when it hits the cart ( =90 degree).
5

2. State-Space
Below, we show how the problem would be set up using MATLAB for the state-space model. If you copy the following text into a m-file (or into a '.m' file located in the same directory as MATLAB) and run it, MATLAB will give you the A, B, C, and D matrices for the state-space model and a plot of the response of the cart's position and pendulum angle to a step input of 0.2 m applied to the cart.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matrices A = [0 1 0 0; 0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0; 0 0 0 1; 0 -(m*l*b)/p m*g*l*(M+m)/p 0] B = [ 0; (i+m*l^2)/p; 0; m*l/p] C = [1 0 0 0; 0 0 1 0] D = [0; 0] pend=ss(A,B,C,D); T=0:0.05:10; U=0.2*ones(size(T)); [Y,T,X]=lsim(pend,U,T); plot(T,Y) axis([0 2 0 100])

You should see the following output after running the m-file:

The blue line represents the cart's position and the green line represents the pendulum's angle. It is obvious from this plot and the one above that some sort of control will have to be designed to improve the dynamics of the system. Several example controllers are included with these tutorials; select from below the one you would like to use.

Note: The solutions shown in the PID, root locus and frequency response examples may not yield a workable controller for the inverted pendulum problem. As stated previously, when we put this problem into the single-input, single-output framework, we ignored the x position of the cart. The pendulum can be stabilized in an inverted position if the x position is constant or if the cart moves at a constant velocity (no acceleration). Where possible in these examples, we will show what happens to the cart's position when our controller is implemented on the system. We emphasize that the purpose of these examples is to demonstrate design and analysis techniques using MATLAB; not to actually control an inverted pendulum.

Modeling Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Modeling a Pitch Controller


Physical setup and system equations Design requirements Transfer function and state-space MATLAB representation and open-loop response Closed-loop transfer function

Physical setup and system equations


The equations governing the motion of an aircraft are a very complicated set of six non-linear coupled differential equations. However, under certain assumptions, they can be decoupled and linearized into the longitudinal and lateral equations. Pitch control is a longitudinal problem, and in this example, we will design an autopilot that controls the pitch of an aircraft. The basic coordinate axes and forces acting on an aircraft are shown in the figure below:

Assume that the aircraft is in steady-cruise at constant altitude and velocity; thus, the thrust and drag cancel out and the lift and weight balance out each other. Also, assume that change in pitch angle does not change the speed of an aircraft under any circumstance (unrealistic but simplifies the problem a bit). Under these assumptions, the longitudinal equations of motion of an aircraft can be written as:

(1)
Please refer to any aircraft-related textbooks for the explanation of how to derive these equations. Also, click Variables to see what each variable represents. For this system, the input will be the elevator deflection angle, and the output will be the pitch angle.
1

Design requirements
The next step is to set some design criteria. We want to design a feedback controller so that the output has an overshoot of less than 10%, rise time of less than 2 seconds, settling time of less than 10 seconds, and steady-state error of less than 2%. For example, if the input is 0.2 rad (11 degrees), then the pitch angle will not exceed 0.22 rad, reaches 0.2 rad within 2 seconds, settles to 2% of the steady-state within 10 seconds, and stays within 0.196 to 0.204 rad at the steady-state. Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

Transfer function and the state-space


Before finding transfer function and the state-space model, let's plug in some numerical values to simplify the modeling equations (1) shown above.

(2)
These values are taken from the data from one of Boeing's commercial aircraft.

1. Transfer function
To find the transfer function of the above system, we need to take the Laplace transform of the above modeling equations (2). Recall from your control textbook, when finding a transfer function, zero initial conditions must be assumed. The Laplace transform of the above equations are shown below.

After few steps of algebra, you should obtain the following transfer function.

2. State-space
Knowing the fact that the modeling equations (2) are already in the state-variable form, we can rewrite them into the state-space model.

Since our output is the pitch angle, the output equation is:

MATLAB representation and open-loop response


Now, we are ready to observe the system characteristics using MATLAB. First, let's obtain an open-loop system to a step input and determine which system characteristics need improvement. Let the input (delta e) be 0.2 rad (11 degrees). Create an new m-file and enter the following commands.
de=0.2; num=[1.151 0.1774]; den=[1 0.739 0.921 0]; pitch=tf(num,den); step(de*pitch) axis([0 15 0 0.8])

Running this m-file in the MATLAB command window should give you the following plot.

From the plot, we see that the open-loop response does not satisfy the design criteria at all. In fact the open-loop response is unstable. If you noticed, the above m-file uses the numerical values from the transfer function. To use the
3

state-space model, enter the following commands into a new m-file (instead of the one shown above) and run it in the command window.
de=0.2; A=[-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0]; B=[0.232; 0.0203; 0]; C=[0 0 1]; D=[0]; pitch=ss(A,B,C,D); step(de*pitch) axis([0 15 0 0.8])

You should get the same response as the one shown above. Note: It is possible to convert from the state-space to transfer function, or vice versa using MATLAB. To learn more about conversions, see Conversions

Closed-loop transfer function


To solve this problem, a feedback controller will be added to improve the system performance. The figure shown below is the block diagram of a typical unity feedback system.

A controller needs to be designed so that the step response satisfies all design requirements. Several different methods to design a controller are listed at the bottom of this page.

Modeling Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Pitch Controller Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control| Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: M hnh qu bng v thanh


Thit lp bi ton Phng trnh h thng Biu din p ng ca h thng h trong Matlab

Thit lp bi ton
Mt qu bng t trn thanh nh hnh bn di, y qu bng c ln trn thanh , l mt bc t do. Mt tay quay c gn 1 u vo thanh , u kia gn vo ng c. Khi ng c quay 1 gc teta, tay n s lm cho thanh quay 1 gc alpha. Khi thanh thay i gc t v tr cn bng, trng lc s khin qu bng ln trn thanh . Ta cn thit k mt b iu khin kim sot v tr ca qu bng.

Vi bi ton ny, chng ta gi thit qu bng ln khng trt trn thanh v ma st gia bng v thanh l khng ng k. Cc tham s v bin cho bi ton ny xc nh nh sau: M khi lng ca bng 0.11 kg R bn knh ca bng 0.015 m d tay quay 0.03 m g gia tc trng trng 9.8 m/s^2 L chiu di thanh 1.0 m J momen qun tnh ca bng 9.99e-6 kgm^2 r to ca bng alpha to gc ca thanh theta gc quay ca ng c

Mc tiu ra iu khin: Thi gian qu khng qu 3 giy qu iu chnh nh hn 5%

Phng trnh h thng


o hm bc hai ca gc quay alpha trn thc t s lm thay i to r. Tuy nhin, chng ta b qua iu ny. Phng trnh Lagrange dch chuyn ca bng c vit nh sau:

Tuyn tnh ho gc quay ca alpha (alpha = 0) cho ta phng trnh gn ng sau:

Phng trnh m t mi lin h gia gc quay ca thanh v gc quay ca ng c vit gn ng nh sau:

Thay vo phng trnh trc, ta c phng trnh:

1. Hm truyn
Bin i Laplace phng trnh trn ta c phng trnh mi:

Vit li di dng hm truyn gia gc quay ca ng c teta(s) v to dich chuyn ca bng R(s) ta c hm truyn.

Ch hm truyn ca c cu chp hnh y l tch phn cp hai. Do vy n bin n nh v s rt phc tp c th iu khin.

2. Khng gian trng thi


Phng trnh tuyn tnh ho ca h cng c th biu din bng khng gian trng thi. iu ny thc hin bng cch chn to r v vn tc r chm lm bin trng thi, gc quay teta lm u vo. Khng gian trng thi c biu din nh sau :

Tuy nhin, vi v d nay ta biu din hi khc mt cht. Phng trnh cho bng vn gi nguyn nhng thay v iu khin to bng theo gc quay teta ca ng c ta s iu khin momen quay ca thanh . Di y l phng trnh biu din ca khng gian trng thi:

Ch : Vi h thng mi ny ta coi nh khng c tay quay v ng c, thay vo l mt ng c gn vo gia thanh to momen quay iu khin to bng.

Biu din trong Matlab v kho st p ng ca h thng h


1. Hm truyn
Hm truyn sau khi bin i Laplace c th biu din trong Matlab bng cch khai bo t s v mu s nh hai vector. lm iu ny ta to mt m-file c cc lnh nh sau:
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; ball=tf(num,den)

Hm truyn cui cng se l:


Transfer function: 0.21 ---s^2

By gio chng ta quan st xem qu bng s ln nh th no khi u vo l tn hiu bc thang 0.25 m. Thm vo m-file lnh sau:
step(0.25*ball)

Ta s c to dch chuyn ca bng theo thi gian nh sau:

T th trn ta thy rng h thng h l khng n nh, bng s ln ra khi thanh . Do vy ta cn

mt s phng php iu khin to bng. Bn c c th chn trong 3 phng php l PID, Qu tch nghim (Root Locus), v p ng tn s (Frequency Response).

2. Khng gian trng thi


Khng gian trng thi c biu din trong Matlab vi cc lnh sau y (cc phng trnh ny dng cho iu khin momen quay nh cp trn).
m R g J = = = = 0.111; 0.015; -9.8; 9.99e-6;

H = -m*g/(J/(R^2)+m); A=[0 1 0 0 0 0 H 0 0 0 0 1 0 0 0 0]; B=[0;0;0;1]; C=[1 0 0 0]; D=[0]; ball=ss(A,B,C,D);

p ng vi u vo tn hiu bc thang 0,25m c c sau khi thc hin lnh:


step(0.25*ball)

u ra theo thi gian c dng sau:

Cng ging nh th c c t hm truyn ta thy rng h thng h l khng n nh, bng s ln ra khi thanh . Do vy ta cn mt s phng php iu khin to bng. Cc v d v khng gian trng thi s cho bn mt s cch iu khin iu hnh. Nu bn mun bit cch lm sao chuyn t biu din trong khng gian trng thi sang biu din theo hm truyn tham kho phn Chuyn i.

Cc m hnh v d

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Bi ton qu bng v thanh

Ball & Beam Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

PID
Gii thiu Ba lut iu khin c tnh ca tng lut iu khin P, I, v D Cc v d p ng bc thang n v ca h thng vng h iu khin t l Piu khin t l-vi phn Piu khin t l-tch phn iu khin t l-vi phn-tch phn Nhng gi chung thit k b iu khin PID
Lnh dng trong MATLAB tm hiu v phn ny l: step feedback

Gii thiu
Phn ny s cho cc bn bit v c tnh ca tng lut iu khin t l(P), vi phn(D) v tch phn(I) v cch dng chng c c p ng mong mun. Ta xt 1 h thng nh sau:

Plant: L 1 c cu php hnh cn iu khin Controller: a ra nhng kch thch cho plant v c thit k iu khin ton b h thng

Ba lutiu khin
The hm truyn ca phng php iu khin PID c dng nh sau:

Kp = H s t l KI = H s tch phn Kd = H s vi phn Trc tin, ta tm hiu xem b iu khin PID lm vic nh th no, ta ly s h thng trn. Bin e th hin cho sai s gia u vo mong mun R v u ra chp hnh Y. Tn hiu lch ny s c gi n b iu khin PID v b iu khin PID s tnh c tch phn v vi phn ca sai s. Tn hiu iu khin u c gi tr bng Kp ln bin ca sai s cng vi Ki ln tch phn ca sai s cng vi Kd ln vi phn ca sai s.

Tn hiu u s c gi ti c cu chp hnh, v cho ra u ra Y. u ra Y s c gi n sensor o v ta li c gi tr sai s e mi. B iu khin s li ly gi tr sai s mi, tnh ton tch phn, vi phn v li a ra tn hiu iu khin. Qu trnh ny s c lp i lp li.

c tnh ca lut iu khin P, I, v D


Lut iu khin P c tc dng lm gim thi gian qu nhng khng th kh c steady-state error. Lut iu khin I c tc dng kh c steady-state error, nhng n lm gim kh nng p ng tc thi ca h thng. Lut iu khin D c tc dng tng n nh ca h thng gim overshoot, v ci thin kh nng p ng tc thi. Hiu qu ca tng lut iu khin P, I, v D ln h thng vng kn c tng kt qua bng sau. . CL RESPONSE RISE TIME OVERSHOOT SETTLING TIME S-S ERROR Kp Ki Kd Gim Gim t thay i Tng Tng Gim t thay i Tng Gim Gim Kh c t thay i

Nn nh s tng quan ny khng hon ton tuyt i, v cc gi tr Kp, Ki, v Kd l ph thuc vo nhau. Trn thc t, khi thay i gi tr ca h s lut iu khin ny s lm thay i hiu qu tc ng ca lut kia. V vy bng trn ch l tham kho trong qu trnh thit k ta c th chn Ki, Kp v Kd.

V d
Gi s ta c 1 h khi lng, l xo v chng rung nh sau.

Phng trnh ng lc hc ca h thng trn l (1) Bin i Laplace t (1), ta c

Hm truyn gia dch chuyn X(s) v u vo F(s) by gi l

Ly cc gi tr M = 1kg b = 10 N.s/m k = 20 N/m F(s) = 1 Lp cc gi tr trn vo hm truyn ta c

Mc ch ca v d ny l minh ho cho vic chn Kp, Ki v Kd sao cho Qu nhanh Overshoot nh nht Khng c steady-state error

p ng bc thang n v ca h thng h
Trc ht ta kho st p ng bc thang n v ca h thng h. To mt m-file c cc lnh nh sau:
num=1; den=[1 10 20]; plant=tf(num,den); step(plant)

Chy m-file ny ta s c th nh sau.

Gi tr DC gain ca hm truyn l 1/20, do vy 0.05 l gi tr cui ca output p ng u vo bc thang n v. Nh vy steady-state error l 0.95, thc s l kh ln. Hn na, thi gian qu khong 1 giy, thi gian n nh khong 1,5 giy. By gi nhim v ca ta l thit k b iu khin sao cho gim thi gian qu , thi gian n n nh ngn, v kh c steady-state error.

Lut iu khin P
Chng ta bit lut iu khin P lm gim thi gian qu , tng overshoot, v gim steady-state error. Hm truyn iu khin vng kn vi lut iu khin P by gi l:

Chn Kp = 300 v thm vo m-file on m lnh sau:


Kp=300; contr=Kp; sys_cl=feedback(contr*plant,1); t=0:0.01:2; step(sys_cl,t)

Chy m-file trong MATLAB ta c kt qu nh sau.

Ch : Hm feedback dng to mt hm truyn vng kn t mt hm truyn vng h cho trc (thay cho vic thit lp li bng tay). T th trn ta thy rng lut iu khin P lm gim thi gian qu v steady-state error, nhng tng qu iu chnh, v lm ko di thi gian n n nh i 1 cht.

iu khin t l-vi phn


By gi chng ta xt n lut iu khin PD. Nh bng nu phn trc, ta thy rng lut iu khin D lm gim c qu iu chnh v lm h thng nhanh n n nh. Hm truyn vng kn vi lut iu khin PD by gi l:

Nh trc ta vn ly Kp = 300, ly Kd = 10. Nh trc ta thm vo m-file on m lnh sau v chy cho ra th.
Kp=300; Kd=10; contr=tf([Kd Kp],1); sys_cl=feedback(contr*plant,1); t=0:0.01:2; step(sys_cl,t)

th ny cho ta thy ngay lut iu khin D gim c qu iu chnh v lm h thng nhanh n n nh so vi trc, v tc ng khng ng k ti thi gian qu cng nh steady-state error.

iu khin t l-tch phn


Trc khi xy dng b iu khin PID, chng ta xem xt ti lut iu khin PI . Nh bng tng kt, ta thy rng lu iu khin I lm gim thi gian qu , nhng lm tng qu iu chnh v ko di thi gian n n nh ca h thng, nhng li kh c steady-state error. Vi h thng trn, chuyn thnh hm truyn vng kn vi lut iu khin PI ta c hm truyn mi:

By gi ta chn li Kp = 30, Ki = 70. To m-file vi cc dng lnh sau.


Kp=30; Ki=70; contr=tf([Kp Ki],[1 0]); sys_cl=feedback(contr*plant,1); t=0:0.01:2; step(sys_cl,t)

Chy chng trnh trong MATLAB ta c th sau.

L do chng ta gim Kp i l v lut iu khin I cng c tc dng lm gim thi gian qu v tng qu iu chnh nh lut iu khin P. th trn cho ta thy lut iu khin I kh c sai s xc lp.

iu khin t l-tch phn-vi phn(PID)


By gi ta xt n b iu khin PID. Hm truyn ca h thng trn vi b iu khin PID vng kn by gi l:

Sau mt s ln chy th vi cc tham s khc nhau, ta chn c Kp=350, Ki=300, v Kd=50 h thng s t c c tnh nh mong mun. Bn c c th th li bng cch to m-file vi cc lnh sau v chy th. Chng ta s c th p ng nh mong mun.
Kp=350; Ki=300; Kd=50; contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(contr*plant,1); t=0:0.01:2; step(sys_cl,t)

By gi ta c mt h thng iu khin vng kn khng c qu iu chnh, qu nhanh, v khng c sai s xc lp.

Mt s gi cho thit k b iu khin PID


Khi mun thit k mt b iu khin PID cho mt h thng no ta i theo nhng bc sau y. 1. 2. 3. 4. 5. Kho st ht thng h v xc nh iu g cn c ci thin Thm vo lut iu khin P ci thin qu trnh qu Thm vo lut iu khin D gim qu iu chnh Thm vo lut iu khin I kh sai s xc lp i chnh cc gi tr Kp, Ki, v Kd cho n khi t c p ng nh mong mun. Bn cng c th tham kho trang "PID Tutorial" bit lut iu khin no tc ng vo nhng c tnh no.

Cui cng, xin nh l khng pha lc no ta cng phi kt hp c 3 lut iu khin (t l, vi phn, v tch phn) vo h thng. V d, nu lut iu khin PI cho p ng t cht lng tt ri (nh v d trn), th chng ta khng nht thit thm vo c lut iu khin D. Cn thit k lm sao h thng ca ta cng n gin cng tt.

V d v b iu khin PID Tutorials

iu khin xe | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php iu khin xe s dng b iu khin PID


iu khin t l iu khin PI iu khin PID
Hm truyn ca h thng iu khin xe c dng.

m = 1000 b = 50 U(s) = 10 Y(s) = vn tc ra s khi chung c feedback ca h thng nh sau.

Tiu chun t ra cho thit k iu khin l: Thi gian qu < 5 sec qu iu chnh < 10% Sai s xc lp < 2% bit cch thit lp m hnh h thng, xem trang M hnh iu khin xe . Nhc li kin thc t PID tutorial, hm truyn ca 1 b iu khin PID l

Trc ht ta xem xt lut iu khin P.

iu khin P
iu u tin cn quan tm l tm ra hm truyn iu khin vng kn vi lut iu khin P t h thng trn. Bng cch bin i s khi, hm truyn iu khin vng kn vi lut iu khin P by gi l:

Nhc li kin thc v b iu khin PID, Lut iu khin P lm gim thi gian qu . V y cng l vn t ra phn M hnh iu khin xe . Trc ht ta cho Kp = 100 v xem th h thng p ng nh th no. To mt m-file c cc lnh sau.
Kp=100; m=1000; b=50; u=10; num=[1]; den=[m b]; cruise=tf(num,den); sys_cl=feedback(Kp*cruise,1); t=0:0.1:20; step(u*sys_cl,t) axis([0 20 0 10])

Chy m-file trong MATLAB s c p ng bc thang n v nh sau.

Nh ta thy trn th, c sai s xc lp v thi gian qu u khng t c mc tiu ra. Ta c th tng h s Kp ci thin cht lng u ra. Sa li m-file 1 cht Kp = 10000 v chy li trong MATLAB. Ta s c th sau .

Sai s xc lp gim gn v 0 trong khi thi gian qu ch cn nh hn 0.5 giy. Tuy nhin, p ng trn l khng thc t lm bi v mt h thng iu khin xe trn thc t khng th thay i vn tc xe t 0 n 10 m/s trong thi gian nh hn 0.5 giy. Gii php cho vn ny l chn Kp sao cho c mt thi gian qu hp l, v thm vo iu khin tch phn kh sai s xc lp.

iu khin PI
Hm truyn iu khin vng kn ca h thng iu khin xe vi b lut iu khin PI l:

Nhc li kin thc phn PID tutorial, khi ta thm iu khin tch phn vo h thng th s c tc dng kh sai s xc lp. By gi cho Kp = 600, Ki = 1 v xem p ng ca ht thng by gi ra sao. Sa li m-file nh sau.
m=1000; b=50; num=[1]; den=[m b]; cruise=tf(num,den); Kp = 600; Ki = 1; contr=tf([Kp Ki],[1 0]); sys_cl=feedback(contr*cruise,1); u=10; t=0:0.1:20; step(u*sys_cl,t) axis([0 20 0 10])

Ta c th p ng u ra nh sau:

By gi ta iu chnh cc h s Kp v Ki t c p ng mong mun. Khi iu chnh Ki, chng ta nn bt u vi gi tr nh v nu Ki ln s c th lm h thng mt n nh. Khi Kp = 800 v Ki =l 40, p ng bc thang n v s c dng sau:

Nh chng ta thy, p ng ny t c mc tiu ra.

iu khin PID
Vi v d ny, ta khng cn thc hin lut iu khin D m vn t c muc tiu ra. Tuy nhin, ta cng nn xem h thng s hot ng nh th no nu c b iu khin PID c th tham kho sau ny. Hm truyn iu khin vng kn ca h thng vi b iu khin PID nh sau.

Cho Kp = 1, Ki = 1, v Kd = 1 sau to mt m-file nh sau.


Kp=1; Ki=1; Kd=1; m=1000;

b=50; u=10; cruise=tf(num,den); contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(contr*cruise,1); t=0:0.1:20; step(u*sys_cl,t) axis([0 20 0 10])

Chy m-file ny ta s c p ng ca h thng. iu chnh cc gi tr Kp, Kd, v Ki n khi bn c c p ng nh mun. Chng ti ginh vic cho bn t thc hnh. Li khuyn: Thng thng vic chn cc gi tr tham s phi tri qua qu trnh th nhiu ln. Cch tt nht trnh cng vic bun t l bn nn iu chnh tng tham s (Kp, Kd, hoc Ki) mt v quan st xem tham s nh hng th no n u ra ca ht thng. Nhng c tnh nh hung ca Kp, Kd, v Ki c tng hp trong phn PID Tutorial.

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

V d v b iu khin PID V d v iu khin xe Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: iu khin PID vn tc ng c mt chiu


iu khin t l iu khin PI iu chnh h s
T v d trc y, ta c phng trnh ng lc hc ca v hm truyn h ca m hnh ng c 1 chiu nh sau:

v s khi ca h thng c dng nh sau:

bit cch thit lp m hnh h thn, xem trang Modeling a DC Motor. Vi hm u vo bc thang 1 rad/sec, mc tiu thit k l: Thi gian n nh nh hn 2 giy qu iu chnh nh hn 5% Sai s xc lp nh hn 1% By gi ta thit k 1 b iu khin PID thm vo ht thng. Trc ht to mt m-file gm cc cu lnh sau (tham kho phn Modeling).
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; motor=tf(num,den);

Nh li hm truyn ca b iu khin PID.

iu khin P
Trc ht ta th iu khin P vi Kp = 100. lp hm truyn iu khin vng kn, ta dng lnh feedback. Thm vo cui m-file va ri cc lnh sau:
Kp=100; contr=Kp; sys_cl=feedback(contr*motor,1);

By gi ta s xem p ng bc thang ca h thng nh th no. Li thm tip vp cui m-file va ri on lnh sau:
t=0:0.01:5; step(sys_cl,t) title('Step response with Proportional Control')

Khi chy m-file ta s c th nh sau:

iu khin PID
T th trn ta c th thy sai s xc lp v qua iu chnh u rt ln. Nh li phn PID tutorial rng lut iu khin I kh c sai s xc lp v lut D lm gim qu iu chnh. Ta cng th b iu khin PID vi cc gi tr nh ca Ki v Kd. Sa li m-file nh di y. Sau chy m-file ta s c th.
J=0.01; b=0.1; K=0.01; R=1; L=0.5;

num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; motor=tf(num,den); Kp=100; Ki=1; Kd=1; contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(contr*motor,1); step(sys_cl) title('PID Control with small Ki and Kd')

iu chnh h s
Hin ti ta thy thi gian n nh l qu ln. Ta phi tng gi tr Ki gim thi gian n t n nh. Sa li gi tr Ki trong m-file bng 200. Cho chy m-file ta s c th nh sau:

By gi ta c th thy p ng nhanh n nh hn trc, nhng gi tr Ki ln lm gim kh nng p ng tc thi ( qu iu chnh ln). Ta tng Kd gim q iu chnh. Sa li gi tr Kd trong m-file bng 10. Cho chy m-file ta s c th nh sau:

By gi ta c b iu khin PID vi Kp=100, Ki=200, Kd=10, tho mn mc tiu t ra

V d v b iu khin PID Motor Speed Examples Tutorials

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: iu khin PID gc quay ng c


iu khin t l iu khin PID iu chnh h s
t vn , phung trnh ng lc hc hm truyn ca h thng nh sau:

v s khi ca h thng:

bit cch thit lp m hnh h thng tham kho trang M hnh ng c 1 chiu Vi u vo bc thang 1 rad, mc tiu thit k l: Thi gian n nh nh hn 0,04 giy qu iu chnh nh hn 16% Khng c sai s thit lp Khng b pht sinh sai s thit lp do nhiu Ta thit k 1 b iu khin PID cho vo trong h thng trn. Trc tin ta to mi 1 m-file c cc dng lnh sau (tham kho hm truyn pha trn).
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; motor=tf(num,den);

Nh li hm truyn ca b iu khin PID c dng:

iu khin t l
Trc tin ta th iu khin t l vi h s Kp = 1,7. lp hm truyn iu khin vng kn ta dng lnh feedback. Thm cc lnh sau vo m-file:
Kp=1.7; contr=Kp; sys_cl=feedback(contr*motor,1);

By gi mun xem p ng ca h thng nh th no ta thm cc lnh tip theo vo m-file v chy:


t=0:0.001:0.2; step(sys_cl,t)

Ta c th nh sau:

By gi mun bit p ng vi nhiu nh th no ta thm cc lnh sau vo cui m-file, sau chy trong Matlab:
dist_cl=feedback(motor,contr); step(dist_cl,t)

Ta s c th sau:

iu khin PID
T th trn ta thy rng sai s thit lp l chp nhn c nhng thi gian n nh qu ln cng nh qu iu chnh l cao. Cng c th thy rng sai s thit lp do nhiu tc ng cng ln. Nh li phn PID tutorial rng lut P kh c sai s thit lp v lut D lm gim qu iu chnh. Nhng trc ht hy th iu khin PI khc phc nh hng ca nhiu. Sa li m-file nh sau:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; motor=tf(num,den); Kp=1.7; Ki=20; contr=tf([Kp Ki],[1 0]); sys_cl=feedback(contr*motor,1); t=0:0.001:0.4; step(sys_cl,t)

Ta s c th nh sau:

V xem nh hng ca nhiu bc thang nh th no, hy thm cc lnh sau vo m-file:


figure dist_cl=feedback(motor,contr); step(dist_cl,t)

Ta s c th sau:

Nh vy lut I c tc dng lm gim sai s thit lp v gi tr 0.

iu chnh h s
Ta thy thi gian t ti n nh vn qu di. Ta phi tng gi tr cc h s qu trnh n nh nhanh hn na. Sa li gi tr Ki = 200 v Kp = 17 trong m-file. Cho chy trong Matlab ta s c th nh sau:

By gi c th thy thi gian t n nh nhanh hn, nhng gi tr Ki ln li lm tng qu iu chnh. By gi ta phi dng lut D gim qu iu chnh. Sa li m-file nh sau ri cho chy trong Matlab.
Kp=17; Ki=200; Kd=0.15; contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(contr*motor,1); t=0:0.001:0.1; step(sys_cl,t)

Ta s c th sau:

V th p ng vi u vo nhiu bc thang nh sau:

By gi ta c th thy qu trnh qu tt hn hn, qu iu chnh nh hn 16% thi gian qu ch khong 40ms, v khng c sai s thit lp. Tuy nhin p ng vi nhiu li n nh chm. Ta s tng Ki p ng vi nhiu n nh nhanh hn. Cho Ki = 600 trong m-file chy trong Matlab. Ta s c th sau:

By gi ta c k qu l qu trnh qu ch khong 40ms, qu eu chnh nh hn 16%, v khng c sai s thit lp. p ng vi nhiu khng b sai s thit lp. Nh vy b iu khin PID vi Kp=17, Ki=600, Kd=.15, tho mn yu cu iu khin ra.

V d v b iu khin PID

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

V d v u khin gc quay ng c
7

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

Tutorials

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example:PID Design Method for the Bus Suspension System


Adding a PID controller Plotting the closed-loop response Choosing the gains for the PID controller
From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations and schematic, please refer to the bus modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands).
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; nump=[(m1+m2) b2 k2] denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; G1=tf(nump,denp); num1=[-(m1*b2) -(m1*k2) 0 0];

den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; G2=tf(num1,den1); numf=num1; denf=nump; F=tf(numf,denf);

Adding a PID controller


Recall that the transfer function for a PID controller is:

where KP is the proportional gain, KI is the integral gain, and KD is the derivative gain. Let's assume that we will need all three of these gains in our controller. To begin, we might start with guessing a gain for each: KP=208025, KI=832100 and KD=624075. This can be implemented into MATLAB by adding the following code into your m-file:
KD=208025; KP=832100; KI=624075; contr=tf([Kd Kp Ki],[1 0]);

Now let's simulate the response of the system (the distance X1-X2) to a step disturbance on the road. From the schematic above we can find the transfer function from the road disturbance W to the output(X1-X2), and simulate:
sys_cl=F*feedback(F*G1,contr);

Plotting the closed-loop response


Now we have created the closed-loop transfer function in MATLAB that will represent the plant, the disturbance, as well as the controller. Let's see what the closed-loop step response for this system looks like before we begin the control process. Keep in mind that we are going to use a 0.1 m high step as our disturbance, to simulate this, all we need to do is to multiply sys_cl by 0.1. Add the following code your m-file:
t=0:0.05:5; step(0.1*sys_cl,t) title('closed-loop response to 0.1m high step w/ pid controller')

you should see the response (X1-X2) to a step W like this:

From the graph, the percent overshoot is 9mm, which is larger than the 5mm requirement, but the settling time is satisfied, less than 5 seconds. To choose the proper gain that yields reasonable output from the beginning, we start with choosing a pole and two zeros for PID controller. A pole of this controller must be at zero and one of the zeros has to be very close to the pole at the origin, at 1. The other zero, we will put further from the first zero, at 3, actually we can adjust the second-zero's position to get the system to fulfill the requirement. Add the following command in the m-file, so you can adjust the second-zero's location and choose the gain to have a rough idea what gain you should use for KD,KP, and KI.
z1=1; z2=3; p1=0; numc=conv([1 z1],[1 z2]); denc=[1 p1]; contr=tf(numc,denc); rlocus(contr*G1) title('root locus with PID controller') [K,p]=rlocfind(contr*G1)

you should see the closed-loop poles and zeros on the s-plane like this and you can choose the gain and dominant poles on the graph by yourself:

We will explain root locus method in more detail in the "Root Locus" page.

Choosing the gains for the PID controller


Now that we have the closed-loop transfer function, controlling the system is simply a matter of changing the KD,KP,and KI variab. From the figure above, we can see that the system has larger damping than required, but the settling time is very short. This response still doesn't satisfy the 5% overshoot requirement. As mentioned before, this can be rectified by adjusting the KD, KP and KI variab to find better response. Let's increase KP,KI,KD by a factor of 2 to see what will happen. Go back to your m-file and multiply KP,KI,KD by 2 and then rerun the program, you should get the following plot:

To compare this graph with the graph of low-gain PID controller, you can change the axis:
axis([0 5 -.01 .01])

Now we see that the percent overshoot and settling time meet the requirements of the system. The percent overshoot is about 5% of the input's amplitude and settling time is 2 seconds which is less than the 5 second requirement. For this problem, it turns out that the PID design method adequately controls the system. This can been seen by looking at the root locus plot. Such a task can be achieved by simply changing only the gains of a PID controller. Feel free to play around with all three of the parameters,KD,KP and KI, as we suggested, but you will most likely get the response to have either large percent overshoot or very long settling time.

PID Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php iu khin PID cho con lc ngc


Biu din h thng h Hm truyn ca h thng iu khin vng kn Tch hp b iu khin PID iu g xy ra vi v tr ca xe
Hm truyn ca c cu chp hnh nh sau:

y,

Mc tiu thit k (vi iu kin lc u con lc nhn c xung lc1N t xe) l: Thi gian n nh nh hn 5 giy. Con lc di chuyn khng qu 0,05 rad so vi phng thng ng. bit cch xy dng m hnh bi ton ny nh th no tham kho phn m hnh con lc ngc

Biu din h thng h


iu u tin cn lm khi mun thit k b PID l tm hm truyn ca h thng v xem xt n. Hm truyn sau khi bin i Laplace vi u ra Phi (gc quay ca con lc) c th c biu din trong Matlab vi t s v mu s l hai vector. To mt m-file c cc lnh sau thit lp hm truyn:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q];

q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q

-(M+m)*m*g*l/q 1

pend=tf(num,den);

Hm truyn ca h thng iu khin vng kn


M hnh ca h thng ny c th hi khc so vi nhng m hnh truyn thng m ta quen bit. Do vy y t bi ton l iu khin con lc v tr thng bng vi u vo nhiu l mt xung lc ban u, tn hiu reference l 0. Xung lc ban u y chnh l mt xung nhiu. S h thng nh vy s c dng sau.

d dng chuyn i thnh hm truyn trong Matlab ta bin i tng ng thnh s sau:

Tch hp b iu khin PID


Hm truyn trn c thit lp trong Matlab bng cc lnh sau:
Kd = 1; Kp = 1; Ki = 1; contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(pend,contr);

cho tin vi hm truyn ny ta ly lun c 3 lut iu khin P, I v D (iu ny cng khng bt buc, bn c vn c th bt u tng bc mt vi cc b iu khin PI, PD tu theo mnh).
contr=tf([Kp Ki],[1 0]); % PI control contr=tf([Kd Kp],1); % PD control

By gi l lc ta bt u iu khin h thng hin ti. Trc ht ta xem p ng xung ca h thng hin ti ra sao. Thm cc cu lnh sau vo M-Enter the following code to the end of your m-file:
t=0:0.01:5; impulse(sys_cl,t) axis([0 1.5 0 40])

Ta c p ng dch chuyn ca h thng nh sau:

p ng ny cha n nh. Trc ht ta tng h s Kp xem tc ng nh th no, cho Kp=100, v t li h to bng lnh axis([0, 2.5, -0.2, 0.2]), ta s c th sau:

Thi gian qu 2 giy y l chp nhn c. V sai s xc lp v 0 nn ta khng cn iu khin Ki thm na. Bn c th thy iu bng cch th thay i Ki v chy li. D nhn thy l qu iu chnh l qu ln v ta cn gi quyt vn ny. gii quyt ta tng gi tr Kd. Vi Kd = 20, ta c kt qu nh mong mun. Hy xem p ng vi gi tr Kd = 20:

Nh ta thy, qu iu chnh gim xung nh hn 0,05 rad nh th ngha l con lc dch chuyn xung quanh v tr thng ng khng qu 0,05 rad. Mc tiu iu khin t, khng cn thit phi iu chnh tip na.

iu g xy ra vi v tr ca xe?
Ngay t u, chng ta a ra m hnh ca h thng. Nhng m hnh cha thc s hon thin. Chng ta b qua dch chuyn ca xe v ta ang quan tm ti gc quay ca con lc. S rt hay nu ta xem xt dch chuyn ca xe ra sao khi ta ang iu khin gc quay ca con lc. c th, trc ht ta xem s khi thc t nh sau:

Bin i li 1 cht ta c s khi nh sau :

Vng kn trn l h thng ta va thit k vi b iu khin PID cho gc quay con lc. Hm truyn vi u ra l dch chuyn ca xe by gi nh sau:

By gi ta c th xy dng hm truyn cho ton b h thng, hy xem p ng ca n ra sao. Trc ht ta tm hm truyn cho dich chuyn ca xe. c uc iu ny ta phi s dng bin i Laplace cho phng trnh h thng ri tm mi lin h gia X(s) v U(s). Ta c phng trnh sau:

y,

bit thm v bin i Laplace tham kho phn m hnh con lc ngc. Lc ny li xut hin pole/zero hm truyn m lc trc c gin c trong hm truyn ca Phi. Do den1 = den2. By gi to 1 m-file vi cc dng lnh sau:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q 0];

q = (M+m)*(i+m*l^2)-(m*l)^2; num1 = [m*l/q 0 0]; den1 = [1 b*(i+m*l^2)/q G1=tf(num1,den1); num2 = [(i+m*l^2)/q den2 = den1; G2=tf(num2,den2); 0

-(M+m)*m*g*l/q -m*g*l/q];

kd = 20; kp = 100; ki = 1; contr=tf([kd kp ki],[1 0]); xpos=feedback(1,G1*contr)*G2; t=0:0.01:5; impulse(xpos,t)

Nh ta thy, xe chuyn ng theo chiu m vi vn tc l hng s ngha l theo thi gian v tr xe s tin ra v cng. Nh vy mc d b iu khin PID n nh c gc quay ca con lc nhng thit k khng kh thi ng dng thc t.

V d v b iu khin PID

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Ball & Beam

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: PID Design method for the Pitch Controller


Proportional control Proportional-Derivative control Proportional-Integral-Derivative control
In the Pitch Controller Modeling page, the transfer function was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the pitch angle (theta). The design requirements are Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2% To see the original problem setup, please refer to the Pitch Controller Modeling page. Recall from the PID Tutorial page, the transfer function of a PID controller is:

We will implement combinations of proportional (Kp), integral (Ki), and derivative (Kd) controllers in an unity feedback system shown below to study the system output.

Let's first take a look at a proportional control.,

Proportional control
The first thing in solving this problem using PID control is to find a closed-loop transfer function with a proportional control (Kp). A closed-loop transfer function can be obtained either by hand or using the MATLAB function called feedback. By hand, you should obtain the closed-loop transfer function as:
1

Note: MATLAB cannot manipulate symbolic variab. To use the function feedback, enter the following commands to an m-file and run it in the MATLAB command window.
Kp=[1]; num=[1.151 0.1774]; den=[1 0.739 0.921 0]; pitch=tf(num,den); contr=tf(Kp,1); sys_cl=feedback(contr*pitch,1);

For now, let the proportional gain (Kp) equal 2 and observe the system behavior. Enter the following commands into a new m-file and run it in the MATLAB command window. You should obtain the step response similar to the one shown below:
de=0.2; Kp=2; sys_cl=feedback(Kp*pitch,1); t=0:0.01:30; step(de*sys_cl,t)

As you see, both the overshoot and the settling time need some improvement.

PD control
Recall from the PID Tutorial page, the derivative controller will reduce both the overshoot and the settling time. Let's try a PD controller. The closed-loop transfer function of the system with a PD controller is:

Using the commands shown below and with several trial-and-error runs, a proportional gain (Kp) of 9 and a derivative gain (Kd) of 4 provided the reasonable response. To confirm this, change your m-file to the following and run it in the MATLAB command window. You should obtain the step response similar to the one shown below:
de=0.2; Kp=9; Kd=4; contr=tf([Kd Kp],1); sys_cl=feedback(contr*pitch,1); t=0:0.01:10; step(de*sys_cl,t)

This step response shows the rise time of less than 2 seconds, the overshoot of less than 10%, the settling time of less than 10 seconds, and the steady-state error of less than 2%. All design requirements are satisfied.

PID Control
Even though all design requirements were satisfied with the PD controller, the integral controller (Ki) can be added to reduce the sharp peak and obtain a smoother response. After several trial-and-error runs, the proportional gain (Kp) of 2, the integral gain (Ki) of 4, and the derivative gain (Kd) of 3 provided a smoother step response that still satisfies all design requirements. To confirm this, enter the following commands to an m-file and run it in the command window. You should obtain the step response shown below:
de=0.2; Kp=2; Kd=3; Ki=4; num=[1.151 0.1774]; den=[1 0.739 0.921 0]; pitch=tf(num,den); contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(contr*pitch,1); t=0:0.01:10; step(de*sys_cl,t)

Comments
1. To find appropriate gains (Kp, Kd, and Ki), you can use the table shown in PID Tutorial page; however, please keep in your mind that changing one gain might change the effect of the other two. As a result, you may also need to change the other two gains. 2. Our system with a PI controller does not provide the desired response; thus, a PI Control section was omitted from this page. You may confirm this by using the m-file shown in PID Control section, and set Kd to zero.

PID Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Control | Ball and Beam

Pitch Control Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php cho bi ton Bng &Thanh s dng b iu khin PID


Biu din h thng vng kn iu khin T l iu khin T l-Vi phn
Hm truyn h thng h thit lp trong phn m hnh nh sau:

Mc tiu thit k b iu khin: Thi gian qu nh hn 3 giy qu iu chnh nh hn 5% bit cch thit lp m hnh ton hc trn, tham kho trang m hnh bng v thanh .

Biu din h thng iu khin vng kn


S khi h thng iu khin vng kn nh sau:

Trc tin, ta xem p ng ca h thng ra sao vi b iu khin P. Sau s thm vo cc lut D hoc I nu cn thit. Nhc li rng hm truyn ca b iu khin PID c dng:

iu khin t l
Hm truyn ca h thng iu khin t l vng kn vi Kp = 100 c thit lp trong Matlab bng cch to ra mt m-file c lnh sau:
1

m R g L d J

= = = = = =

0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; ball=tf(num,den); kp = 1; sys_cl=feedback(kp*ball,1);

xem p ng ca h thng vi u vo l tn hiu bc thang 0.25 m. Thm lnh sau vo cui m-file v chy:
step(0.25*sys_cl)

Ta s c th sau:

Nh ta thy Kp khng lm h thng n nh. Hy th mt s gi tr khc c Kp, bn s vn thy h thng khng th n nh.

iu khin T l - Vi phn
By gi ta thm lut D vo b iu khin. To mt m-file c cc lnh sau ri chy.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m));

num = [-K]; den = [1 0 0]; ball=tf(num,den); kp = 10; kd = 10; contr=tf([kd kp],1); sys_cl=feedback(contr*ball,1); t=0:0.01:5; step(0.25*sys_cl)

th sau khi chy chng trnh s c dng sau:

By gi h thng n nh song qu iu chnh li rt ln, v thi gian qu cng cn iu chnh nhanh hn 1 cht. phn PID tutorial trong bng tng kt v cc lut iu khin P, I, v D ta bit rng nu tng Kd ln th s ci thin c qu iu chnh v thi gian qu mt cht. Do , thay i m-file cho Kd = 20 v chy li ta s c th sau:

qu iu chnh t nhng cn gim thi gian qu mt cht. Ta tip tc tng Kd ln mt cht. Tng Kd cng c tc dng lm gim qu iu chnh do Kp to ra. Sau mt s ln iu chnh vi Kp = 15 v Kd =40 ta c p ng:

Nh ta thy ch cn s dng iu khin P v D l ta gii quyt yu cu t ra (thi gian qu thm ch cn b hn 2% thi gian yu cu). Nn nh rng vi bt k vn iu khin no ta cng phi c ln hn 1 phng n th mi m bo. Xem cc lin kt bn di c thm nhng phng php iu khin khc cho bi ton bng v thanh .

PID Examples

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension |

iu khin con lc ngc | Pitch Controller | Bi ton bng v thanh

V d v bi ton bng v thanh Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Qu tch nghim (Root Locus)


Cc ca h kn th qu tch nghim ca mt hm truyn Chn gi tr K t qu tch nghim p ng mch kn
Cc lnh MATLAB quan trng trong phn ny: feedback, rlocfind, rlocus, sgrid, step

Cc ca h kn
Qu tch nghim (mch h) ca mt hm truyn H(s) l th th hin v tr ca tt c cc cc ca h kn vi h s t l K v phn hi n v:

Hm truyn mch kn vi phn hi n v bng:

v v vy, cc ca h kn l cc gi tr, sao cho 1 + K H(s) = 0. Nu vit H(s) = b(s)/a(s), th phng trnh c dng:

Gi n l bc ca a thc a(s) v m l bc ca a thc b(s) [bc ca mt a thc l s m cao nht c trong a thc ]. Chng ta tt c cc gi tr dng ca K. Ti gii hn K -> 0, cc ca h kn ng vi a(s) = 0 hoc cc ca H(s). Ti gii hn K -> v cng, cc ca h kn ng vi b(s) = 0 hoc im 0 ca H(s). D K ng vi im no i na th h kn cng phi lun lun c n cc, trong n chnh l s cc ca H(s). ng qu tch phi c n nhnh, mi nhnh xut pht t cc ca H(s) v chy n im 0 ca H(s). Nu H(s) c s cc nhiu hn s im 0 (iu thng xy ra), m < n th ta ni rng H(s) c
1

im 0 v cng. Trong trng hp ny, gii hn ca H(s) khi s -> v cng bng 0. S im 0 v cng l hiu n-m, ngha l hiu gia s cc v s im 0 v l s nhnh ca qu tch nghim tin ti v cng (tim cn). V qu tch nghim biu din mi v tr c th ca cc cc ca h kn, nn t qu tch nghim chng ta c th chn h s truyn t cc tnh nng t ra cho h thng. Nu c bt k mt cc no nm trong na mt phng pha phi th h kn s khng n nh. Cc cng nm gn trc o th nh hng cng nhiu n p ng h kn. V vy, d h c ti 3 hay 4 cc th n cng ch hot ng nh h bc hai, thm ch bc 1, tu thuc vo v tr ca cc cc ch o.

V qu tch nghim ca hm truyn


Gi s c h h vi hm truyn sau:

Chng ta cn thit k mt b iu khin c phn hi bng phng php qu tch nghim theo cc ch tiu k thut sau: qu iu khin 5% v thi gian n nh 1 giy. To mt m-file vi tn rl.m. Nhp hm truyn v lnh v qu tch nghim nh sau:
num=[1 7]; den=conv(conv([1 0],[1 5]),conv([1 15],[1 20])); sys=tf(num,den); rlocus(sys) axis([-22 3 -15 15])

Chn gi tr ca K t qu tch nghim


th trn biu din v tr ca tt c cc cc ca b iu khin thun tu t l. Tt nhin, khng phi tt c cc cc u tho mn cc ch tiu thit k. xc nh xem cc no c th chp nhn c, chng ta dng lnh sgrid(Zeta,Wn) v cc ng c h s gim chn v tn s dao ng ring bng hng s. Hai tham s ca lnh chnh l h s gim chn (Zeta) v tn s dao ng ring (Wn)

[chng c th l cc vector nu chng ta mun nhn thy khong cc gi tr c th chp nhn c]. Trong bi ton ny, chng ta cn lng qu iu khin di 5% (ngha l h s gim chn Zeta > 0.7) v thi gian xc lp b hn 1 giy (ngha l tn s dao ng ring Wn > 1.8). Nhp thm chng vo cui m-file trc :
zeta=0.7; Wn=1.8; sgrid(zeta, Wn)

Trn th, 2 ng nt chm nghing gn 45 th hin v tr ca cc cc ng vi gi tr Zeta = 0.7. Nm gia 2 ng ny l cc cc ng vi Zeta > 0.7 v pha ngoi l cc cc vi Zeta < 0.7. Na vng trn th hin cc cc vi tn s dao ng ring Wn = 1.8. Cc cc nm pha trong ng vi Wn < 1.8, pha ngoi ng vi Wn > 1.8. Tr li bi ton, lng qu iu khin nh hn 5%, th cc cc phi nm gia 2 ng nt chm, cn mun c thi gian xc lp ngn hn 1 giy th cc cc phi nm ngoi na vng trn. Nh vy, ch c on qu tch nghim nm ngoi na vng trn v gia 2 ng nt chm l tho mn yu cu. Tt c cc cc trn on ny u nm na mt phng bn tri, nn h kn thit k ra s n nh. T th trn, ta thy ch c mt phn ca qu tch nghim l nm trong vng mong mun. V vy, chng ta ch cn 1 b iu khin t l a cc cc v vng ny. Chng ta dng lnh rlocfind tm cc cc mong mun trn qu tch:
[k,poles] = rlocfind(sys)

Dng chut kch vo im m chng ta nh dng lm cc ca h kn. Bn c th chn cc im nh trong hnh di tho mn cc ch tiu thit k.

Ch rng, v qu tch nghim c trn 1 nhnh nn khi chn mt cc, bn c th tm ra v tr ca cc cc khc. Nh rng chng cng nh hng n p ng ca h thng. T th trn, chng ta c th thy tt c cc cc chn (c du +) l tho mn yu cu. By gi chng ta tip tc s dng gi tr K chn cho b iu khin t l.

Phn ng h kn
tm p ng ca h vi tn hiu vo bc thang, chng ta cn bit hm truyn h kn. Ta c th thc hin iu bng phng php rt gn s khi, hoc yu cu MATLAB thc hin:
sys_cl= feedback(k*sys,1)

Hai tham s ca hm feedback l t s v mu s ca hm truyn h h. Bn cn thm vo h s truyn K chn, v gi thit dng phn hi n v. Nu bn gp trng hp phn hi khc n v th xem tr gip (help) vi hm feedback bit cch tm hm truyn h kn c h s phn hi khc 1. kim tra phn ng ca h kn, dng lnh:
step(sys_cl)

ng nh mong mun, lng qu iu khin nh hn 5% v thi gian xc lp di 1 giy.

Root Locus Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php iu khin xe bng phng php qu tch nghim


B iu khin t l B iu khin Lag
Hm truyn ca h h c dng:

y m=1000 b=50 U(s)=10 Y(s)=vn tc u ra Mc tiu thit k b iu khin: Thi gian qu < 5 sec qu iu chnh < 10% Sai s xc lp < 2% bit cch thit lp phng trnh h thng, tham kho M hnh iu khin xe .

B iu khin t l
Nh li kin thc t phn Root-Locus Tutorial , qu tch nghim cho chng ta bit tt c cc v tr ca cc cc ca h kn khi h s t l thay i t 0 n v cng. Do , ta ch quan tm ti b iu khin t l gii quyt vn ny. y ta c hm truyn ca h kn:

T phm Root-Locus Tutorial, ta cng bit lnh sgrid c dng tm vng mong mun trn qu tch nghim. dng lnh sgrid, hai h s cn xc nh trc l h s gim chn zeta v tn s dao ng ring Wn cn c xc nh trc. Hai phng trnh sau c dng xc nh hai h s trn:

y Wn=Tn s dao ng ring zeta=H s gim chn Tr=Thi gian qu Mp= qu iu chnh ln nht Mt trong nhng mc tiu iu khin ca ta l thi gian qu nh hn 5 giy. T phng trnh u tin ta thy tn s dao ng ring cn nh hn 0,36. T phng trnh th hai ta thy h s gim chn phi ln hn 0,6 th qu iu chnh s nh hn 10%. By gi chng ta s v qu o nghim v dng lnh sgrid tm cc vng mong mun. To 1 m-file c cc lnh sau.
m=1000; b=50; u=10; num=[1]; den=[m b]; cruise=tf(num,den); rlocus(cruise) axis([-0.6 0 -0.6 0.6]); sgrid(0.6, 0.36) [Kp, poles]=rlocfind(cruise) sys_cl=feedback(Kp*cruise,1); t=0:0.1:20; step(u*sys_cl,t) axis ([0 20 0 10])

Cho chy m-file ta c qu tch nghim nh sau.

Hai ng nt chm th hin v tr cc cc ng vi zeta=0,6, nm gia 2 ng ny l cc cc ng vi Zeta > 0,6 v pha ngoi l cc cc vi Zeta < 0,6. Na vng elip th hin cc cc vi tn s dao ng ring Wn = 0,36. Cc cc nm pha trong ng vi Wn < 0,36, pha ngoi ng vi Wn > 0,36. Nu chy chng trnh trong ca s lnh ca Matlab ta s thy c du nhc ta chn 1 im trn qu o nghim. Nh vy, ta cn chn im nm gia hai ng nt chm (zeta>0.6) v nm ngoi na vng elip (Wn>0.36), hy kch vo im nm ngoi elip (to trn trc thc khong 0,4). Ta s c g tr Kp v cc im cc trong ca s lnh ca Matlab. ng thi ta cng thy p ng vi u vo tn hiu bc thang nh sau.

Vi gi tr Kp ny (do ta va chn), thi gian qu v qu iu chnh t yu cu; tuy nhin sai s xc lp vn cn ln hn 10%.

B iu khin Lag

gim sai s xc lp, b iu khin Lag c t vo h thng. Hm truyn ca b iu khin Lag l:

Hm truyn ca h h (cha c Kp) by gi l:

Hm truyn ca h kn vi b iu khin t l by gi l:

Nu ta tra mc "Lag or Phase-Lag Compensator using Root-Locus" trong phn Lead and Lag Compensator im khng v im cc phi c t gn nhau. Cng t , sai s xc lp s gim xung 1 lng khong bng Zo/Po. V hai l do trn, ta chn Zo = -0.3 v Po = -0.03. To mt m-file, c cc lnh sau.
m = 1000; b = 50; u = 10; Zo=0.3; Po=0.03; num=[1]; den=[m b]; cruise=tf(num,den); contr=tf([1 Zo],[1 Po]); rlocus(contr*cruise); axis([-0.6 0 -0.4 0.4]) sgrid(0.6,0.36); [Kp, poles]=rlocfind(contr*cruise); sys_cl=feedback(Kp*contr*cruise,1); t=0:0.1:20; step(u*sys_cl,t) axis ([0 20 0 12])

Chy m-file s cho ta qu tch nghim nh sau:

Chn 1 im nh nh lc trc (trn trc thc vi to trc thc khong -0.4). Ta s c p ng sau.

Nh ta thy, sai s xc lp nh gn bng 0. Ta cng thy 1 lng qu iu chnh nh do c im khng trong b iu khin Lag. By gi mc tiu iu khin hon thnh v ta khng cn phi iu chnh thm g na.

Root Locus Examples

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Bi ton bng v thanh

Cruise Control Examples

Modeling | PID | Qu tch nghim | Frequency Response | State Space | Digital Control | Simulink
5

Tutorials

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Phng php qu tch nghim cho iu khin tc ng c 1 chiu


V qu o nghim ca h h Tm cc h s bng lnh rlocfind t thm b iu khin Lag p ng ca h thng iu khin kn
Ta c phng trnh ng lc hc v hm truyn ca h h nh sau:

v s khi ca h thng nh sau:

bit cch thit lp h m hnh trn, tham kho phn M hnh ng c in 1 chiu. Vi u vo tn hiu bc thang 1 rad/sec, mc tiu thit k l: Thi gian t n nh nh hn 2 giy qu iu chnh nh hn 5% Sai s xc lp nh hn 1% Ta s thit k 1 b iu khin s dung phng php qu o nghim. To 1 m-file c cc lnh sau to hm truyn trong Matlab (tham kho phn m hnh).
J=0.01; b=0.1; K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; motor=tf(num,den);

V qu tch nghim ca h h
Mc ch chnh ca vic tm qu tch nghim l xc nh p ng ca h kn t qu tch nghim ca h h. Sau bng cch thm vo khu chp hnh cc im cc hoc im khng sa i p ng ca h kn theo yu cu. Trc tin ta hy xem xt qu tch nghim ca khu chp hnh. Thm cc lnh sau vo m-file.
rlocus(motor) sgrid(.8,0) sigrid(2.3) title('Root Locus without a controller')

Hm sigrid l mt hm do ngi dng to ra. Ta cn copy file sigrid.m vo cng th mc vi m-file ca ta c th gi d dng. bit hm ny c chc nng g, tham kho phn functions. Hai i s ca lnh sgrid l h s v gim chn zeta (0,8 tng ng vi qu iu chnh l 5%), v v tn s dao ng ring Wn (= 0 ngha l khng c iu kin v thi gian qu ). i s duy nht ca lnh sigrid l sigma (4.6/2 giy = 2.3). Sau khi c file sigrid.m trong th mc hin hnh, chy m-file trn ta s c qy tch nghim nh sau:

Tm h s bng lnh rlocfind


Nh rng, ta cn thi gian qu v qu iu chnh cng nh cng tt. gim chm ln tng ng vi vic nhng im trn qu tch nghim s gn trc thc hn. Vi p ng cng nhanh th cc im trn qu tch nghim cng ra xa v pha bn tri trc o. tm h s tng ng vi 1 im trn qu tch nghim, ta dng lnh rlocfind. Ta s tm c h s v xem ngay c vi h s y th p ng ca h thng by gi nh th no. lm iu ny, thm cc cu lnh sau vo m-file ri chy.
[k,poles] = rlocfind(motor) sys_cl=feedback(k*motor,1); t=0:0.01:3; step(sys_cl,t) title('Step response with gain')

Tr v ca s plot v chn 1 im khong gia trc thc v iu kin sigma, gn im -6+2.5i. Matlab se tnh cho ta kt qu sau.
selected_point = -5.9596 + 2.0513i k = 10.0934

poles = -6.0000 + 2.0511i -6.0000 - 2.0511i

Ta cng s c p ng sau:

Nh ta thy, y qu iu chnh l khng c v thi gian qu khong 1 giy, nh vy l tho mn yu cu. Vn duy nht l sai s xc lp qu ln (khong 50%). Nu ta tng gi tr ca h s gim sai s xc lp th qu iu chnh s rt ln (hy th vi ln). Chng ta cn t thm vo b iu khin Lag gim sai s xc lp.

t thm b iu khin Lag


Qu tch nghim ta gp y l kh n gin. gim chm v thi gian qu t c d dng vi b iu khin t l. Ch c sai s xc lp l cha t c. Mt c cu b Lag s lm gim sai s xc lp. Khi thc hin nh vy, chng ta mt phn no s lm tng thi gian qu . Ban u hy th b iu khin Lag sau:

Cng vic ny thc hin trong m-file nh sau:


J=0.01; b=0.1; 3

K=0.01; R=1; L=0.5; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; Zo=1; Po=0.01; motor=tf(num,den); contr=tf([1 Zo],[1 Po]); rlocus(contr*motor) sgrid(.8,0) sigrid(2.3) title('Root Locus with a lag controller')

Ta s c qu tch nghim ca h h gn ging nh ban u nh sau:

Biu din p ng ca h kn
By gi ta s tm p ng ca h kn. Thm vo m-file cc lnh sau:
[k,poles]=rlocfind(contr*motor) sys_cl=feedback(k*contr*motor,1); t=0:0.01:3; step(sys_cl,t) title('Step response with a lag controller')

Chy m-file trn trong Matlab. Khi c yu cu chn 1 im trn qu tch nghim, hy chn im gn ng gim chn (ng nt t cho). Ta s c p ng sau:

Gi tr h s t l s gn bng 20. Nh ta thy p ng trn cng cha c tt. V ta cng nhn thy rng vi vic chn 1 im gn ng iu kin gim chn nh vy, qu iu chnh vn cha th bng 5%. l do nh hng ca b iu khin Lag (gi tr im cc ca n nh). iu ny c ngha l ta c th vt qua ng iu kin gim chn chn h s t l ln hn m khng phi lo ti qu iu chnh. Hy chy li m-file, chn im va vt ra khi ng nt t. Th mt vi ln n khi ra c p ng mong mun. N s c dng nh sau (lc h s t l c gi tr xp x 50):

Sai s xc lp by gi n hn 1%, thi gian qu cng nh qu iu chnh cng t yu cu t ra. V nh ta thy, phng php thit k theo qu o nghim cn nhiu n k nng th sai. iu ny kh hay v ta thc hin v qu tch nghim, chn h s, v v p ng trong cng 1 bc. Nu ta gp kh khn trong vic chn h s, ta c th thay i b iu khin Lag khc, hoc c th thm vo c b iu khin Lead.

Root Locus Examples

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Speed Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Phng php qu tch nghim cho iu khin gc quay ng c 1 chiu


V qu o nghim h h Ti gin ha m hnh iu khin Tch phn iu khin T l-Tch phn iu khin T l-Tch phn-Vi phn Tm h s s dng lnh rlocfind v v p ng ca h iu khi kn
T bi ton chnh, phng trnh ng lc hc v hm truyn ca h thng nh sau:

v s khi ca h thng nh sau:

bit cch thit lp h m hnh trn, tham kho phn M hnh ng c in 1 chiu. Vi u vo tn hiu bc thang 1 rad, mc tiu iu khin l: Thi gian qu nh hn 0.04 giy qu iu chnh nh hn 16% Khng c sai s xc lp vi u vo reference Khng c sai s xc lp do nhiu Ta s thit k b iu khin bng phng php qu tch nghim. To 1 m-file c cc lnh sau lp hm truyn (thao kho phn m hnh).
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; 1

num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; motor=tf(num,den);

V qu tch nghim ca h h
Mc ch chnh ca vic tm qu tch nghim l xc nh p ng ca h kn t qu tch nghim ca h h. Sau bng cch thm vo khu chp hnh cc im cc hoc im khng sa i p ng ca h kn theo yu cu. Trc tin ta hy xem xt qu tch nghim ca khu chp hnh. Thm cc lnh sau vo m-file.
rlocus(motor) sgrid(.5,0) sigrid(100)

Hm sigrid l mt hm do ngi dng to ra. Ta cn copy file sigrid.m vo cng th mc vi m-file ca ta c th gi d dng. bit hm ny c chc nng g, tham kho phn functions. Hai i s ca lnh sgrid l h s v gim chn zeta (0,5 tng ng vi qu iu chnh l 16%), v v tn s dao ng ring Wn (= 0 ngha l khng c iu kin v thi gian qu ). i s duy nht ca lnh sigrid l sigma (4/0,04 giy = 100). Sau khi c file sigrid.m trong th mc hin hnh, chy m-file trn ta s c qu tch nghim nh sau:

Nhn vo t l xch ca cc trc ta thy c 1 im cc rt xa v pha tri (xa hn -1x10^6). im cc ny khng nh hng ti qu trnh ng lc hc ca h kn tr khi ta s dng cc h s rt ln, h thng tr nn mt n nh. Chng ta b qua im cc ny v thc hin ti gin ho m hnh h thng.

Ti gin ha m hnh h thng


Nu hm truyn c 1 cc (hoc hn) xa v pha bn tri so vi cc cc chnh, th cc ny s khng nh hng lm ti h thng vi cc h s nh (vi h s ln cc cc ny s y qu tch nghim sang pha phi mt phng to , dn n mt n nh). C mt cch c th b qua cc ny l gi h s
2

DC ca hm truyn l hng bng cch xp x nh sau:

Ta hy xem cc im cc ca hm truyn l g. Gi lnh sau trong Matlab:


roots(den)

Ta c kt qu sau:
ans = 1.0e+06 * 0 -1.4545 -0.0001

Ta mun b qua im cc -1.45e6. Thc hin iu ny bng on lnh sau:


J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; poles=roots(den); den2=deconv(den,[1/max(abs(poles)) 1]); motor=tf(num,den2);

By gi ta c th kim tra li bng lnh


roots(den2)

trong ca s lnh ca Matlab. By gi ta v qu o nghim ca h c ti gin. Thm nhng lnh sau vo m-file trn.
rlocus(motor) sgrid(.5,0) sigrid(100)

Ta c qu tch nghim nh sau v c th thy ch vi cc h s nh l h kn s n nh.

T qu tch nghim trn ta thy, cc cc ca h kn khng bao gi nhanh tho mn tt iu kin v thi gian n nh (ngha l chng khng bao gi tin ti ng thng ng sigma=100). V cng cn nh rng ta phi thm vo lut iu khin tch phn cho b iu khin (khng ch cho h thng) kh sai s xc lp do nhiu gy nn.

iu khin tch phn


Ta hy th thm iu khin tch phn kh sai s xc lp do nhiu. Sa li m-file nh sau:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6; num=K; den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; poles=roots(den); den2=deconv(den,[1/max(abs(poles)) 1]); motor=tf(num,den2); contr=tf(1,[1 0]); rlocus(contr*motor) sgrid(.5,0) sigrid(100)

Nh trn l ta nhn h h vi 1/s. Chy m-file v ta c qu tch nghim sau.

T qu tch trn ta thy h kn s khng th n nh c, do vy y ta phi s dng b iu khin khc.

iu khin T l-Tch phn


By gi ta sa b iu khin tch phn thnh b i khin PI. Vic s dng b iu khin PI thay cho I s thm 1 im khng vo h h. Ta cho im khng ny l s=-20. im khng ny s nm gia cc im cc ca h h v trong trng hp ny h kn s tr nn n nh. Thay i cu lnh thit lp b iu khin trong m-file nh sau:
contr=tf([1 20],[1 0]);

Chy m-file mi ta s c qu tch nghim sau.

Ch : Ta c th khng c c hnh v nh trn trong Matlab, iu ny ph thuc vo bn s dng version no ca lnh rlocus. Nu khng c qu o nh trn, hoc khng

c nh , bn s phi xc nh cc h s m bn mun Matlab biu din. Trong trng hp ny lnh sau cho ta biu din tt hn.
gain = 0:0.1:20; rlocus(contr*motor,gain)

By gi ta n nh c h thng v khng c sai s xc lp do nhiu, nhng p ng ca h thng vn cha nhanh.

iu khin T l-Tch phn-Vi phn


ko qu o nghim dch v pha tri cho p ng ca h thng nhanh hn, ta cn thm 1 im khng na vo h h, kt qu l ta c mt b iu khin PID. Sau mt s ln th, ta c th chn hai im khng l s=-60 v s=-70. Thay i li on m lnh xc nh b iu khin trong m-file.
numc=conv([1 60],[1 70]); denc=[1 0]; contr=tf(numc,denc);

Chy m-file v ta c qu tch nghim sau.

By gi ta c th thy c cc im cc ca h kn nm trong vng tho mn c v thi gian qu v qu iu chnh. im cc th ba ca h h chy t cc s=-59.2 ti im khng s=-60. im cc ny ca h kn rt st im khng (im ny vn tn ti hm truyn ca h kn). Do vy, ta c th b qua nh hng ca ca im cc ny. Tuy nhin, im khng ca h h vn tn ti trn h kn, n s nh hng n p ng ca h thng lm cho h thng p ng chm i v tng qu iu chnh. V th, ta phi ch khi chn v tr ca im cc trn qu tch nghim.

Tm h s bng lnh rlocfind


Mc tiu ca chng ta l gim thi gian qu v qu iu chnh cng nh cng tt, c bit l gim nh hng ca im khng mi. gim chn ln ng vi cc im trn qu tch nghim nm gn trc thc. Kh nng p ng nhanh tng ng vi cc im nm xa v pha tri trc o. tm h s tng ng vi 1 im trn qu o nghim ta dng lnh rlocfind. Chng ta cng v p ng ca h

thng vi h s lun. Thc hin iu ny bng cch lp trnh cho m-file nh di y v chy chng trnh.
[k,poles] = rlocfind(contr*motor) sys_cl=feedback(k*contr*motor,1); t=0:0.001:.1; step(sys_cl,t)

Chn trn th im nm v pha bn tri ca ng cong v nm gn trc thc (du + nh hnh bn di). im ny s cho ta p ng nhanh v qu iu chnh b nht. V tr cc im cc ny cho ta qu iu chnh nh nht, v lu l im khng th li lm tng qu iu chnh.

Sau khi chn im xong, ta s c kt qu trong ca s lnh Matlab nh sau.


selected_point = -1.3943e+02+ k = 0.1309 poles = 1.0e+06 * -1.4542 -0.0001 + 0.0000i -0.0001 - 0.0000i -0.0001 1.8502e+01i

Lu l c th kt qu do bn chn s khng chnh xc nh trn, nhng cng phi xp x. V ta cng c p ng ca h thng vi cc h s nh sau:

Nh ta thy, qu iu chnh khong 15%, thi gian qu xp x 0,04 giy, khng c sai s thit lp. By gi ta hy xem nh hng ca nhiu nh th no, lp hm truyn ca nhiu v v p ng bng cc lnh sau.
dist_cl=feedback(motor,k*contr); step(dist_cl,t)

Chy m-file, chn li im trn qu tch nghim nh khi ny ta c p ng vi nhiu nh sau.

Nh ta thy sai s xc lp do nhiu gn bng 0, thc t l khong 0,02 (hay 2%) sau 0,04 giy. Do , ch tiu thit k c tho mn. Trong v d ny, ta c quyn la chn v tr ca im khng . Do cn 1 s ln th sai, iu ny gip ta hiu r hn v cch v qu tch nghim, cng c Matlab gip chng ta xc minh v chn lc v tr theo t c kt qu mong mun.

Root Locus Examples

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Bi ton bng v thanh

Motor Position Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Root Locus Design Method for the Bus Suspension System
Plotting the root locus Adding a notch filter Finding the gain from the root locus Plotting closed-loop response
From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations and schematic, please refer to the bus modeling page. If you are interested in running an animation of this example based on the control techniques used in the root locus tutorial please go to the bus suspension animation page after completing this tutorial. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands).
m1=2500; m2=320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020;

nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; G1=tf(nump,denp); num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; G2=tf(num1,den1); numf=num1; denf=nump; F=tf(numf,denf);

We are now ready to design a controller using the root locus design method. First let's see what the open loop poles of the system are:
R=roots(denp)

MATLAB should return:


R = -23.9758 +35.1869i -23.9758 -35.1869i -0.1098 + 5.2504i -0.1098 - 5.2504i

Therefore the dominant poles are the roots -0.1098+/-5.2504i, which are close to the imaginary axis with a small damping ratio.

Plotting the root locus


The main idea of root locus design is to estimate the closed-loop response from the open-loop root locus plot. By adding zeros and/or poles to the original system (adding a compensator), the root locus and thus the closed-loop response will be modified. Let's first view the root locus for the plant. In your m-file, add the following command and then run the file, you should get the root locus plot below:
rlocus(G1) z=-log(0.05)/sqrt(pi^2+(log(0.05)^2)) sgrid(z,0)

Note from the specification, we required the overshoot, %OS, to be less than 5% and damping ratio, zeta, can be

find from approximation damping ratio equation, z = -log(%OS/100)/sqrt(pi^2+[log(%OS/100)^2]). The command sgrid is used to overlay the desired percent overshoot line on the close-up root locus; you can find more information from commands list. From the plot above, we see that there are two pair of poles and zeros that are very close together. These poles and zeros are almost on the imaginary axis, they might make the bus system marginally stable, which might cause a problem. We have to make all of the poles and zeros move into the left-half plane as far as possible to avoid an unstable system. We have to put two zeros very close to the two poles on the imaginary axis of uncompensated system for pole-and-zero cancellation. Moreover, we will put another two poles further to the left on the real axis to get fast response.

Adding a notch filter


We will probably need two zeros near the two poles on the complex axis to draw the root locus, leading those poles to the compensator zeros instead of to the plant zeros on the imaginary axis. We'll also need two poles placed far to the left to pull the locus to the left. It seems that a notch filter (2-lead controller) will probably do the job. Let's try putting the poles at 30 and 60 and the zeros at 3+/-3.5i. In your m-file add the following lines of code:
z1=3+3.5i; z2=3-3.5i; p1=30; p2=60; numc=conv([1 z1],[1 z2]); denc=conv([1 p1],[1 p2]); contr=tf(numc,denc); rlocus(contr*G1)

Rerun the m-file; you should get a new root locus plot looking like this:

Now let's change the axis to see the details of the root locus.
axis([-40 10 -30 30]) z=-log(0.05)/sqrt(pi^2+(log(0.05)^2)) sgrid(z,0)

Finding the gain from the root locus


Now that we have moved the root locus across the 5% damping ratio line, we can choose a gain that will satisfy the design requirements. Recall that we want the settling time and the overshoot to be as small as possible. Generally, to get a small overshoot and a fast response, we need to select a gain corresponding to a point on the root locus near the real axis and far from the imaginary axis or the point that the root locus crosses the desired damping ratio line. But in this case, we need the cancellation of poles and zeros near the imaginary axis, so we need to select a gain corresponding to a point on the root locus near the zeros and percent overshoot line. There is a method to do this with the rlocfind command in MATLAB. Enter the following command into the MATLAB command window:
[k,poles]=rlocfind(contr*G1)

Go to the plot and select the point at the position mentioned above (indicated by the cross on the plot below:

You should see something similar to the following:

selected_point = -2.9428 -13.0435i K = 1.0678e+08 poles = 1.0e+02 * -0.6322 -0.6322 -0.0294 -0.0294 -0.0292 -0.0292 + + + 6.1536i 6.1536i 0.1306i 0.1306i 0.0367i 0.0367i

Note that the value returned from your MATLAB command window may not be exactly the same, but should at least have the same order of magnitude. This returned value can be used as the gain for the compensator. Recall that the schematic of the system is the following:

and the closed-loop transfer function can be derived as following:


sys_cl=F*feedback(G1,k*contr);

Plotting the closed-loop response


Let's see what the closed-loop step response looks like with this compensator. Keep in mind that we are going to use a 0.1 m high step as the disturbance. To simulate this, simply multiply sys_cl by 0.1. Add the following commands into the m-file and put % marks in front of all rlocus and rlocfind commands.
t=0:0.01:2; step(0.1*sys_cl,t) title('closed-loop response to 0.1m high step w/ notch filter')

and you should see the following plot:

From this plot we see that when the bus encounters a 0.1 m step on the road, the maximum deviation of the bus body from the wheel (or the road) is about 3.75 mm, and the oscillations settle in 2 seconds. Thus this response is satisfactory. Note: A design problem does not necessarily have an unique answer. Using the root locus method (or any other method) may result in many different compensators. For practice, you may want to go back to the original open-loop root locus and try to find other good ways to add zeros and poles to get a better response. If you are interested in running an animation of the bus suspension example based on the control techniques used in this tutorial please go to the Bus Suspension Animation Page.

Root Locus Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink | Animation

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php iu khin con lc ngc bng phng php qu tch nghim
Hm truyn Thit k qu o nghim B iu khin Lead-lag iu g xy ra vi v tr ca xe
Hm truyn ca khu chp hnh nh sau:

y,

Mc tiu thit k (vi iu kin ban u con lc nhn c 1 xung lc 1N t xe) t ra l: Thi gian n nh nh hn 5 giy. Con lc dao ng khng qu 0,05 rad xung quanh trc thng ng. bit cch thit lp m hnh, tham kho phn m hnh con lc ngc.

Hm truyn
Lnh rlocus trong Matlab cho ta qu tch nghim ca h thng c m t bi phng trnh trong khng gian trng thi hoc hm truyn. Vi bi ton ny ta s dng hm truyn s thun tin hn (l do chng ta s tm hiu sau). Hm truyn c lp nn t php bin i Laplace vi u ra Phi (gc quay ca con lc) c thit lp trong Matlab bng cch khai bo t s v mu s nh hai vecto. To 1 m-file c on m lnh sau:
M m b i g = = = = = .5; 0.2; 0.1; 0.006; 9.8;

l = 0.3; q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q pend=tf(num,den); %simplifies input -b*m*g*l/q];

-(M+m)*m*g*l/q

S khi h thng
M hnh ca h thng ny c th hi khc so vi nhng m hnh truyn thng m ta quen bit. Do vy y t bi ton l iu khin con lc v tr thng bng vi u vo nhiu l mt xung lc ban u, tn hiu reference l 0. Xung lc ban u y chnh l mt xung nhiu. S h thng nh vy s c dng sau.

d dng chuyn i thnh hm truyn trong Matlab ta bin i tng ng thnh s sau:

Thit k qu tch nghim


Hm truyn ca h kn ny c th c biu din trong Matlab. iu u tin cn lm l kho st qu tch nghim ca khu chp hnh khi cha c khu b. Khi chn h s iu khin t l cn nh rng thi gian qu phi nh hn 5 giy. iu ny ngha l sigma ln hn 4,6/5=0,92. Ta c th t iu kin ny ln qu tch nghim bng cch s dng hm sigrid, hm ny c th c lu trong 1 m-file. v qu tch nghim, thm cc lnh sau vo m-file :
rlocus(pend) sigrid(0.92) axis([-6 6 -6 6]) Sau khi chy ta c qu o nghim nh sau:

Nh ta thy, c 1 nghim ca hm truyn h kn nm pha na bn phi mt phng to , ngha l h thng s mt n nh. Hy nhn vo qu tch nghim bit ti sao. Mt phn ca qu tch nghim nm gia gc ta v im cc bn phi mt phng to . Do vy m cho d chn h s nh th no th ta vn lun c 1 cc nm vng ny v n lm p ng xung ca h thng mt n nh. gii quyt vn ny, ta cn thm vo gc to mt im cc, cc im khng v im cc ti gc to trit tiu ln nhau v lm xut hin cc nghim bi na bn phi mt phng to . Cc nghim bi c th c dch v na pha tri mt phng to hon tt mc tiu ra. Thm cc cu lnh sau vo m-file:
contr=tf(1,[1 0]); rlocus(contr*pend) sigrid(0.92) axis([-10 10 -10 10])

Ta c qu tch nghim vi cc nghim bi na bn phi mt phng to nh sau:

By gi ta hy th v cc nhnh ca qu tch nghim v pha bn tri ca mt phng to . Thm vo m-file cc lnh sau:
[num,den]=tfdata(contr*pend,'v'); roots(num) roots(den)

Ta s c kt qu sau trong ca s lnh Matlab,


ans = ans = 0

0 -5.6041 5.5651 -0.1428

By gi ta c 4 cc v 1 im khng. iu ny ngha l s c 3 ng tim cn: mt ng dc theo chiu m ca trc thc, hai ng cn li v tr 120o so vi ng th nht. Nhng vi dng ng ny ta s khng th c cc nghim bi na bn tri mt phng to . Chng ta cn gim s lng ng tim cn xung cn 2 bng cch thm 1 im khng na vo b iu khin. Nu ch c 1 im khng c thm vo th giao im alpha ca cc ng tim cn s l [(-5.6041+5.5651-0.1428+0+0)-(0+0+z2)]/2. iu ny ngha l hai ng tim cn s l -0.1-(1/2)z2. Cho d chn z2 nh nht c th (gi s l st gc to ) th cng khng th y cc nghim bi ra xa v pha bn tri mt phng to t c cc ch tiu iu khin ra (giao im cc ng tim cn s gn im trn trc thc-0.1).

B iu khin Lead-lag
Gii php cho vn ny l thm 1 im cc xa hn v pha tri so vi cc im cc v im khng

khc. V gi nguyn s ng tim cn, ta cng cn thm 1 im khng na. Cch xc nh v tr im cc v im khng l khng quan trng tr vic gi tr ca im cc phi tng i ln v gi tr ca im khng phi tng i nh. Hy th chy m-file sau v xem nh hng ca cc im cc v im khng ln qu tch nghim.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q];

q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q pend=tf(num,den);

-(M+m)*m*g*l/q

z1 = 3; p1 = 0; z2 = 4; p2 = 50; numlag = [1 z1]; denlag = [1 p1]; numlead = [1 z2]; denlead = [1 p2]; numc = conv(numlead, numlag); denc = conv(denlead, denlag); contr=tf(numc,denc); rlocus(contr*pend) sigrid(0.92) axis([-50 50 -50 50]) figure rlocus(contr*pend) sigrid(0.92) axis([-10 10 -10 10]) [k,poles]=rlocfind(contr*pend) sys_cl=feedback(pend,k*contr); figure impulse(sys_cl) axis([0 2 -0.05 0.05])

Ch : Vi 1 s version ca Matlab, qu tch nghim i khi khng c chnh xc v vecto h s c chn 1 cch t ng. Nu iu ny xy ra vi version ca bn th bn nn truy cp vo trang ch MathWorks download phn sa li. Bn cng c th t t cho bit Matlab min tham s no m bn ang quan tm. Trong trng hp , cc lnh sau c dng thay cho lnh rlocus.
gain=0:10:2000; rlocus(contr*pend,gain)

Qu tch nghim s c v bi du "x" thay v ng nt lin, nhng lnh rlocfind vn s c thc hin ng n. Cc im cc v im khng c xc nh sau 1 s ln th sai. Bn ch cn nh l ta cn phi c 1 im cc gc to v 1 im cc khc xa v pha bn tri mt phng to , v hai im cc
5

phi tng i nh. Hn na, hai im khng m kh gn nhau v gn v pha phi ca im cc xa nht bn tri th cng tt. Ta s thy th th nht ca qu tch nghim tng ng vi cc im cc v im khng trn:

th th hai l phng to ca th th nht nhn r thy vng gn gc to .

Khi chn 1 im trn qu tch nghim, chn 1 im trn cc nghim bi trc khi n quay v trc thc. p ng xung ca ta s c dng sau:

p ng trn tho mn cc yu cu t ra v ta khng cn iu chnh thm g na.

iu g xy ra vi v tr ca xe?
Ngay t u, chng ta a ra m hnh ca h thng. Nhng m hnh cha thc s hon thin. Chng ta b qua dch chuyn ca xe v ta ang quan tm ti gc quay ca con lc. S rt hay nu ta xem xt dch chuyn ca xe ra sao khi ta ang iu khin gc quay ca con lc. c th, trc ht ta xem s khi thc t nh sau:

Bin i li 1 cht ta c s khi nh sau:

Vng kn trn l h thng ta va thit k vi b iu khin PID cho gc quay con lc. Hm truyn vi u ra l dch chuyn ca xe by gi nh sau:

By gi ta c th xy dng hm truyn cho ton b h thng, hy xem p ng ca n ra sao. Trc ht ta tm hm truyn cho dich chuyn ca xe. c uc iu ny ta phi s dng bin i Laplace cho phng trnh h thng ri tm mi lin h gia X(s) v U(s). Ta c phng trnh sau:

y,

bit thm v bin i Laplace tham kho phn m hnh con lc ngc. Thm on lnh sau vo m-file v cho chy:
num2 = [(i+m*l^2)/q 0 -m*g*l/q]; den2 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q G2=tf(num2,den2); figure; t=0:0.01:6; subplot(2,1,1); impulse(sys_cl,t) axis([0 6 -0.05 0.05]) subplot(2,1,2); xpos=feedback(1,k*contr*pend)*G2; impulse(xpos,t) axis([0 6 -0.1 0.1]) -b*m*g*l/q 0];

Ta s c p ng nh sau:

p ng pha trn l gc quay ca con lc, p ng pha di l v tr ca xe. Nh ta thy, lc u xe c dch chuyn , sau v tr n nh trong khong 5 giy, ri b mt n nh. C th trn thc t ma st (b b qua m hnh ny) s lm cho xe n nh. Hy nh l trn thc t c xy ra nh vy, nhng n c l nh may mn, bi ta u c t vn iu khin v tr ca xe.

Root Locus Examples

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Bi ton bng v thanh

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Root-Locus Design method for the Pitch Controller


Original root-locus plot Lead compensator Quick summary
In the Pitch Controller Modeling page, the transfer function was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the pitch angle (theta). The design requirements are Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2% To see the original problem setup, please refer to the Pitch Controller Modeling page.

Original root-locus plot


Recall from the Root-Locus Tutorial page, a root-locus plot shows all possible closed-loop pole locations for a pure proportional controller. Since not all poles are acceptable, the MATLAB function called sgrid should be used to find an acceptable region of the locus. This sgrid function requires two arguments: Natural frequency (Wn) and damping ratio (zeta). These two arguments can be determined from the rise time, the settling time, and the overshoot requirements and three equations shown below.

where,

Wn=Natural frequency zeta=Damping ratio Ts=Settling time Tr=Rise time Mp=Maximum overshoot From these three equations, we can determine that the natural frequency (Wn) must be greater than 0.9 and the damping ratio (zeta) must be greater than 0.52. Let's generate a root-locus plot and use the sgrid to find the acceptable region of the locus. Create a new m-file and enter the following commands:
num=[1.151 0.1774]; den=[1 0.739 0.921 0]; plant=tf(num,den); Wn=0.9; zeta=0.52; rlocus (plant) sgrid (zeta,Wn) axis ([-1 0 -2.5 2.5])

Run this m-file in the MATLAB command window. You should see the root-locus plot similar to the one shown below:

The two dotted lines in an angle indicate the locations of constant damping ratio, and the damping ratio is greater than 0.52 in between these lines. The dotted semi-ellipse indicates the locations of constant natural frequency, and the natural frequency is greater than 0.9 outside the semi-ellipse (this would be a semi-circle if the axes were uniformly scaled). As you may have noticed, there is no root-locus plotted in our desired region. We need to bring the root-locus in between two dotted lines and outside the semi-ellipse by modifying the controller.

Lead compensator
We need to shift the root-locus more toward the left to get it inside our desired region. If you refer to the Designing Lead and Lag Compensators page, you will notice that the lead compensator can move
2

the root locus to the left. The transfer function of a typical lead compensator is:

Zo=zero Po=pole Zo < Po In general, the zero is placed in the neighborhood of the natural frequency criterion, and the pole is placed at a distance 3 to 20 times the value of the zero location. Let's place the zero (Zo) at 0.9 and the pole (Zo) at 20. Let the MATLAB functions conv and feedback determine the closed-loop transfer function with the lead compensator. Enter the following commands to an new m-file and run it in the MATLAB command window. You should obtain the following root-locus plot:
num=[1.151 0.1774]; den=[1 0.739 0.921 0]; plant=tf(num,den); Zo=0.9; Po=20; contr=tf([1 Zo],[1 Po]); Wn=0.9; zeta=0.52; rlocus (contr*plant) axis ([-3 0 -2 2]) sgrid (zeta,Wn)

The root-locus has been generated in our desired region. Now, we are ready to pick a gain (K) and generate the step response corresponding to that gain. Add the following commands to the m-file shown above and run it in the command window. You should see a prompt asking you to pick a point on the root-locus plot. Pick a point close to the zero on the natural frequency criterion, say around -1 on real axis. This point should give you a gain around 200. You should see a step response similar to the one shown below.
[K, poles]=rlocfind (contr*plant)

de=0.2; sys_cl=feedback (K*contr*plant,1); step(sys_cl)

This response satisfies all the design requirements.

How to solve a problem using Root-Locus method: Quick Summary


1. 2. 3. 4. 5. 6. 7. 8. Obtain a root-locus plot with the sgrid using the original plant transfer function. Add a lead (or lag) compensator to bring the root-locus to the desired region, if necessary. Pick a point on the root-locus and obtain the corresponding gain (K). Generate the step response with the chosen gain (K). Determine what needs to be changed from the step response. Add or modify the lead (or lag or lead-lag) compensator. Obtain new root-locus plot with the sgrid command active. Repeat steps 3 to 7 until you obtain a satisfactory result.

Root-Locus Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Control | Ball and Beam

Pitch Controller Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control| Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php cho bi ton Bng & Thanh s dng phng php qu tch nghim
Qu tch nghim h h B iu khin Lead Chn h s V p ng ca h kn
Hm truyn h thng h thit lp trong phn m hnh nh sau:

Mc tiu iu khin: Thi gian qu nh hn 3 giy qu iu chnh nh hn 5% bit cch thit lp m hnh ton hc trn, tham kho phn m hnh bng v thanh . S khi h thng iu khin vng kn nh sau:

Qu tch nghim ca h h
tng chnh ca vic thit k qu tch nghim l c on p ng ca h kn thng qua qu tch nghim ca h h. Bng cch thm vo h thng cc im khng v im cc (thm khu b), qu tch nghim v p ng ca h kn s c iu chnh theo mun. Trc ht chng ta hy kho st qu tch nghim ca h h. To 1 m-file vi cc lnh sau lp m hnh h h v v qu tch nghim.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m));

num = [-K]; den = [1 0 0]; plant=tf(num,den); rlocus(plant)

Chy m-file ta s c qu tch nghim nh sau:

Nh ta thy, h thng c hai im cc ti gc to v i v v cng dc theo trc o. Tiu chun iu khin cng c biu din bng lnh sgrid. Lnh ny s to ta 1 li c nh ph thuc vo gi tr h s gim chn v tn s dao ng ring. H s gim chn v tn s dao ng ring tnh theo hai phng trnh sau, chng ph thuc vo phn trm qu iu chnh (PO) v thi gian qu (Ts):

Ch , phng trnh vi Ts c lp nn khi p ng t ti ln cn trong khong 2% so vi gi tr n nh cui cng. T cc phng trnh trn, t cc phng trnh trn ta tnh c h s gim chn v tn s dao ng ring l 0.7 v 1.9.
sgrid(0.70, 1.9) axis([-5 5 -2 2])

Vng gii hn bi cc ng gch cho l vng qu iu chnh nh hn 5%. Vng bn ngoi na ng cong elip l vng thi gian qu nh hn 3 giy. Ta ch v thy l qu o nghim khng c im no nm vo vng tho mn mc tiu iu khin. gii quyt vn ny ta phi lm sao dch qu o nghim v pha bn tri, ta s thm vo h thng khu b lead.

B iu khin Lead
Trc tin ta ch quan tm ti tc dng ca b iu khin Lead l lm cho qu o nghim dch v pha tri mt phng to . bit chi tit hn v b iu khin ny, tham kho phn Thit k khu b Lead & Lag. Mt khu b Lead c hm truyn nh sau:

y, zo b hn po. By gi ta s thm b iu khin Lead vo v xem qu tch nghim by gi thay i nh th no. Ta s t mt im khng gn gc to kh i mt cc. Cc ca khu b s c t pha bn tri gc to ko qu tch nghim v pha tri mt phng to . Thm cc lnh sau vo m-file.
zo = 0.01; po = 5; contr=tf([1 zo],[1 po]); rlocus(contr*plant) sgrid(0.70, 1.9)

Chy m-file trong ca s lnh ca Matlab ta c qu tch nghim nh sau:

By gi ta c th thy cc nhnh ca qu tch nghim nm trong vng mong mun.

Chn h s
By gi qu tch nghim dch v na bn tri mt phng to , ta c th tin hnh chn cc h s tho mn mc tiu ra. Ta dng lnh rlocfind thc hin chn. Thm cc lnh sau vo m-file.
[k,poles]=rlocfind(contr*plant)

Chn 1 im gn mt trong cc im c nh du + nh hnh v di:

Ta s thy trong ca s lnh ca Matlab kt qu ra gn gin nh sau (c th gi tr kt qu c hi khc 1 cht):

selected_point = -2.4988+ 1.2493i k = 37.3131 poles = -2.4950+ 1.2493i -2.4950- 1.2493i -0.0101

V by gi ta c th xem p ng ca h thng nh th no vi cc h s nh trn.

V p ng ca h kn
Gi tr ny ca kc c t vo h thng v ta c th thu c p ng vi u vo bc thang 0,25m. Thm on lnh sau vo m-file v c tnh ca h thng.
sys_cl=feedback(k*contr*plant,1); t=0:0.01:5; figure step(sys_cl,t)

Chy m-file v chn im nh ni trn. Ta c p ng nh sau:

T p ng trn ta thy vi u vo tn hiu bc thang 0.25m th c thi gian qu v qu iu chnh u t yu cu. Ch : Mi v d u c nhiu phng n gii quyt. Vi phng n ny (hoc bt k phng n no khc) ta c th c nhiu khu b khc nhau. Hy th chy m-file nhiu ln v mi ln chn 1 in khc nhau, ta s c p ng khc nhau v qua ta se hiu hn v mc nh hng ca cc im

trn qu tch nghim ti p ng ca h thng. V bn cng c th thay i cc im khng v cc ca khu b tu tm ra nhng p ng tt hn.

Root Locus Examples

iu khin xe | iu khin tc ng c | iu khin gc quay ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Bi ton bng v thanh

Ball & Beam Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Phn tch p ng tn s (Frequency Response Analysis)


I. Biu Bode [ Bin ca h s v pha | Di bng tn | p ng ca h kn ] II. Biu Nyquist [ Tnh n nh ca h kn | Bin ca h s | Bin ca pha ]
Cc lnh ch yu dng trong phn ny l bode, nyquist, nyquist1, lnyquist, margin, lsim, step, and feedback Phng php phn tch c tnh tn s c v t trc quan hn cc phng php khc m ta nghin cu. Tuy vy, n c nhiu u im, c bit l trong cc tnh hung thc t v d nh hm truyn t cc d liu vt l. p ng tn s ca h thng c th c biu din theo hai cch: thng qua biu Bode hoc Nyquist. Chng l cng 1 vn ; ch khc nhau cch biu din thng tin. Trong phn ny ta nghin cu c hai cch trn. p ng tn s l biu din p ng ca h thng vi u vo dng sin vi cc tn s khc nhau. u ra ca h tuyn tnh vi u vo sin cng c dng sin cng tn s vi u vo ch khc pha v bin . p ng tn s c nh ngha l s khc nhau v bin v pha gia u ra v u vo sin. Trong phn ny, ta s tm hiu xem lm cch no m ta c th tin on c hnh vi ca h kn thng qua p ng ca h h. biu din p ng tn s ca h h, ta to ra 1 vecto tn s (cc gi tr chy t 0 hoc "DC" n v cng) v tnh gi tr ca hm truyn ti cc tn s . Nu G(s) l hm truyn ca 1 h h v w l vecto tn s, th ta s biu din G(j*w) v w. V G(j*w) c gi tr phc nn ta biu din c bin v pha ring (biu Bode) hoc biu din trn mt phng phc (biu Nyquist). bit thm, tham kho phn biu din p ng tn s.

Biu Bode
Nh nhn mnh trn, biu Bode biu din c bin v pha ca G(j*w) (vecto tn s w ch gm nhng tn s dng). xem biu Bode ca 1 hm truyn, ta dng lnh bode. V d,
num = 50; den = [1 9 30 40]; sys = tf(num,den); bode(sys)

s cho ta biu Bode nh sau:

50 ----------------------s^3 + 9 s^2 + 30 s + 40

Ch n v ca biu . Tn s ly theo loga, pha ly theo , bin ly theo dexiben.. Ch : 1 dexiben c nh ngha bng 20*log10 ( |G(j*w| ) Kch vo y xem 1 s biu Bode in hnh.

Bin ca h s v pha
Gi s ta c h thng sau:

K l 1 h s c gi tr thay i v G(s) l khu chp hnh. Bin ca h s c nh ngha l s thay i ca h s trong h h cho h thng b mt n nh. Nhng h thng c bin h s ln th c th n nh vi c nhng thay i ln ca cc thng s h thng trc khi tr nn mt n nh. Hy ghi nh rng gi tr bin bng 1 ng vi gi tr bng 0 DB. Bin ca pha c nh ngha l s thay i pha lm cho h kn mt n nh. Qua bin ca pha cng o c dung sai v thi gian tr ca h thng. Nu xut hin mt thi gian tr ln hn180/Wpc (vi Wpc l tn s m ti pha b lnh i 180 ), th h kn s khng n nh. Thi gian tr c coi nh l mt khi ph thm trong s khi lm tng pha ca h thng. Ngha l, mt khong thi gian tr c th c biu din nh mt khi vi bin bng 1 v pha bng w*(thi gian tr) [n v l radian/giy). By gi ta khng quan tm ngay ti vic vn ny bt ngun t u m tp trung lun vo xc nh

bin ca h s v pha trn biu Bode. Bin ca pha l lch pha gia ng cong pha v ng -180o ti tn s m bin p ng tn s bng 0dB (im Wgc). Cng tng t nh vy, bin h s l lch gia ng cong bin v ng 0dB ti tn s m pha ca p ng tn s bng -180o (im Wpc).

Mt iu rt hay l ta khng cn v li biu Bode xc nh li bin ca pha khi thay i h s khu t l. Ta nh li rng khi thm khu t l th biu bin-tn s ch b y dc theo chiu ca trc tung. V d, gi s ta gi lnh bode(sys). Ta s c biu Bode nh sau:

Ta thy rng bin ca pha l 100o. By gi gi s ta c khu t l l 100, gi lnh bode(100*sys) ta c biu sau (lu l ta khng thay i trc to nn t l cc trc vn nh vy, biu c th khng ging y nh trc v cn phi tu vo h s m ta chn):

Nh ta thy biu pha-tn s vn gi nguyn, biu bin-tn b y xung 1 khong l 40dB (v t l l 100). Bin ca pha by gi l -60o. Kt qu ny cng c tm thy nu ta tm bin pha trn biu c ti tn s cho p ng bin l -40dB. Ta c th trc tip tm bin h s v pha vi lnh margin trong Matlab. Lnh ny tm cho ta bin h s v pha, tn s ti v c trn th :
margin(sys)

Di bng tn
Di bng tn c nh ngha l tn s m ti p ng bin ca h kn bng -3 dB. Tuy nhin, khi thit k h thng d trn p ng tn s, ta quan tm ti vic phn on hnh vi ca h kn t p ng tn s ca h h. Do vy, ta dng pht biu gn ng l di bng tn l cc tn s ti bin
4

p ng tn s ca h h nm trong khong -6 n - 7.5dB, v tha nhn p ng pha ca h h nm trong khong -135o n -225o. xem y cc dn xut ca pht biu gn ng ny, hy tham kho thm trong sch in. Nu bn mun tm hiu thm v cch tm di bng tn t h s gim chn v tn s dao ng ring ca h kn thi c th tham kho phn Bandwidth Frequency. minh ho tm quan trng ca di bng tn, chng ta a ra v d v s thay i ca u ra vi cc tn s ca u vo khc nhau. Chng ta s tm tt c u ra tng ng vi cc tn s nh hn Wbw (di bng tn). Cc u vo sin vi tn s ln hn Wbw nhng bin nh hn (bng 0,707) hoc ln hn 1 cht. Gi s ta c hm truyn ca h kn nh sau:
1 --------------s^2 + 0.5 s + 1

Trc tin, ta tm di bng tn t biu Bode:


num = 1; den = [1 0.5 1]; sys = tf(num,den); bode (sys)

T hm truyn trn, di bng tn ca ta s l tn s cho p ng bin l -3 dB. Nhn vo biu , ta thy tn s khong 1.4 rad/s. Ta cng thy ngay rng vi tn s u vo 0.3 rad/s, u ra sin c bin bng 1 v b lch pha vi chm sau so vi u vo. Vi tn s u vo 3 rad/s, bin u ra khong -20dB (bng 1/10 so vi u vo) v pha gn bng -180 (gn nh ngc pha). Ta c th dng lnh lsim m phng p ng u ra vi u vo sin. Trc tn, ta xem xt u vo sin vi tn s nh hn Wbw. V ta nn nh rng ta ang quan tm ti p ng ca h thng trng thi n nh. Do vy, ta s thay i khung nhn c th thy p ng ca h thng giai on n nh d dng hn (b qua p ng tc thi).
w = 0.3; 5

num = 1; den = [1 0.5 1]; sys = tf(num,den); t = 0:0.1:100; u = sin(w*t); [y,t] = lsim(sys,u,t); plot(t,y,t,u) axis([50,100,-2,2])

Ch u ra (ng mu xanh) kh bm theo u vo (ng mu tm); ch chm sau vi . Tuy nhin, nu tn s u vo cao hn di bng tn, ta s c u ra khc hn vi u vo:
w = 3; num = 1; den = [1 0.5 1]; sys = tf(num,den); t = 0:0.1:100; u = sin(w*t); [y,t] = lsim(sys,u,t); plot(t,y,t,u) axis([90, 100, -1, 1])

Bin u ra ch bng 1/10 u vo, nh suy on trc, v chng gn nh ngc pha nhau (u ra chm pha 180o). Bn hy t nhin thc hin vi mt s tn s khc v xem nhng g xy ra c ng nh d bo trn biu Bode khng.

Biu hin ca h kn
suy on biu hin ca h kn t p ng tn s ca h h chng ta cn thng nht vi nhau 1 s quan nim sau: Khi thit k bng phng php biu Bode, th yu cu h h phi l n nh. Nu ng ging bin b hn ng ging pha (ngha l Wgc < Wpc), th h kn s n nh. Vi cc h thng dn xut, h s gim chn bng bin ca pha chia cho 100 nu bin ca pha

nm trong khong 0 n 60o. Ta s ch ti iu ny khi di bng tn ln hn 60o. Vi cc h thng dn xut, quan h gia h s gim chn, di bng tn v thi gian qu c biu din thng qua 1 phng trnh nu phn di bng tn. Vi 1 c lng rt th ta c th coi di bng tn xp x bng tn s dao ng ring. Vi nhng quan nim nh trn, ta tin hnh thit k 1 b iu khin cho h thng c dng sau:

Gc(s) l b iu khin v G(s) l:


10 ---------1.25s + 1

Yu cu thit k: Khng c sai s xc lp. qu iu chnh ln nht cho php phi nh hn 40%. Thi gian qu phi nh hn 2 giy. C hai phng php thc hin l: phng php th v phng php s. Vi Matlab, tip cn theo th l tt nht, ta s dng cch ny. Trc ht, ta xem biu Bode. To 1 m-file c cc lnh sau:
num = 10; den = [1.25,1]; sys = tf(num,den); bode(sys)

Qua biu ny ta c th thy ngay mt s c tnh ca h thng. Trc tin, ta thy di bng tn khong 10 rad/s. V di bng tn c lu th l bng tn s dao ng ring (vi h thng dng dn
7

xut th nht), thi gian qu l 1,8/BW=1,8/10=1,8 giy. y ch l 1 c lng th, ta c th ni thi gian qu bng 2 giy. Bin ca pha by gi khong 95 . H s gim chn = pm/100 ch c ngha vi PM < 60. V h thng c dng dn xut th nht nn khng c qu iu chnh. iu cui cng cn lu l sai s xc lp. Sai s xc lp c th c tm trc tip t biu Bode. Cc hng s (Kp, Kv, hoc Ka) c xc nh l giao im ca ng tim cn di vi ng w=1. Bin ti im ny l hng s. Vi h thng ny ta c ng tim cn di nm ngang ( dc = 0), nn h thng ny c dng 0. Do , giao im c xc inh d dng. Bin loga 20dB (bin = 10). iu ny ngha l tham s ca hm sai s l 10. Kch vo y xem bng thng k cc dng hm v hm sai s ca chng. Sai s xclp y bng 1/(1+Kp)=1/(1+10)=0.091. Nu h thng c dng 1, th tham s ca hm sai s c xc nh ging nh hnh sau

Hy xem cc phn on ca ta v p ng ca h kn c ng khng. Thc hin hai lnh sau.


sys_cl = feedback(sys,1); step(sys_cl)

Nh ta thy phn on ca ta l ng. Thi gian qu ca h thng khong 2 giy, khng c qu iu chnh, sai s xc lp khong 9%. By gi ta thit k 1 b iu khin tho mn yu cu t ra. Ta nn chn 1 b iu khin PI v n s cho sai s xc lp bng 0 vi tn hiu u vo bc thang. B

iu khin PI cng cho ta 1 im khng m ta c th t tu . iu ny lm cho ta c thm kh nng iu kin mm do hn na. Nhc li kin thc c, mt b iu khin PI c hm truyn nh sau:
K*(s+a) Gc(s) = ------s

Vic u tin l tm l h s gim chn tng ng vi qu iu chnh 40%. Thay gi tr 40% vo phng trnh biu din quan h gia h s gim chn v qu iu chnh (hoc tham kho th ca quan h ny), ta tm c h s gim chn xp x bng 0,28. Do , bin ca pha t nht cng khong 30o. T Ts*Wbw v h s gim chn, ta tm c Ts*Wbw ~ 21. Ta phi c di bng tn ln hn hoc bng 12 nu mun thi gian qu nh hn 1,75 giy nh mong mun. By gi ta xc nh c di bng tn v bin ca pha cn thit, ta c th bt u cng vic thit k. Lu rng ta ang xem xt biu Bode ca h h. Do , di bng tn s l tn s tng ng vi bin xp x bng -7 dB. By gi ta hy xem b iu khin PI nh hng ti h thng nh th no. Thay i m-file nh sau (ch c iu khin I, cha c iu khin P):
num = 10; den = [1.25 1]; plant = tf(num,den); numPI = 1; denPI = [1 0]; contr = tf(numPI,denPI); bode(contr * plant, logspace(0,2))

Bin ca pha v di bng tn l qu nh. Ta thm im khng vo tng chng ln. t im khng ti l 1 v xem iu g xy ra. Thay i m-file nh sau:
num = den = plant numPI denPI contr 10; [1.25 1]; = tf(num,den); = [1 1]; = [1 0]; = tf(numPI,denPI);

bode(contr * plant, logspace(0,2))

im khng 1 vi t l l 1 cho ta li gii p. Bin ca pha ln hn 60o ( qu iu chnh cng tt hn mong i) v di bng tn xp x 11 rad/s, nh th ta s c c p ng mong mun. Mc d tho mn, nhng p ng cng cha hn tt i vi chng ta. Do , ta hy th cho di bng tn cao ln m khng lm thay i nhiu n bin ca pha xem sao. Ta tng t l ca im khng ln bng 5 v xem iu g xy ra.
num = 10; den = [1.25 1]; plant = tf(num,den); numPI = 5*[1 1]; denPI = [1 0]; contr = tf(numPI,denPI); bode(contr * plant, logspace(0,2))

Nh vy l tt thc s. Hy xem p ng vi u vo tn hiu bc thang v kim nghim li. Thm cc lnh sau vo m-file:
10

sys_cl = feedback(contr * plant,1); step(sys_cl)

Nh ta thy by gi p ng tt hn hn nhng g mong i. Tuy nhin chng ta cng cn 1 cht may mn trong qu trnh iu chnh cc im cc v im khng t cc yu cu t ra. Ch ny s cn c bn trong phn biu Nyquist.

Biu Nyquist Frequency response Examples Tutorials


Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

11

V d: Gii php iu khin xe dng biu p ng tn s


Biu Bode v p ng ca h h B iu kin t l B iu khin Lag
Hm truyn h h ca h nh sau:

m=1000 b=50 U(s)=10 Y(s)=Velocity output Mc tiu iu khin l: Thi gian qu < 5 sec qu iu chnh < 10% Sai s xc lp < 2% bit cch thit lp m hnh, tham kho phn M hnh iu khin xe.

Biu Bode v p ng ca h h
Bc u tin gii quyt vn ny l xc nh hm truyn h h. Cng ging nh phong php Qu o nghim, chng ta ch s dng b iu khin t l gii quyt. S khi h thng v hm truyn nh sau.

dng c biu Bode, p ng ca h h nht thit phi n nh. Cho Kp = 1 v xem p ng ca h h nh th no. To 1 m-file c cc lnh sau.
m = 1000; b = 50; u = 500;

Kp = 1; num = 1; den = [m b]; cruise = tf(num,den); contr = Kp; step(u*contr*cruise)

Chy m-file ta s c p ng sau.

Nh ta thy, h h n nh; ta c th lm tip v tm biu Bode. Thay i m-file bng cch thay lnh step bng lnh sau.
bode(contr*cruise)

Chy m-file v ta s c biu Bode nh sau.

B iu khin t l
Hy xem ta c th xc nh c c tnh g t biu trn. Nhc li kin thc t phn Qu o nghim, di bng tn (BW) (tn s ti t l bin M(dB)=-6~-7,5dB) xp x th bng tn s dao ng ring (Wn). Dng phng trnh sau,

thi gian qu (Tr) ca h thng c th c xc nh qu ln v h s t l bin trn cha thc s nm trong khong -6~-7,5dB. Hn na, h s gim chn xp x th bng bin ca pha (tnh theo ) chia cho 100.

V bin ca pha trong trng hp ny xp x 155o, nn h s gim chn l 1,55. Nh vy, ta h thng ca ta b gim chn qu mc (h s gim chn ln hn 1). Cui cng, sai s xc lp c th c tnh theo phng trnh sau:

Vi h thng ca ta, v h s t l bin tn tho M(dB) xp x -35dB nn sai s xc lp khong 98%. Ta c th kim tra li bng cc lnh sau.
u=10; sys_cl=feedback(contr*cruise,1); step(u*sys_cl);

Vi biu Bode, nu ta tng h s t l ln th c di bng tn v h s t l bin tn thp s tng ln v thi gian qu cng nh sai s xc lp s uc ci thin. Chng ta c th thc hin iu bng cch tng h s t l Kp. Hy choKp = 100 v xem iu g xy ra. Thay i m-file thnh nh sau.
m = 1000; b = 50; u = 10; Kp = 100; num = 1; den = [m b]; cruise = tf(num,den); contr = Kp; 3

bode(contr*cruise)

Chy m-file trong Matlab ta c biu Bode nh sau.

By gi, h s t l bin tn thp khong 6dB (bin l 2) ta c th on trc sai s xc lp khong 33%. Di bng tn khong 0.1 rad/sec nn thi gian qu s khong 18 giy. Ta hy xem p ng ca h kn kim chng li.
sys_cl=feedback(contr*cruise,1); step(u*sys_cl);

Nh ta d on, c sai s xc lp v thi gian qu c ci thin. Ta th tng h s t l ln na xem iu g xy ra. Ch cn thay i m-file 1 cht v chy li. Ta c biu Bode nh sau.
m = 1000; b = 50; u = 10; Kp = 500; num = 1; den = [m b]; cruise = tf(num,den); contr = Kp; bode(contr*cruise) 4

By gi, h s t l bin tn thp l 20dB (bin l 10) tc sai s xc lp khong 9%, v di bng tn khong 0,6 tc thi gian qu d on khong 3 giy (gi tr chp nhn c). Nh vy l c sai s xc lp v thi gian qua c ci thin. Mt ln na ta li kim nghim li p ng ca h kn.
sys_cl=feedback(contr*cruise,1); step(u*sys_cl);

Nu bn , sai s xc lp cui cng cng s t yu cu vi h s t l tng cao hn na. Tuy nhin, cng vi vic sai s xc lp t yu cu th thi qu trnh qu li qu nhanh (iu ny khng thc t vi h vt l). Do vy, hy gi tr Kp nh th v tin hnh b tr thm b iu khin Lag x l sai s xc lp.

B iu khin Lag
Nu bn xem mc "Khu b Lag hoc Pha-Lag using c s dung p ng tn s" trong phn B b Lead v Lag, b iu khin Lag c tc dng lm tng h s t l bin trong khi vn gi nguyn di bng tn.. V chnh l iu ta mong mun: h s t l bin ln hn gim sai s xc lp v gi nguyn di bng tn bo m thi gian qu . Hm truyn ca b iu khin Lag nh sau.

By gi, ta cn chn gi tr cho a v T. Nh li kin thc trong phn "Khu b Lag hoc Pha-Lag dng p ng tn s", ta s thy sai s xc lp s gim theo h s a. Gi tr ca T s c chn sao cho hai tn s gc s khng b t gn nhau (nu nh vy kh nng o ng tc thi s xu i). Nh th cho a = 0,05 v T = 700 v xem iu g xy ra. To m-file sau v chy trong Matlab. Ta s c biu sau.
m = 1000; b = 50; u = 10; Kp = 600; num = 1; den = [m b]; cruise = tf(num,den); a = 0.05; T = 700; numlag = [a*T 1]; denlag = a*[T 1]; contr = Kp*tf(numlag,denlag); bode(contr*cruise) figure sys_cl=feedback(contr*cruise,1); step(u*sys_cl)

V h s t l bin tn thp c tng trong khi di bng tn vn gi nguyn nn sai s xc lp s gim v thi gian qu vn gi nguyn. Ta kim nghim li p ng ca h kn nh sau.

Nh trn c v kh xem, nhng trn biu c 1 ng nt t mu xanh pha di 10. ng ny cho ta gi tr xc lp ca h kn v ta thy sai s xc lp t yu cu. Tuy nhin, thi gin n nh li qu lu. gii quyt, ta tng h s t l Kp=1500. H s ny c tm ra sau 1 s php th sai m chng ti khng tin trnh by y. Vi thay i ny, biu Bode v p ng s nh sau.

Nh ta thy, qu iu chnh thc t bng 0, sai s xc lp gn nh bng 0, thi gian qu khong 2 giy, v thi gian t nh b hn 3,5 giy. H thng tho mn cc yu cu t ra.

Frequency Response Examples Cruise Control Examples Tutorials

iu khin xe | iu khin tc ng c | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Cruise Control Examples

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control

V d: iu khin tc ng c dng p ng tn s
V biu Bode nguyn gc Thm h s t l Biu din p ng ca h kn Thm b iu khin Lag
T phn t vn , ta c phng trnh ng lc hc v hm truyn h iu khin tc ng c in 1 chiu nh sau:

v s khi h thng nh sau:

bit cch thit lp m hnh, tham kho phn M hnh ng c in 1 chiu. Vi u vo tn hiu bc thang 1 rad/s, mc tiu iu khin ra l: Thi gian n nh nh hn 2 giy qu iu chnh nh hn 5% Sai s xc lp nh hn 1% To 1 m-file c cc lnh sau (tham kho phn m hnh).
J = b = K = R = L = num den 0.01; 0.1; 0.01; 1; 0.5; = K; = [(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

motor = tf(num,den);

V biu Bode nguyn gc


tng chnh ca phng php thit k theo tn s l dng biu Bode ca h h c on p ng ca h kn. Bng cch thm vo 1 b iu khin thay i biu Bode ca h h t thay i p ng ca h kn. Trc ht hy v biu Bode ca hm truyn gc. Thm vo cui m-file lnh sau v chy trong Matlab.
bode(motor)

Ta s c biu Bode sau:

Thm h s t l
T biu Bode trn, ta thy bin ca pha c th ln hn 60o nu w nh hn 10 rad/sec. Thm h s vo h thng sao cho di bng tn bng 10 rad/s, lc ta s c bin ca pha khong 60o. tm h s ng vi tn s l 10 rad/s, ta c th tm ngay trn biu Bode (n hn -40 dB 1 cht, hay bin bng 0,01 ). Lnh bode dn thm cc i s bn tay tri c dng tm chnh xc bin :
[mag,phase,w] = bode(motor,10) mag = 0.0139

c h s t l bin l 1 ng vi tn s 10 rad/sec, ta cn thm h s iu khin bng 1/0.0139 tc l bng 72 vo h thng.


contr=72; bode(contr*motor);

v cho chy m-file. Ta s c biu Bode nh sau:

Biu din o ng ca h kn
T biu trn ta thy bin ca pha l kh ln. Hy xem p ng ca h kn ra sao. Thm k t % vo trc lnh bode tm ngng lnh, v thm cc lnh sau vo m-file:
sys_cl=feedback(contr*motor,1); t=0:0.01:10; step(sys_cl,t)

Ta s c o ng sau:

Thi gian n nh tho mn, nhng qu iu chnh v sai s xc lp qu ln. qu iu chnh c th c gim bng cch gim h s t l i 1 cht (s lm cho bin ca pha ln hn), nhng nh th li lm cho sai s xc lp tng. Mt b iu khin Lag l iu cn c lc ny.

Thm b iu khin Lag


Ta c th thm b iu khin Lag gim sai s xc lp. Cng lc ta s gim qu iu chnh bng cch gim h s t l. Gim h s xung bng 50, v thm b iu khin Lag sau

s gim sai s xc lp theo h s 1/0.01 = 100 (nhng c th lm tng thi gian n nh). Thay i m-file ging nh sau:
num = K; den = [(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; motor = tf(num,den); Zo = 1; Po = 0.1; contr = 50*tf([1 Zo],[1 Po]); bode(contr*motor)

Chy m-file v ta c biu sau

Bin ca pha n. Sai s xc lp d on khong 1/40dB (quy ra l 1%) tho mn yu cu. Hy xem p ng ca h kn. Thm vo m-file cc lnh sau v chy trong Matlab.
sys_cl=feedback(contr*motor,1); t=0:0.01:10; step(sys_cl,t)

By gi ta c p ng nh mong mung. Sai s xc lp nh hn 1%, qu iu chnh khong 5%, v thi gian n nh khong 2 giy.

Frequency Response Examples Motor Speed Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Thit k h iu khin v tr ng c in 1 chiu bng phng php tn s


V biu Bode nguyn gc Thm khu tch phn Xc nh bin h s, pha v thit k b iu khin
T phn m hnh gc, ta c phng trnh ng lc hc v hm truyn c h nh sau :

v s khi h thng nh sau:

bit cch thit lp m hnh, tham kho phn M hnh ng c in 1 chiu. Vi u vo tn hiu bc thang 1 rad, mc tiu iu khin t ra l: Thi gian n nh nh hn 40 mili giy qu iu chnh nh hn 16% Khng c sai s xc lp Khng c sai s xc lp do nhiu

V biu Bode nguyn gc


To 1 m-file c cc lnh sau (tham kho phn m hnh).
J b K R L = = = = = 3.2284E-6; 3.5077E-6; 0.0274; 4; 2.75E-6;

num = K; den = [(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; motor = tf(num,den);

tng chnh ca phng php thit k theo tn s l dng biu Bode ca h h c on p ng ca h kn. Bng cch thm vo 1 b iu khin thay i biu Bode ca h h t thay i p ng ca h kn. Trc ht hy v biu Bode ca hm truyn gc. Thm vo cui m-file lnh sau v chy trong Matlab.
w=logspace(0,4,101); bode(motor,w)

Ta s c biu Bode sau:

Thm khu tch phn


Ta thm khu tch phn c sai s xc lp do nhiu bng 0. Thm cc lnh sau vo m-file:
numi = 1; deni = [1 0]; contr = tf(numi,deni); bode(contr*motor,w)

Ta s c biu sau:

Xc nh bin h s, pha v thit k b iu khin


Chng ta mun qu iu chnh nh hn 16%, do vy ta phi tnh h s gim chn thch hp. Ta cng phi tnh c bin ca pha tng ng bng 100 ln h s gim chn. T yu cu v thi gian n nh, ta c th tnh ra di tn s cn c. Thm cc lnh sau vo m-file:
zeta = -log(.16)/sqrt(pi^2+(log(.16))^2); PM = 100*zeta; wbw = (4/(0.04*zeta))*sqrt((1-2*zeta^2)+sqrt(4*zeta^4-4*zeta^2+2));

Chng ta cn c bin ca pha t nht l 50o, cho nn h s t l bin phi ri vo khong -6 n -7,5 dB ti mt s tn s ln hn 250 rad/s. T biu Bode ta thy rng phi tng thm pha 80o v h s t l tng thm 60dB ti tn s 250 rad/s. H s t l lc s nm gia -6 n -7,5 dB v hn 244 rad/s. T biu Bode pha ta c th thy c 1 cc gn tn s 60 rad/s. Chng ta s dng 1 b iu khin PI t 1 im khng ti s=60 lm phng ng cong pha. Thm cc lnh sau vo m-file:
numpi = [1 60]; denpi = [1 0]; contr = tf(numpi,denpi); bode(contr*motor,w)

Ta s c biu sau:

T biu trn ta thy pha ti tn s 250rad/s cn tng thm 50o. Hy thm 1 khu b thm chnh xc 50o vo pha. Thm cc lnh sau vo m-file:
a = (1 - sin(PM*pi/180))/(1 + sin(PM*pi/180)); T = 1/(wbw*sqrt(a)); contr2 = tf([T 1],[a*T 1]); w = logspace(2,3,101); bode(contr*contr2*motor,w)

Biu Bode mi cho ta thy bin ca pha ngay ti im 250 rad/s, nhng ti h s t l bin li qu nh. H s t l bin phi tng ng tn s 240 rad/s. tng h s t l bin ln thm 20dB, chng ta nhn h s vi 10. Thm cc lnh sau vo m-file:
4

Kp = 10; bode(Kp*contr*contr2*motor,w)

Ta c biu Bode nh sau:

Ta kim tra p ng ca h h by gi ra sao. Thm cc lnh sau vo m-file:


sys_cl = feedback(Kp*contr*contr2*motor,1); t = 0:0.001:0.1; step(sys_cl,t)

Ta s c p ng sau:

qu iu chnh l qu ln, tuy nhin thi gian n nh tt hn mong i. V th nn ta hy th thit


5

k khc sao cho bin ca pha ln hn, ly khong 70o. Thm cc lnh sau vo m-file:
PM = 70; a = (1 - sin(PM*pi/180))/(1 + sin(PM*pi/180)); T = 1/(wbw*sqrt(a)); contr2 = tf([T 1],[a*T 1]); w = logspace(2,3,101); bode(contr*contr2*motor,w)

Ta c p ng sau:

Biu Bode ny c bin ca pha ti tn s gn nh bng 250 rad/s, nhng h s t l bin li qu nh mt khong 14 dB. H s t l bin phi tng ng tn s 240 rad/s. nng gi tr h s ti l bin ln ta phi nhn h s vi 5. Thm cc lnh sau vo your m-file:
Kp=5; bode(Kp*contr*contr2*motor,w)

Ta c biu sau:

By gi ta li kim tra xem p ng ca h kn ra sao. Thm cc lnh sau vo m-file:


sys_cl = feedback(Kp*contr*contr2*motor,1); t = 0:0.001:0.1; step(sys_cl)

Ta s c biu sau:

Vi p ng trn ta thy qu iu chnh tt, nhng thi gian n nh li ln. Ta hy cho di bng tn ln hn. Thm cc lnh sau vo m-file:
wbw = 300; a = (1 - sin(PM*pi/180))/(1 + sin(PM*pi/180)); T = 1/(wbw*sqrt(a));

contr2 = tf([T 1],[a*T 1]); w = logspace(2,3,101); bode(contr*contr2*motor,w)

Ta c biu sau:

Biu Bode ny cho ta thy bin ca pha s ti tn s 250 rad/s, nhng h s t l bin l qu nh mt khong 18 dB. H s t l bin phi tng ng tn s 240 rad/s. nng gi tr h s ti l bin ln ta phi nhn h s vi 8. Thm cc lnh sau vo m-file:
Kp = 8; bode(Kp*contr*contr2*motor,w);

Ta c biu sau:

Kim tra p ng ca h kn by gi ra sao. Thm cc lnh sau vo m-file:


sys_cl = feedback(Kp*contr*contr2*motor,1); t = 0:0.001:0.1; step(sys_cl)

Ta c p ng sau:

Mi th t yu cu. qu iu chnh nh hn 16% v thi gian n nh khong 40 milligiy.

Ch : Bn c th thy phng php dng p ng tn s cho v d c th ny cn phi thc hin thao tc th sai ng k. M-file dic y gin lc nhng thao tc va thc hin. Sau khi chy m-file ny, ta cng vn s c hai biu kt qu nh trn.
J b K R L = = = = = 3.2284E-6; 3.5077E-6; 0.0274; 4; 2.75E-6;

num = K; den = [(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0]; motor = tf(num,den); PM = 70; wbw = 300; a = (1-sin(PM*pi/180))/(1+sin(PM*pi/180)); T = 1/(wbw*sqrt(a)); numpi = [1 60]; denpi = [1 0]; contr = tf(numpi,denpi); contr2 = tf([T 1],[a*T 1]); Kp = 8; w = logspace(2,3,101);

bode(Kp*contr*contr2*motor,w) figure sys_cl = feedback(Kp*contr*contr2*motor,1); t = 0:0.001:0.1; step(sys_cl)

Frequency Response Examples Motor Position Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

10

Example: Frequency Design Method for the Bus Suspension System


Plotting the frequency response using the bode command Adding a two-lead controller Plotting the closed-loop response
From the main problem, the dynamic equations in transfer function form are the following:

and the system schematic is:

For the original problem and the derivation of the above equations and schematic, please refer to the bus modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to the main problem for the details of getting those commands).
m1 m2 k1 k2 b1 b2 = = = = = = 2500; 320; 80000; 500000; 350; 15020;

nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2]; G1=tf(nump,denp); num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2];

G2=tf(num1,den1); numf=num1; denf=nump; F=tf(numf,denf);

Plotting the frequency response using the bode command


The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot so that the closed-loop response will also change. Let's first draw the Bode plot for the original open-loop transfer function. Add the following line of code to your m-file and rerun:
w = logspace(-1,2); bode(G1,w)

You should get the following bode plot:

For convenience in representing systems with different natural frequencies of the system, we normalize and scale our findings before plotting the Bode plot, so that the low-frequency asymptote of each term is at 0 dB. This normalization by adjusting the gain, K, makes it easier to add the components of the Bode plot. The effect of K is to move the magnitude curve up (increasing K) or down (decreasing K) by an amount 20*logK, but the gain, K, has no effect on the phase curve. Therefore from the previous plot, K must be equal to 100 dB or 100,000 to move the magnitude curve up to 0 dB at 0.1 rad/s. Go back to your m-file and add the following line of code to your m-file before the bode command and rerun:
K=100000; bode(K*G1,w)

You should get the following bode plot:

Adding a two-lead controller


From the Bode plot above, we see that the phase curve is concave at about 5 rad/sec. First, we will try to add positive phase around this region, so that the phase will remain above the -180 degree line. Since a large phase margin leads to a small overshoot, we will want to add at least 140 degrees of positive phase at the area near 5 rad/sec. Since one lead controller can add no more than +90 degrees, we will use a two-lead controller. To obtain T and a, the following steps can be used: 1: Determine the positive phase needed : Since we want 140 degrees total, we will need 70 degrees from each controller. 2: Determine the frequency where the phase should be added: In our case this frequency should be 5.0 rad/sec. 3: Determine the constant a from the equation below, this determines the required space between the zero and the pole for the desired maximum phase added.

4: Determine T and aT from the following equations, these determine the corner frequencies so that the maximum phase will be added at the desired frequency.

Now let's put our 2-Lead controller into the system and see what the Bode plot looks like. Add the following code to your m-file, and add a % in front of the previous bode command (if there is one):
a = (1-sin(70/180*pi))/(1+sin(70/180*pi)); w=5; T=1/(w*sqrt(a)); aT=sqrt(a)/w; numc = conv([T 1], [T 1]); denc = conv([aT 1], [aT 1]); contr = tf(numc,denc);

margin(K*contr*G1)

You should get the following Bode plot:

From this plot we see that the concave portion of the phase plot is above -180 degrees now, and the phase margin is large enough for the design criteria. Let's see how the output (the distance X1-X2) responds to a bump on the road (W). Recall that the schematic of the system is:

and the closed-loop transfer function can be derived as follows:


sys_cl = F*feedback(G1,K*contr);

Plotting the closed-loop response


Let's see what the step response looks like now. Keep in mind that we are using a 0.1 m high step as the disturbance. To simulate this, simply multiply the system by 0.1. Add the following code into the m-file and rerun it. Don't forget to put % mark in front of all bode and margin commands!
t=0:0.01:5; step(0.1*sys_cl,t) axis([0 5 -.01 .01])

and you should see the following plot:

The amplitude of response is a lot smaller than the percent overshoot requirement and the settling time also is less than 5 seconds. Since we can see that an amplitude of the output's response less than 0.0001 m or 1% of input magnitude after 4 seconds. Therefore we can say that the settling time is 4 seconds from the above plot. From the Bode plot above, we see that increasing the gain will increase the crossover frequency and thus make the response faster. We will increase the gain and see if we can get a better response. Go back to your m-file and change numc to numc=4*conv([T1 1],[T1 1]). Rerun the m-file and you should get the following plot:

From this plot we can see that the percent overshoot is about 0.15 mm less than the previous plot's and the settling time also less than 5 seconds. This response is now satisfactory and no more design iteration is needed.

Frequency Response Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php cho bi ton con lc ngc dng p ng tn s


Biu din h h p ng ca h kn khi cha c khu b p ng ca h kn khi c khu b iu g xy ra vi v tr xe?
Hm truyn khu chp hnh ca v d ny nh sau:

y,

Ch : Hm truyn ny c s c lc 1 cp im cc/khng . Trong cc v d trc chng c rt ra khi hm truyn. Tuy nhin, trong v d ny chng c chuyn vo vi l do ta s bn sau. Mc tiu thit k h iu khin (vi iu kin ban u con lc nhn c 1 xung lc 1N t xe) l: Thi gian n nh nh hn 5 giy. Con lc khng c quay gc qu0,05 radian so vi phng thng ng. bit cch thit lp bi ton nh th no, tham kho phn m hnh con lc ngc. Ch : Trc khi i vo gii quyt vn , cn ch rng gii quyt bi ton ny bng phng php p ng tn s l mt vn phc tp. Bn s d nhn thy bi v d ny c 1 im cc nm na bn tri mt phng to lm cho h h mt n nh. Phng php p ng tn s ch trin khai tt khi h h n nh. Vi l do nh vy nn v d ny khng phi l v d hay bn c th hc cch dng phng php p ng tn s. V d ny ch p dng cho nhng sinh vin mun hc cch gii quyt vn v p ng tn s phc tp m thi.

Biu din h h
1

Hm truyn thu c t php bin i Laplace vi u ra phi (gc quay ca con lc) c thit lp trong Matlab bng cch khai bo t s v mu s di dng cc vecto. To 1 m-file c cc lnh sau m hnh ho hm truyn:
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q 0];

q = (M+m)*(i+m*l^2)-(m*l)^2; num = [m*l/q 0 0]; den = [1 b*(i+m*l^2)/q pendulum = tf(num,den)

-(M+m)*m*g*l/q

p ng ca h kn khi cha c khu b


Chng ta s thit k 1 b iu khin vi u vo l 1 xung dng biu Nyquist (chng ta khng th dng biu Bode c v h h khng n nh). Ta bt u bng vic xem xt s khi ca h thng:

Cch tt nht c th p dng Matlab l thay i s khi sao cho ta c th d dng m hnh ho. Sp xp li so ta c s mi nh sau:

By gi ta c th bt u thit k. Trc ht, ta xem xt cc im cc v im khng ca hm truyn:


x = roots(num) y = roots(den) x = 0 0

y =

0 -5.6041 5.5651 -0.1428

Nh bit trc, ta c s c lc cp im cc-khng ti gc to , v cng c 1 im cc thc dng nm na bn phi mt phng to . Nh th c ngha l ta cn 1 ln bao quanh im -1 theo chiu ngc kim ng h ca biu Nyquist c h kn n nh (Z = P + N; P = 1, N = -1). Vi m-file sau s rt hu dng thit k b iu khin.
function[ ] = pend() %define TF num = [4.5455 0 0]; den = [1.0000 0.1818 -31.1818 -4.4545 0]; pendulum = tf(num,den); figure(1) %ask numc denc k user for controller = input('numc?.........'); = input('denc?.........'); = input('K?............');

%view compensated system bode contr = k*tf(numc,denc); loop = contr*pendulum; bode(loop) %view compensated system nyquist figure(2) subplot (2,1,1) nyquist(loop) %view compensated CL system impulse response subplot(2,1,2) sys_cl = feedback(pendulum,contr); impulse(sys_cl)

Vi m-file ny ta s xem xt biu Nyquist ca h khi cha c khu b bng cch chn t s v mu s v h s t l cho hm truyn ca b iu khin bng 1. nh lnh pend trong ca s lnh v nhp gi tr 1 cho t s, mu s v K. Ta s c cc biu sau:
numc?.........1 denc?.........1 K?............1

Ch : Biu Bode trong version Matlab c th khc so vi biu Bode trong phn ny 360o. Lch pha 360o khng lm thay i quan h gia biu Bode v p ng ng lc hc ca h thng.

p ng ca h kn khi c khu b
H kn khng n nh (v khng biu khng bao im -1 ln no). Bc u tin cn lm l thm 1 khu tch phn kh im khng ph thm gc ta ( ta s c 2 cc v 2 im khng ti gc to ). Ta li dng lnh pend.

numc?.........1 denc?.........[1 0] K?............1

Ta thy biu Nyquist bao im -1 thun chiu kim ng h. By gi ta c hai cc na bn phi mt phng to (Z= P + N = 1 + 1). Ta cn tng thm pha c c ng bao ngc chiu kim ng h. Ta s thc hin iu ny bng cch thm 1 im khng vo b iu khin ca ta. bt u, ta chn im khng ti im -1.
numc?.........[1 1] denc?.........[1 0] K?............1

nh ta thy, lch pha vn cha . Biu vn bao im -1 theo chiu kim ng h. Chng ta cn thm 1 im khng na.
numc?.........conv([1 1],[1 1]) denc?.........[1 0] K?............1

Ta vn thy biu bao im -1 thun chiu kim ng h. Tuy nhin, nu ta tng h s ln 1 cht, ta c th lm cho h thng n nh n nh bng cch dch biu Nyquist v pha tri bao im -1 ngc chiu kim ng h, cho N = -1.
numc?.........conv([1 1],[1 1]) denc?.........[1 0] K?............10

Nh ta c th thy, h thng by gi n nh. By gi ta ch cn tp trung vo ci thin cht lng ca p ng. Ta s iu chnh li cc cc ca b iu khin. Ta nn ghi nh rng cc cc gi tr nh ( gn gc to ) s nh hng ti p ng ca h thng ti cc tn s nh, trong khi cc cc c gi tr ln ( xa gc to ) s nh hng ti p ng ca h thng ti cc tn s cao. Khi thit k thng qua o ng tn s, chng ta c gng cho ra c nhng biu n gin, v nh th th s thc hin d dng hn c th t c mc tiu mong mun. Do vy, ta s dng tng 'lm phng' p ng tn s (biu Bode). Cng lc, ta cng ch rng biu Nyquist s c dng oval. Nu ta th dng s phi hp gia cc cc v im khng khc, ta c th c nhng p ng rt hp l. (Hy gi lnh axis([0 5 -0.05 0.1]) sau khi thc hin lnh pend)
numc?.........conv([1 1.1],[1 5]) denc?.........[1 0]

K?............10

p ng t yu cu ra. Hy t mnh chn cc tham s khc nhau v xem iu g xy ra.

iu g xy ra vi v tr xe?
Ngay t u ca phn ny, s khi h thng c trnh by. Tuy nhin s khi khng hon ton hon thin. Khi biu din cho v tr ca xe b b qua v n khng phi l mc tiu iu khin ca ta. S rt hay nu ta xem xt iu g xy ra vi v tr xe khi b iu khin ang iu khin gc ca con lc. xem xt vn , ta quan tm ti s khi h thng nh sau:

Sp xp li 1 cht, ta s c s khi nh sau:

Vng phn hi kn chnh l b iu khin m ta thit k lc trc cho iu khin gc quay ca con lc. Hm truyn cho v tr xe vi iu kin u vo ban u l 1 xung lc v b iu khin thit k trc nh sau:

Hm truyn
By gi ta c hm truyn cho ton b h thng, hy xem p ng ca n nh th no. Trc ht ta tnh hm truyn cho v tr xe. c iu ny ta thc hin php bin i Laplace cho h phng trnh ca h thng v tm hm truyn t U(s) ti X(s). Di y l hm truyn :

y,

bit thm v php bin i Laplace tham kho phn m hnh con lc ngc. By gi, to 1 m-file v chy trong ca s lnh ca Matlab:
10

M m b i g l

= = = = = =

.5; 0.2; 0.1; 0.006; 9.8; 0.3; %simplifies input -b*m*g*l/q 0];

q = (M+m)*(i+m*l^2)-(m*l)^2; num1 = [m*l/q 0 0]; den1 = [1 b*(i+m*l^2)/q G1 = tf(num1,den1); num2 = [(i+m*l^2)/q den2 = den1; G2 = tf(num2,den2); 0

-(M+m)*m*g*l/q -m*g*l/q];

k = 10; numc = conv([1 1.1],[1 5]); denc = [1 0]; contr = tf(k*numc,denc); sys_cl = feedback(1,contr*G1)*G2; sys_cl = minreal(sys_cl); t=0:0.01:100; impulse(sys_cl,t)

Lnh minreal ch ra cho Matlab c lc i tt c cc cc v im khng ca hm truyn h kn; iu ny lm cho hm impulse c tnh cht s ho tt hn. Nh ta thy, xe di chuyn theo chiu m sau n nh ti v tr khong -0,18m. Thit k ca ta c th hot ng tt khi p dng b iu khin trong thc t, gi s rng xe cn c khong khng ln di chuyn th l iu khng may mn. Chng ta c gng thit k sao cho v tr ca xe c n nh v trn thc t nhng g chng ta c l may mn.

Frequency Response Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

11

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

12

Example: Frequency Response Design method for the Pitch Controller


Open-loop response Lead compensator Lag compensator
In the Pitch Controller Modeling page, the transfer function was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the pitch angle (theta). The design requirements are Overshoot: Less than 10% Rise time: Less than 5 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2% To see the original problem setup, please refer to the Pitch Controller Modeling page.

Open-loop response
Recall from your control textbook that the frequency response design method is most effective for systems with stable open-loop. To check the open-loop stability of our system, create a new m-file, and enter the following commands. Running this m-file in the MATLAB command window should give you the step response shown below:
de = 0.2; num = [1.151 0.1774]; den = [1 0.739 0.921 0]; pitch = tf(num,den); step(de*pitch)

Unfortunately, our system is unstable in open-loop; however, we can still design the feedback system via frequency response method (even though this might not be the easiest way). First, let's generate the open-loop Bode plot and see what it looks like. Change the m-file to the following and re-run it in the MATLAB command window. You should see a Bode plot similar to the one shown below:
num = [1.151 0.1774]; den = [1 0.739 0.921 0]; pitch = tf(num,den); bode(pitch)

From our design requirements, we can determine that the natural frequency (Wn) must be greater than 0.9 and the damping ratio (zeta) must be greater than 0.52 (please refer to the Pitch Controller: Root-Locus method for details). Using two equations shown below, we see that the bandwidth frequency and the phase margin must be greater than 0.9 and 52 degrees, respectively.

Tr = Rise time Wn = Natural frequency BW = Bandwidth frequency zeta = Damping ratio PM = Phase margin Currently, we have a bandwidth frequency of 1 rad/sec and a phase margin of 80 degrees. These values are within our desired region. Let's plot the closed-loop step response and see what it looks like. Delete the bode command from the above m-file and add the following commands. Running this new m-file should give you the following closed-loop step response:
sys_cl = feedback(pitch,1); de = 0.2; t = 0:0.01:10; step(de*sys_cl,t)

As you can see, the transient response is worse, there is a long settling time. We will implement a lead compensator to improve the system response.

Lead Compensator
Referring to the "Lead or phase-lead compensator using frequency response" section of Lead and Lag Compensator page, a lead compensator will add a positive phase to the system. An additional positive phase increases the phase margin; thus, increase damping. The settling time should decrease as a result of this increased damping. The transfer function of a typical first-order lead compensator is

We need to find alead, Tlead and Klead. First, the phase margin requirement and the following equation can be used to find alead

Since we are required to have the phase margin of greater than 52 degrees, the alead must be greater than 8.43. Using this alead, the bandwidth frequency requirement of greater than 0.9 and the following equation leads us to have the Tlead of smaller than 0.382.

Let the Klead equal 0.1, alead equal 10, and Tlead equal 0.3 for now and enter the following commands to an new m-file.
num = [1 151 0.1774]; den = [1 0.739 0.921 0]; pitch = tf(num,den); alead = 10; Tlead = 0.3; k = 0.1; lead = tf(k*[alead*Tlead 1],[Tlead 1]); bode(lead*pitch) sys_cl = feedback(lead*pitch,1); de = 0.2; t = 0:0.01:10; figure step (de*sys_cl,t)

Running this m-file in the MATLAB command window gives you the following Bode and step response plots.

Although both the bandwidth frequency and phase margin have increased, the response still does not satisfy the design requirements. Let's increase alead and decrease Tlead. After several trial and error runs, an alead of 200, Tlead of 0.0025, and Klead of 0.05 , were found which gave the following lead compensator,

providing the desired transient response. To see the step response and the corresponding Bode plot, enter the following commands to an m-file and run it in the command window. You should see both the Bode plot and the step response shown below:
num = [1 151 0.1774]; den = [1 0.739 0.921 0]; pitch = tf(num,den); alead = 200; Tlead = 0.0025; k = 0.1; lead = tf(k*[alead*Tlead 1],[Tlead 1]); bode(lead*pitch) sys_cl = feedback(lead*pitch,1); de = 0.2; t = 0:0.01:10; figure step (de*sys_cl,t)

If you compare the above Bode plot to the original Bode plot, you see both the phase margin and the bandwidth frequency have increased. Increasing both of them improves the rise time, the overshoot, and the settling time, as seen in the above step response plot. To improve the steady-state error, we will add a lag compensator to the system.

Lag compensator
Referring to the "Lag or phase-lag Compensator using frequency response" section of Lead and Lag Compensator page, a lag compensator reduces the steady-state error. The typical first-order transfer function of a lead compensator is

The steady-state error will be reduced by a factor of alag. From the above step response, we see that the steady-state error is roughly 10%. Thus, alag needs to be approximately 0.1. The Tlag should be greater than alag*Tlag because this compensator must not greatly change the transient response.
6

After several trial and error runs, an alag of 0.1, Tlag of 20, and Klag of 1.5, were found which gave the following lag compensator,

provided the desired response. To see the step response and the corresponding Bode plot, enter the following commands to an new m-file. Running this m-file in the command window should give you the two plots shown below:
num = [1 151 0.1774]; den = [1 0.739 0.921 0]; pitch = tf(num,den); alead = 200; Tlead = 0.0025; k = 0.05; lead = tf(k*[alead*Tlead 1],[Tlead 1]); Tlag = 20; alag = 0.1; at = alag*Tlag; k2 = 1.5; numlag = k2/alag*[at 1]; denlag = [Tlag 1]; lag = tf(numlag,denlag); bode(lag*lead*pitch) sys_cl = feedback(lag*lead*pitch,1); figure t = 0:0.01:10; step(0.2*sys_cl,t)

If you see the Bode plot, the low frequency gain has increased while keeping the bandwidth frequency the same. This tells us that steady-state error has reduced while keeping the same rise time. The above step response shows that the steady-state error is acceptable. Now all design requirements are satisfied.

Frequency Response Examples Pitch Control Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Control | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php cho bi ton Bng & Thanh dng p ng tn s


Biu Bode ca h h B iu khin Phase-Lead Tng thm pha
Hm truyn h khu chp hnh cho bi ton bng v thanh nh sau:

Mc tiu iu khin l: Thi gian n nh nh hn 3 giy qu iu chnh nh hn 5% bit cc phng trnh ca h xut pht t u, tham kho phn m hnh bng v thanh . S h kn vi b iu khin nh sau:

Biu Bode ca h h
tng chnh ca phng php thit k theo tn s l dng biu Bode ca h h c on p ng ca h kn. Bng cch thm vo 1 b iu khin thay i biu Bode ca h h t thay i p ng ca h kn. Trc ht ta hy v biu Bode cho h h ban u. To 1 m-file vi cc lnh sau v chy trong ca s lnh ca Matlab:
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0];

ball=tf(num,den); bode(ball)

You should get the following Bode plot:

T biu Bode trn ta thy bin ca pha bng 0. V bin ca pha c nh ngha l s thay i pha cn thit trong h h h kn khng n nh, iu ny ngha l vi bin pha bng 0 th h l khng n nh. Chng ta cn tng bin pha ln v ta c th dng 1 b iu khin b Lead thc hin. bit r hn v Bin h s v pha tham kho phn p ng tn s.

B iu khin Pha-Lead
Khu b phase-lead loi 1 c dng nh sau:

Khu b Pha-Lead s thm 1 pha dng cho h thng trn di tn s t 1/aT n 1/T, gi l nhng tn s gc. Gi tr pha thm vo nhiu nht l 90o. Ta cn thit k qu iu chnh nh hn 5 %, tng ng vi zeta (h s gim chn) bng 0,7. Nhn zeta * 100 s cho ta gi tr nh nht m bin ca pha cn c t qu iu chnh mong mun. Nh vy, ta cn bin ca pha t nht l 70o. c "T" v "a", cn thc hin theo cc bc sau. 1. Xc nh pha dng cn thit l bao nhiu: Vi b iu khin ta cn t nht l 70o. 2. Xc inh tn s m ta thc hin thm pha ti (tn s trung tm): Trong trng hp ny l kh kh xc nh bi biu tn s-pha hin gio l mt ng thng. Tuy nhin, ta li c lin h gia di bng tn (wbw) v thi gian n nh (tham kho phn Di bng tn bit phng trnh lin h) v phng trnh ny gip ta tm c wbw xp x 1.92
2

rad/s. Do vy ta chn tn s trung tm trc wbw. Tm thi chn bng 1. 3. Xc nh hng s "a" t phng trnh bn di, hng s ny cho bit khong cch cn thit gia cc v im khng ca pha thm ln nht.

y phi l bin pha cn c. Vi phi bng 70 , a = 0.0311. 4. Xc nh "T" v "aT" t phng trnh sau:

Vi phi = 70 v tn s trung tm w = 1, ta c aT = 0.176 v T = 5.67 By gi, ta c th thm b iu khin Lead cho h thng v xem xt biu Bode. B lnh bode trong m-file v thay bng cc lnh sau:
phi=70*pi/180; a=(1-sin(phi))/(1+sin(phi)); w=1; T=1/(w*sqrt(a)); k = 1; numlead = k*[T 1]; denlead = [a*T 1]; contr = tf(numlead,denlead); bode(contr*ball)

Ta c biu sau:

Ta c th thy rng bin ca pha by gi l 70 . Hy kim tra p ng ca h kn by gio ra sao vi u vo tn hiu bc thang 0,25m. Thm cc lnh sau vo m-file:
sys_cl = feedback(contr*ball,1); t = 0:0.01:5; step(0.25*sys_cl,t)

Ta c biu sau:

Mc d h thng n nh v qu iu chnh khong 5%, nhng thi gian n nh khng tho mn. Tng h s s tng gi tr tn s ct ngang v lm cho p ng ca h thng nhanh hn. Vi k = 5, p ng ca ta s nh sau:

p ng nhanh hn, tuy nhin, qu iu chnh li qu cao. Tng h s thm na s lm cho qu iu chnh cng xu.

Tng thm pha


Ta c th tng khu b pha-lead gim qu iu chnh. lm cho cng vic n gin hn ta dng chng trnh sau. To 1 m-file v copy hm sau vo m-file.
function[ ] = phaseball() %define TF m = 0.111; R = 0.015; g = -9.8; L = 1.0; d = 0.03; J = 9.99e-6; 5

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; ball = tf(num,den); %ask pm = w = k =

%simplifies input

user for controller information input('Phase Margin?.......'); input('Center Frequency?...'); input('Gain?...............');

%view compensated system bode plot pmr = pm*pi/180; a = (1 - sin(pmr))/(1+sin(pmr)); T = sqrt(a)/w; aT = 1/(w*sqrt(a)); numlead = k*[aT 1]; denlead = [T 1]; contr = tf(numlead,denlead); figure bode(contr*ball) %view step response sys_cl = feedback(contr*ball,1); t = 0:0.01:5; figure step(0.25*sys_cl,t)

Vi m-file ny bn c th chn bin ca pha, tn s trung tm v h s. Chy your m-file vi cc tham s sau v ta s c biu sau.
Phase Margin?.......80 Center Frequency?...1 Gain?...............1

qu iu chnh t, nhng thi gian n nh li lu. Th cc gi tr khc v xem iu g xy ra. Vi cc gi tr tham s sau mc tiu ra s t c.
Phase Margin?.......85 Center Frequency?...1.9 Gain?...............2

Ch : Mt vn thit k khng ch c 1 li gii p duy nht. Vi phng php ny (hoc cc phng php khc) c th cho ra cc khu b khc nhau. t thc hnh, bn c th chn li pha, h s hoc tn s trung tm ri xem xt cc p ng.

Frequency Response Examples Ball & Beam Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Khng gian trng thi (State Space)


Phng trnh khng gian trng thi Thit k h iu khin bng cch gn im cc Introducing the reference input Observer design
Cc t kho lnh Matlab trong phn ny l: acker, lsim, place, plot, rscale

Phng trnh khng gian trng thi


C nhiu cch khc nhau biu din 1 h thng bng cc phng trnh vi phn. Biu din qua khng gian trng thi c dng nh sau:

vi x l vecto n x 1 biu din trng thi (thng l v tr v vn tc ca h c kh), u l i lng v hng biu din cho u vo (thng l lc hoc momen xon trong h c kh), v y l i lng v hng biu din cho u ra. Cc ma trn A (n x n), B (n x 1), v C (1 x n) xc nh mi lin h gia trng thi, u vo v u ra ca h thng. Lu l y ta c n phng trnh vi ph cp1. Khng gian trng thi c th biu din c h thng vi nhiu u vo ra (MIMO), nhng ta ch xt cc h thng 1 u vo, 1 u ra (SISO) trong phn ny. gii thiu phng php thit k theo khng gian trng thi, chng ti s dng m hnh treo qu bng bng t trng lm v d. Dng in trong cun dy sinh ra 1 lc t cn bng vi trng lc v lm cho qu bng (lm bng vt liu t) l lng trong khng kh. M hnh ny c trong rt nhiu sch in v iu khin (bao gm c cun Automatic Control Systems ca B. C. Kuo, ti bn ln th 7). Phng trnh ca h thng ny c a ra nh sau:

y h l v tr theo phng thng ng ca qu bng, i l dng in chy qua nam chm in, V l in p c t vo, M l khi lng ca bng, g gia tc trng trng, L l t cm ca cun dy,

R in tr, v K l h s xc nh tc dng ca lc t ln bng. n gin, ta chn M = 0,05 Kg, K = 0,0001, L = 0,01 H, R = 1 m, g = 9,81 m/s2 . H thng t v tr cn bng (bng l lng trong khng kh) khi no h = K i2/Mg (ti im ny dh/dt = 0). Chng ta tuyn tnh ho cc phng trnh xung quanh h = 0,01 m ( dng in danh ngha khong 7 ampe) v c phng trnh khng gian trng thi nh sau:

vi:

l tp cc bin trng thi ca h thng (vecto 3x1), u l in o u vo (denta V), v y

(u ra) l denta h. Nhp cc ma trn h thng sau vo 1 m-file.


A = [ 0 1 980 0 0 0 B = [0 0 100]; C = [1 0 0 -2.8 -100];

0];

Mt trong nhng iu u tin ta mun lm vi cc phng trnh trng thi l tm cc cc ca h thng; l nhng gi tr ca s m det(sI - A) = 0, hoc chnh l cc gi tr ring ca ma trn A:
poles = eig(A)

Ta s c 3 cc sau:
poles = 31.3050 -31.3050 -100.0000

Mt trong s cc cc nm pha bn phi mt phng to , c ngha l h h khng n nh. kim tra xem iu g xy ra vi h thng khng n nh ny khi iu kin ban u khc khng, thm cc lnh sau vo m-file,
t = 0:0.01:2; u = 0*t; x0 = [0.005 0 0]; sys = ss(A,B,C,0); [y,t,x] = lsim(sys,u,t,x0); plot(t,y)

v cho chy li.

C v nh khong cch gia bng v nam chm s tin ra v cng, nhng c th bng s chm vo bn hoc mt t trc (v chc chn ra khi phm vi tuyn tnh ho).

Thit k h iu khin bng cch gn im cc


Ta hy xy dng 1 b iu kin cho h thng ny. S c phn hi y cc trng thi ca h thng nh sau:

Nhc li rng a thc c trng ca h kn l nh thc ca (sI-(A-BK)). V cc ma trn A v B*K u l ma trn 3 x 3, nn h thng c 3 cc. Bng cch phn hi y cc trng thi ta c th gn cc cc mi v tr m ta mun. Ta c th s dng lnh place trong Matlab tm ma trn iu khin K, ma trn s cho ta cc cc nh mong mun. Trc khi thc hin phng php ny, chng ta phi quyt nh c v tr no t cc ca h kn. Gi s mc tiu iu khin l thi gian n nh <0,5 giy v qu iu chnh < 5%, chng ta c th c gng t hai cc chnh ti -10 +/- 10i (zeta = 0.7 hay l 45o vi sigma = 10 > 4.6*2). Cc th 3 lc u ta c th t ti im -50, v ta c th thay i n sau ph thuc vo hnh vi ca ht thng ra sao. B lnh lsim trong m-file v cc lnh sau na ri thm cc lnh sau vo m-file,
p1 = -10 + 10i; p2 = -10 - 10i; p3 = -50;

K = place(A,B,[p1 p2 p3]); sys_cl=ss(A-B*K,B,C,0); lsim(sys_cl,u,t,x0);

qu iu chnh qu ln (nh vy l c 1 im khng trong hm truyn v c th n lm tng qu iu chnh, ta khng th thy im khng trong cng thc khng gian trng thi). Th t cc cc ra xa hn v pha bn tri xem kh nng p ng tc thi c c vi thin hay khng (iu ny c ngha l lm cho p ng nhanh hn).
p1 = -20 + 20i; p2 = -20 - 20i; p3 = -100; K = place(A,B,[p1 p2 p3]); sys_cl = ss(A-B*K,B,C,0); lsim(sys_cl,u,t,x0);

Ln ny qu iu chnh nh hn. Tham kho sch in c nhiu li khuyn hn na v cch chn cc im cc mong mun.

Hy so snh kh nng iu khin mong mun ca (K) trong c hai trng hp trn.. Nhn chung, khi ta cng t im cc ra xa th kh nng iu khin ca (K) cng tt ln. Ch : Nu ta mun t hai hoc nhiu cc ti cng 1 v tr, lnh place s khng thc hin c. Khi ta c th dng lnh acker tng t nh lnh place thc hin:
K = acker(A,B,[p1 p2 p3])

Introducing the reference input


By gi ta s ly h iu khin nh nh ngha trn v p dng vi u vo tn hiu bc thang (ta ch chn gi tr tn hiu bc thang nh tho mn iu kin nm trong vng tuyn tnh ha). Thay t,u v lsim trong m-file li nh sau,
t = 0:0.01:2; u = 0.001*ones(size(t)); sys_cl = ss(A-B*K,B,C,0); lsim(sys_cl,u,t)

Ta tht h thng khng bm tn hiu u vo mt cht no; khng ch bin khc 1 m cn c gi tr m thay v phi dng! Xem li s khi bn trn ta thy rng ta khng so snh u ra vi tn hiu reference; thay vo ta o tt c cc trng thi, nhn vi vecto h s K, v ly tn hiu reference tr i kt qu . Khng c c s no khng nh rng K*x s bng u ra mong mun. gii quyt vn ny, ta c th ly t l u vo reference sao cho n bng K*x_trng thi tnh. H s t l ny thng oc g l Nbar; n c a ra di dng nh s sau:

Ta c th c c Nbar bng cch dng hm rscale trong Matlab(t lnh ny sau lnh K = ...).
Nbar=rscale(sys,K)

Ch hm rscale khng phi l 1 hm chun trong MATLAB. dng c bn phi copy n trong 1 m-file. Kch vo y bit chi tit v cch dng hm ny trong MATLAB. By gi nu ta mun tm p ng ca h thng vi phn hi trng thi v tn hiu u vo reference nh ni trn, v ta cng ch quan nim n gin l u vo c nhn vi h s Nbar:
lsim(sys_cl,Nbar*u,t)

v ta c th thy u vo c h thng bm rt tt.

Observer design
Khi chng ta khng th o c tt c cc trng thi ca x (y l trng hp hay gp), ta c th to 1 khu quan st c lng chng trong lc ch cn o u ra y = C x. Vi v d bng t tnh, ta s thm mi ba trng thi c lng vo h thng. S khi h thng s nh sau:

Khu quan st c bn l 1 bn sao ca khu chp hnh; chng c cng u vo v cc phng trnh vi phn hu nh ging nhau. Mt thut ng c bit so snh u ra o thc t y v u ra c lng ; iu ny s sinh ra 1 trng thi c lung tnh xp x gi tr trng thi thc t x. Sai s ng lc hc ca khu quan st c quy nh bi cc cc ca (A-L*C). Trc ht ta cn chn h s ca khu quan st L. V chng ta mun ng lc hc ca khu quan st nhanh hn so vi bn thn h thng nn ta cn t cc cc t nht phi xa v pha bn tri gp 5 ln so vi cc cc chnh. Mun th ta dng lnh place, v cn phi t cc cc v tr khc nhau.
op1 = -100; op2 = -101; op3 = -102;

V c s i ngu gia tnh iu khin c v quan st c, chng ta c th dng cng k thut tm ma trn iu khin, nhng ta thay ma trn B bi ma trn C v ly chuyn v ca tng ma trn (tham kho sch in bit ngun gc vn ):
L = place(A',C',[op1 op2 op3])';

Cc phng trnh trong s khi h thng trn c a ra cho . Thng thng hay vit cc phng trnh lin h cho h thng v khu chp hnh dng trng thi gc x v thm sai s trng thi: e =x. Chng ta dng 1 trng thi phn hi u = -K . Sau 1 s php bin i i s (tham kho sch in), chng ta c phng trnh lin kt trng thi v sai s vi tt c cc trng thi phn hi v mt khu quan st:
At = [A - B*K B*K zeros(size(A)) A - L*C]; Bt = [ B*Nbar zeros(size(B))]; Ct = [ C zeros(size(C))];

xem p ng ca h thng ra sao vi iu kin u khc khng v khng c tn hiu reference, thm cc lnh sau vo m-file. Chng ta gi s khu quan st c iu kin u bng 0, =0. iu ny cho chng ta iu kin u ca sai s bng iu kin u ca trng thi.
sys=ss(At,Bt,Ct,0); lsim(sys,zeros(size(t)),t,[x0 x0])

p ng ca cc trng thi c v theo biu bn di. Nh li rng lnh lsim cho chng ta x v e; c ta phi ly hiu x-e.

Phng to nhn chi tit:

ng nt lin mu xanh da tri l p ng ca v tr bng trng thi c lng ;


8

, ng nt t mu xanh da tri l

ng nt lin mu xanh l cy l p ng ca tc bng , ng nt t mu xanh l ng trng thi c lng ; ng nt lin mu l p ng ca dng in , ng nt t mu l ng trng tho c lng . Chng ta c th thy rng khu quan st c lng cc trng thi nhanh v bm trng thi xc lp kh tt. Cc biu trn v bng lnh plot nh sau:
[y,t,x] = lsim(sys,zeros(size(t)),t,[x0 x0]); plot(t,x) axis([0,.3,-.2,.2])

Cc v d v khng gian trng thi Tutorials

iu khin xe | iu khin tc ng c | iu khin v tr ng c | Bus Suspension | iu khin con lc ngc | Pitch Controller | Bi ton bng v thanh

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: Gii php iu khin xe s dng khng gian trng thi


Thit k iu khin bng cch gn im cc u vo Reference
Phng trnh trng thi ca v d ny l:

y m=1000 kg b=50 N*sec/kg u=500 N v=velocity y=output Mc tiu iu khin l: Thi gian qu < 5 sec qu iu chnh < 10% Sai s xc lp < 2% bit cch thit lp m hnh gc, tham kho phn Cruise Control Modeling.

Thit k iu khin bng cch gn im cc


S khi h thng phn hi y cc trng thi nh sau.

y K=Ma trn iu khin U=-Kv=u vo R=Reference Nhc li phn Khng gian trng thi, chng ta s s dng k thut "gn im cc" t c u ra
1

mong mun. Cc cc ca h kn c th tm c t phng trnh c tnh: nh thc ca ma trn [sI-(A-B*K)] . Nu cc cc c t vo h thng bng cch thit k cho chnh ma trn iu khin K, th c th t c u ra mong mun. Trong phn ny, cc cc s c chn trc, sau ta dng Matlab tm ma trn iu khin K tng ng. By gi ta cn xc nh ni t im cc. V ma trn [sI-(A-B*K)] l ma trn 1x1, nn ta ch gn c 1 cc. Ta cho cc ti im -1.5 (tu ). Cng ging nh trong phn Khng Gian Trng Thi, hm place trong Matlab c dng tm ma trn iu khin K. To 1 m-file c cc lnh sau.
m b t u = = = = 1000; 50; 0:0.1:10; 500*ones(size(t));

A = [-b/m]; B = [1/m]; C = [1]; D = [0]; cruise = ss(A,B,C,D); x0 = [0]; p1 = -1.5; K=place(A,B,[p1]) sys_cl=ss(A-B*K,B,C,D); lsim(sys_cl,u,t,x0);

Chy m-file trong ca s lnh ca Matlab ta s c ma trn iu khin v p ng vi u vo tn hiu bc thang nh sau.

Nh ta thy, thi gian qu tho mn nhng sai s xc lp li qu ln.

u vo Reference
Mt ln na tham kho t phn Khng gian trng thi, h s t l Nbar (nh s khi bn di) c dng kh sai s xc lp. Ta c th dng hm rscale tnh h s t l ny. u vo c nhn ln 500 ln, v ta mun vn tc n nh gi tr 10 m/sec, nn ta cng phi tnh c n cc h s
2

Copy cc lnh sau vo m-file v chy n trong ca s lnh ca Matlab. Ta s c p ng nh sau. Ta s c p ng nh sau.
m=1000; b=50; t=0:0.1:10; u=500*ones(size(t)); A=[-b/m]; B=[1/m]; C=[1]; D=[0]; x0=[0]; p1=-1.5 K=place(A,B,[p1]); Nbar=rscale(cruise,k)*10/500; sys_cl=ss(A-B*K,B*Nbar,C,D); lsim(sys_cl,u,t,x0);

Nh ta thy, sai s xc lp b kh. Thi gian qu nh hn 5 giy v qu iu chnh trn thc t l bng 0. Tt c cc yu cu ra hon thnh.

State Space Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch
3

Controller | Ball and Beam

Cruise Control Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

V d: B iu khin khng gian trng thi cho tc ng c 1 chiu


Thit k b iu khin vi phn hi y cc trng thi Thm u vo reference
T bi ton gc, phng trnh ng lc hc ca h biu din trong khng gian trng thi c dng nh sau:

bit bi ton gc thit lp ra sao v cc h qu ca phng trnh trn, tham kho phn M hnh ng c 1 chiu. Vi tn hiu reference 1 rad/sec a vo h thng, mc tiu thit k t ra l: Thi gian qu nh hn 2 giy qu iu chnh nh hn 5% Sai s xc lp nh hn 1% To 1 m-file c cc lnh sau (tham kho phn bi ton gc xem chi tit).
J b K R L A = = = = = = 0.01; 0.1; 0.01; 1; 0.5; [-b/J K/J -K/L -R/L]; [0 1/L]; [1 0]; 0; = ss(A,B,C,D);

B = C = D = sys

Thit k b iu khin vi phn hi y cc trng thi


V c hai bin trng thi trong v d ny u d dng o c (ch cn thm mt ampe k o dng in v tc k o tc ), nn ta c th thit k b iu khin vi phn hi y cc trng thi cho h thng m khng cn phi lo n vic thm vo khu quan st. S khi cho h vi phn hi y cc trng thi l:

Nh li rng a thc c tnh ca h kn l nh thc ca sI-(A-BK) vi s l bin ton t Laplace. V cc ma trn A v B*K u l ma trn 2x2, nn h thng s c 2 cc. Bng cch thit k b iu khin vi phn hi y cc trng thi, chng ta c th t hai cc ny vo bt c u ta mun. Trc tin ta th t chng ti -5 + i v -5-i (ch rng iu ny tng ng vi zeta = 0.98 cho ta qu iu chnh 0.1% v sigma = 5 s cho ta thi gian n nh l 1 giy). Chng ta quay li vi cc cc m ta mong mun, MATLAB s tm cho ta ma trn iu khin K cho chng ta. Mt cch n gin thm cc lnh sau vo m-file :
p1 = -5 + i; p2 = -5 - i; K = place(A,B,[p1 p2]);

By gi hy nhn vo s khi pha trn. Ta thy rng sau khi thm ma trn iu khin K vo h thng, phng trnh khng gian trng thi by gi l:

Ta c th xem c p ng ca h kn bng cch thm cc lnh sau vo m-file:


t = 0:0.01:3; sys_cl = ss(A-B*K,B,C,D); step(sys_cl,t)

Chy m-file trong ca s lnh, ta s c biu sau:

Thm u vo reference
T biu trn ta thy sai s xc lp qu ln. i chiu vi cc phng php thit k khc, ta phn hi li u ra v so snh n vi u vo reference tnh ra sai lch, y ta phn hi c hai trng thi. Chng ta cn tnh ton xem sai s xc lp l bao nhiu, sau nhn vi h s chn K, v dng gi tr mi ny lm reference cho vic tnh ton u vo. iu ny c th thc hin bng cch thm h s hng Nbar after the reference:

We can find this Nbar factor by using the MATLAB command rscale:
Nbar=rscale(sys,K)

Note that the function rscale is not a standard function in MATLAB. You will have to copy it before you use it. Click here for more information. Now we can plot the step response by adding the following line of code to your m-file:
t = 0:0.01:10; step(sys*Nbar,t) title('Step Response with K Controller and Nbar')

This time, the steady-state error is much less than 1%, and all the other design criteria have been met as well.

State Space Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Speed Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: A State-Space Controller for DC Motor Position Control


Designing the full-state feedback controller Disturbance Response Adding Integral Action
From the main problem, the dynamic equations in state-space form are the following:

For the original problem setup and the derivation of the above equations, please refer to the Modeling DC Motor Position page. With a 1 rad reference added to the system, the design criteria are: Settling time less than 0.04 seconds Overshoot less than 16% Zero steady-state error to a step input Zero steady-state error to a step disturbance Create a new m-file and type in the following commands (refer to the main problem for the details of getting those commands).
J b K R L = = = = = 3.2284E-6; 3.5077E-6; 0.0274; 4; 2.75E-6;

A = [0 1 0 0 -b/J K/J 0 -K/L -R/L]; B = [0 ; 0 ; 1/L]; C = [1 0 0]; D = [0]; sys=ss(A,B,C,D);

Designing the full-state feedback controller


Since all of the state variab in our problem are very easy to measure (simply add an ammeter for current, a tachometer for speed, and a potentiometer for position), we can design a full-state feedback

controller for the system without worrying about having to add an observer. The schematic for a full-state feedback system is:

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-BKc)) where s is the Laplace variable. Since the matrices A and B*Kc are both 3x3 matrices, there should be 3 poles for the system. By designing a full-state feedback controller, we can move these three poles anywhere we want them. We shall first try to place them at -100 + 100i and -100-100i (note that this corresponds to a zeta = 0.5 which gives 0.16% overshoot and a sigma = 100 which leads to a .04 sec settling time). Once we come up with the poles we want, MATLAB will find the controller matrix,Kc, for us. Simply add the following code to the end of your m-file :
p1 p2 p3 Kc = = = = -100+100i; -100-100i; -200; place(A,B,[p1,p2,p3]);

Now look at the schematic above again. We see that after adding the K matrix into the system, the state-space equations become:

We can see the closed-loop response by simply adding the following line to the end of your m-file:
t = 0:0.001:.05; sys_cl=ss(A-B*Kc,B,C,D); step(sys_cl,t)

Run your m-file in the command window, You should see the following plot:

Disturbance Response
In order to get the disturbance response, we must provide the proper input to the system. Physically, a disturbance is a torque which acts on the inertia of the motor. A torque acts as an additive term in the second state equation (which gets divided by J, as do all the other terms in this equation). We can simulate this simply by modifying our closed loop input matrix, B, to have a 1/J in the second row. Add the following line to your m-file and re-run.
dist_cl=ss(A-B*Kc,[0;1/J;0],C,D); step(dist_cl,t)

This is not a zero steady-state error to a disturbance, and we will have to compensate for this.

Adding Integral Action


We know that if we put an extra integrator in series with the plant it can remove steady-state error to an input. If the integrator comes before the injection of the disturbance, it will cancel the disturbance in steady state. This changes our control structure so it now resembles the following:

We can model the integrator by augmenting our state equations with an extra state which is the integral of the output. This adds an extra equation which states that the derivative of the integral of theta is theta. This equation will be placed at the top of our matrices. The input, r, now enters the system before the integrator, so it appears in the newly added top equation. The output of the system remains the same.

These equations represent the dynamics of the system before the loop is closed. We will refer to the matrices in this equation as Aa, Ba, Ca, and Da. We will refer to the state vector of the augmented system as xa. Note that the reference, r, does not affect the states (except the integrator state) or the output of the plant - this is expected, since there is no path from the reference to the plant input, u, without implementing the feedback matrix, Kc. In order to find the closed loop equations, we have to look at how the input, u, affects the plant. In this case, it is exactly the same as in the unaugmented equations. Therefore, there is a vector, call it Bau, which replaces Ba when we are treating u as the input. This is just our old B vector with an extra zero added as the first row. Since u=Kc*xa is the input to the plant for the closed loop, but r is the input to the closed loop system, the closed loop equations will depend on both Bau and Ba. The closed loop equations will become:

Now, the integral of the output will be fed back, and will be used by the controller to remove steady state error to a disturbance. We can now redesign our controller. Since we need to place one closed-loop pole for each pole in the plant, we will place another pole at -300, which will be faster than the rest of the poles. Since the closed-loop system matrix depends on Bau, we will use Bau in the place command rather that Ba. Add the following to your m-file:
Aa = [0 1 0 0 0 0 1 0 0 0 -b/J K/J 0 0 -K/L -R/L]; Ba = [ -1 ; 0 ; 0 ; 0]; Bau = [0 ; 0 ; 0 ; 1/L ]; Ca = [0 1 0 0]; Da = [0]; p4 = -300; Kc = place(Aa,Bau,[p1,p2,p3,p4]); t = 0:0.001:.05; sys_cl = ss(Aa-Bau*Kc,Ba,Ca,Da); step(sys_cl,t)

Run your m-file (or just these new lines) and you will get the following output.

To look at the disturbance response, we apply a similar B matrix as we did previously when simulating the disturbance response.
dist_cl = ss(Aa-Bau*Kc,[0 ; 0 ; 1/J ; 0],Ca,Da); step(dist_cl,t)

We can see that all of the design specifications have been met by this controller.

State Space Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Position Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: A State-space Controller for a Bus Suspension System


Designing the full-state feedback controller Plotting the closed-loop response
From the main problem, the dynamic equations in state-space form are the following:

For the original problem setup and the derivation of the above equations, please refer to the Modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands). We need to define the A, B, C, D matrices by entering the following into the m-file:
m1 m2 k1 k2 b1 b2 = = = = = = 2500; 320; 80000; 500000; 350; 15020; 1 0 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) 0 -((b1/m1)+(b1/m2)+(b2/m2)) 0 -((k1/m1)+(k1/m2)+(k2/m2)) 0 (b1*b2)/(m1*m2) -(b2/m2) -(k2/m2)]; 0 -(b1/m1) 1 0];

A=[0 -(b1*b2)/(m1*m2) b2/m2 k2/m2 B=[0 1/m1 0 (1/m1)+(1/m2) C=[0 0 1 0]; D=[0 0]; sys=ss(A,B,C,D);

Designing the full-state feedback controller


First, let's design a full-state feedback controller for the system. Assuming for now that all the states can be
1

measured (this assumption is probably not true but is sufficient for this problem), the schematic of the system should be:

The characteristic polynomial for this closed-loop system is the determinant of (sI-(A-B[1,0]'K)). Note that it's not sI-(A-BK) because the controller K can only control the force input u but not the road disturbance W. Recall that our B matrix is a 4 x 2 matrix, and we only need the first column of B to control u. For this example, we have to use integral action to achieve zero steady-state error, so we add an extra state which is . In reality the bus will eventually reach an equilibrium that yields a zero steady-state error. The new states are X1, Y1, and Y2. Also the state-space matrices, A,B,and C, after adding extra state change to be the following:
Aa=[0 1 0 -(b1*b2)/(m1*m2) 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) b2/m2 0 -((b1/m1)+(b1/m2)+(b2/m2)) k2/m2 0 -((k1/m1)+(k1/m2)+(k2/m2)) 0 0 1 Ba=[0 0 1/m1 (b1*b2)/(m1*m2) 0 -(b2/m2) (1/m1)+(1/m2) -(k2/m2) 0 0]; Ca=[0 0 1 0 0]; Da=[0 0]; sys=ss(Aa,Ba,Ca,Da); 0 -(b1/m1) 1 0 0 0 0 0 0 0];

Actually, there is a shortcut for MATLAB to achieve the same result.


Aa = [[A,[0 0 0 0]'];[C, 0]]; Ba = [B;[0 0]]; Ca = [C,0]; Da = D; sys=ss(Aa,Ba,Ca,Da);

Add the above MATLAB code into the m-file. In this case, we treat the problem like a PID controller design. The integral control is obtained from the new state. The proportional control is obtained from a gain on Y1 or X1-X2. The direct derivative control of the output isn't possible, since derivative of Y1 or X1-X2 isn't a state. Instead we use the derivative of X1, which is available for feedback. (While X1 maybe hard to measure, could be obtained by integrating the output of an accelerometer mounted on the bus.) It is similar to adding more damping to velocity of oscillation of the bus. Add the following MATLAB code for controller K in the m-file:
K = [0 2.3e6 5e8 0 8e6]

We arrived at this value of the K, matrix by trial and error, adjusting the gain for derivative of X1,Y1 and

integral of Y1, as previously mentioned.

Plotting the closed-loop response


Looking at the schematic above again, we see that after adding the K matrix into the system, the state-space equations become:

We can now obtain the closed-loop response by simply adding the following code into the m-file. Note that we need to multiply B matrix by 0.1 to simulate 0.1 m high step disturbance:
t = 0:0.01:2; sys_cl = ss(Aa-Ba(:,1)*K,-0.1*Ba,Ca,Da); step(sys_cl*[0;1],t) title('Closed-loop response to a 0.1 m step')

Running the m-file in the command window, you should see the following plot:

From the plot we see that the percent overshoot and settling time requirements are satisfied. Moreover the steady-state error approaches zero as well. Therefore, we will determine that the response is satisfactory. Feel free to play around with the gain for matrix K.

State Space Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital

Control | Simulink Basics | Simulink Modeling | Examples

Example: State-space design for the inverted pendulum


Open-loop poles LQR design Adding the reference Input Observer design
The state equations for this problem are:

The design criteria for this system with the cart receiving a 0.2 m step input are as follows: Settling time for x and theta of less than 5 seconds. Rise time for x of less than 1 second. Overshoot of theta less than 20 degrees (0.35 radians). Steady-state error within 2%. As you may have noticed if you went through some of the other inverted pendulum examples, the design criteria for this example are different. In the other examples we were dealing with an impulse and not a step input. Also, we were only concerned with the pendulums angle and disregarded the cart's position in the design of the controller. However, for an inverted pendulum it is unrealistic to consider just the single output system. Using state-space methods it is relatively simple to work with a multi-output system, so in this example we will design a controller with both the pendulum angle and the cart position in mind. To see how this problem was originally set up, consult the inverted pendulum modeling page. This problem can be solved using full state feedback. The schematic of this type of control system is shown below:

If you are interested in running an animation of this example based on the control techniques used in the state-space tutorial please go to the Inverted Pendulum Animation Page after completing this tutorial.

Open-loop poles
In this problem R represents the commanded step input to the cart. The 4 states represent the position and velocity of the cart and the angle and angular velocity of the pendulum. The output y contains both the position of the cart and the angle of the pendulum. We want to design a controller so that when an step input is given to the system, the pendulum should be displaced, but eventually return to zero (i.e. the vertical) and the cart should move to its new commanded position. To view the system's open-loop response please refer to the inverted pendulum modeling page. The first step in designing this type of controller is to determine the open-loop poles of the system. Enter the following lines of code into a m-file (or a '.m' file located in the same directory as MATLAB):
M m b i g l = = = = = = 0.5; 0.2; 0.1; 0.006; 9.8; 0.3; 0; 0; 1; 0];

p = i*(M+m)+M*m*l^2; %denominator A = [0 1 0 0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0 0 0 0 -(m*l*b)/p m*g*l*(M+m)/p B = [0; (i+m*l^2)/p; 0; m*l/p]; C = [1 0 0 0; 0 0 1 0]; D = [0;0]; pend = ss(A,B,C,D); p = eig(A)

The MATLAB command window should output the following text as a result:
p = 0 -0.1428 5.5651 -5.6041

As you can see, there is one right-half-plane pole at 5.5651. This should confirm your intuition that the

system is unstable in open loop.

LQR design
The next step in the design process is to assume that we have full-state feedback (i.e. that we can measure all four states), and find the vector K which determines the feedback control law. This can be done in a number of ways. If you know the desired closed-loop poles, you can use the place or acker command. Another option is to use the lqr function; this will give you the optimal controller (under certain assumptions; consult your textbook for more details). The lqr function allows you to choose two parameters, R and Q, which will balance the relative importance of the input and state in the cost function that you are trying to optimize. The simplest case is to assume R=1, and Q=C'*C. You may notice that we are using both outputs (the pendulum's angle and the cart's position). Essentially, the lqr method allows for the control of both outputs. In this case, it is pretty easy to do. The controller can be tuned by changing the nonzero elements in the Q matrix to get a desirable response. To find the structure of Q, enter the following into the MATLAB command window:
C'*C

You should see the following in the command window:


ans = 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

The element in the 1,1 position will be used to weight the cart's position and the element in the 3,3 position will be used to weight the pendulum's angle. The input weighting R will remain at 1. Now that we know what the Q matrix should look like we can experiment to find the K matrix that will give us a good controller. We will go ahead and find the K matrix and plot the response all in one step so that changes can be made in the control and be seen automatically in the response. Enter the following text into your m-file:
x = 1; y = 1; Q = [x 0 0 0; 0 0 0 0; 0 0 y 0; 0 0 0 0]; R = 1; K = lqr(A,B,Q,R) Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D]; sys_cl=ss(Ac,Bc,Cc,Dc); T=0:0.01:5; U=0.2*ones(size(T)); [Y,T,X]=lsim(sys_cl,U,T); plot(T,Y) legend('Cart','Pendulum')

You should get the following value for K and a response plot:

K = -1.0000 -1.6567 18.6854 3.4594

The curve in green represents the pendulum's angle in radians, and the curve in blue represents the cart's position in meters. As you can see, this plot is not satisfactory. The pendulum and cart's overshoot appear fine, but their settling times need improvement and the cart's rise time needs to go down. As I'm sure you have noticed the cart is not near the desired location but has in fact moved in the other direction. This error will be dealt with in the next section and right now we will focus on the settling and rise times. Go back to your m-file and change the x and y variab to see if you can get a better response. You will find that increasing x makes the settling and rise times go down, and lowers the angle the pendulum moves. Using x=5000 and y=100, the following value of K and step response were found:
K = -70.7107 -37.8345 105.5298 20.9238

You may have noted that if you increased x and y even higher, you could improve the response even more. The reason this plot was chosen was because it satisfied the design requirements while keeping x and y as small as possible. In this problem, x and y have been used to describe the relative weight of the tracking error in the cart's position and pendulum's angle versus the control effort. The higher x and y are, the more control effort is used, but the smaller the tracking error. The system response has a settling time under 2 seconds.

Adding the reference input


Now we want to get rid of the steady-state error. In contrast to the other design methods, where we feedback the output and compare it to the reference input to compute an error, with a full-state feedback controller we are feeding back all the states. We need to compute what the steady-state value of the states should be, multiply that by the chosen gain K, and use a new value as our reference for computing the input. This can be done by adding a constant gain Nbar after the reference. The schematic below shows this relationship:

Nbar can be found using the user-defined function rscale (copy it to the directory that your m-file is in). Delete the lsim line and copy the following to your m-file and run it to view the step response with Nbar added.
Cn = [1 0 0 0]; sys = ss(A,B,Cn,0); 5

Nbar = rscale(sys,K) sys_cl = ss(Ac,Bc*Nbar,Cc,Dc); [Y,T,X] = lsim(sys_cl,U,T); plot(T,Y) legend('Cart','Pendulum')

A different C had to be used because the rscale function will not work for multiple outputs. However, the Nbar found is correct, as you can see from the output below:
Nbar = -70.7107

Now, the steady-state error is within our limits, the rise and settling times are met and the pendulum's overshoot is within range of the design criteria.

Observer design
This response is good, but was found assuming full-state feedback, which most likely will not be a valid assumption. To compensate for this, we will next design a full-order estimator to estimate those states that are not measured. A schematic of this kind of system is shown below, without Nbar:

To begin, we must first find the controller poles. To do this copy the following code to the end of your m-file:
p = eig(Ac)

If you changed the weighting factors x and y above to x=5000 and y=100, you should see the following poles in the MATLAB command window:
p = -8.4910 -8.4910 -4.7592 -4.7592 + + 7.9283i 7.9283i 0.8309i 0.8309i

We want to design estimator poles that are about 4-10 times as fast as slowest pole, say at -40. We will use the place command in MATLAB to find the L vector (note that acker would also work). Remember that the place command cannot have all the desired poles at the same location. Delete from the lsim command on and enter the following text to the end of your m-file to find the L matrix:
P = [-40 -41 -42 -43]; L = place(A',C',P)'

We are using both outputs (the angle of the pendulum and the position of the cart) to design the observer. The system is not observable using only the angle of the pendulum as output; you can check this in MATLAB by computing rank(obsv(A,C(2,:))). This should make sense to you: if you can only measure the angle of the pendulum, you cannot determine what the position of the cart will be. You should see the following in the MATLAB window:
L = 1.0e+03 * 0.0826 1.6992 -0.0014 -0.0762 -0.0010 -0.0402 0.0832 1.7604

Now we will combine the control-law design with the estimator design to get the compensator. The response should be similar to the one from the control-law design. To set up the compensator copy the following code to the end of your m-file:
Ace = [A-B*K B*K; zeros(size(A)) (A-L*C)]; Bce = [ B*Nbar; zeros(size(B))]; Cce = [Cc zeros(size(Cc))]; Dce = [0;0]; est_cl = ss(Ace,Bce,Cce,Dce); T = 0:0.01:5; U = 0.2*ones(size(T)); [Y,T,X] = lsim(est_cl,U,T); plot(T,Y) legend('Cart','Pendulum')

After running this m-file, you should output the following step response simulation plot:

This response is about the same as before. All of the design requirements have been met with the minimum amount of control effort, so no more iteration is needed. As you can see, it is much easier to control multi-input or multi-output systems with the state space method than with any other of the methods. If you are interested in running an animation of the inverted pendulum example based on the control techniques used in this tutorial please go to the Inverted Pendulum Animation Page.

State Space Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink | Animation

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: State-space method for the Pitch Controller


Controllability and Observability Control design via pole placement Reference input
In the Pitch Controller Modeling page, the state-space model was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the pitch angle (theta). The design requirements are Overshoot: Less than 10% Rise time: Less than 5 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2% To see the original problem setup, please refer to the Pitch Controller Modeling page. If you are interested in running an animation of this example based on the control techniques used in the state-space tutorial please go to the Pitch Controller Animation page after completing this tutorial.

Controllability and Observability


The first thing to do in designing a system via state-space method is to check the controllability and observability of the system. For the system to be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

must also have the rank of n. Since our controllability matrix and observability matrix are 3x3, the rank for both matrices must be 3. The MATLAB command rank can give you the ranks of both matrices. Create a new m-file and enter the following commands:
A = [-0.313 -0.0139 0 B = [0.232; 0.0203; 0]; C = [0 0 1]; D = [0]; pitch = ss(A,B,C,D); co = ctrb(pitch); ob = obsv(pitch); Controllability = rank(co) Observability = rank(ob) 56.7 -0.426 56.7 0; 0; 0];

If you run this m-file in the MATLAB command window, you should see
Controllability = 3 Observability = 3

This proves that our system is both completely state controllable and completely state observable.

Control design via pole placement


The schematic of a full-state feedback system is shown below:

where K=Control matrix x=State matrix (alpha, q, theta) de=-Kx=input R=Reference Recall from the State-Space Tutorial page, the "pole placement" technique can be used to find the control matrix (K). Since the determinant of [sI-(A-BK)] matrix is a third-order polynomial, there are three poles we can place. In the State-Space Tutorial, the dominant second-order pole placement method was introduced. However for this example, we will use another method called Linear Quadratic Regulator (LQR) method. This method allows you to find the optimal control matrix that results in some balance between system errors and control effort. Please consult your control textbook for details. To use this LQR method, we need to find three parameters: performance index matrix (R), state-cost matrix (Q), and weighting factor (p). For simplicity, we will choose the performance index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to C' x C. The weighting factor (p) will be varied as we see the step response. To see the structure of the Q matrix, type in the following commands to an m-file and run it in the MATLAB command window (or you can simply type them directly into the command window).
C = [0 0 1]; Q = C'*C

You should see the following Q matrix in the command window:


Q = 0 0 0 0 0 0 0 0 1

Now we are ready to find the control matrix (K) and see the response of the system. First, let the weighting factor (p) equal 50. Enter the following commands to a new m-file and run it in the MATLAB command window.
t = 0:0.1:10; de = 0.2*ones(size(t)); yo = [0 0 0]; A = [-0.313 -0.0139 0 B = [0.232; 3 56.7 -0.426 56.7 0; 0; 0];

0.0203; 0]; C = [0 0 1]; D = [0]; p = 50; Q = [0 0 0; 0 0 0; 0 0 p]; [K] = lqr(A,B,Q,1) sys_cl=ss(A-B*K,B,C,D); lsim(sys_cl,de,t,yo)

After you run this m-file, you should see the step response similar to the one shown below:

The rise time, overshoot, and settling time look satisfactory. However, there is a large steady-state error. This can be easily corrected by introducing the feedforwarding scaling factor (Nbar).

Reference input
Unlike other design methods, the full-state feedback system does not compare the output to the reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference (see the schematic shown above). Thus, we should not expect to see the output equal to the input. To obtain the desired output, we need to scale the reference input so that the output equals the reference. This can be easily done by introducing a feed-forwarding scaling factor called Nbar. The basic schematic with the scaling factor (Nbar) is shown below:

We can easily find Nbar from the MATLAB function rscale. Since this rscale is a user-defined function, you need to copy and save the rscale m-file to your directory. For further assistance in using user-defined functions, refer to the function page. After you have saved the rscale m-file to your directory, enter the following commands to a new m-file and run it in the MATLAB command window. You should see the response shown below:
t = 0:0.1:10; de = 0.2*ones(size(t)); yo = [0 0 0]; A = [-0.313 -0.0139 0 B = [0.232; 0.0203; 0]; C = [0 0 1]; D = [0]; pitch = ss(A,B,C,D); x = 50; Q = [0 0 0; 0 0 0; 0 0 x]; [K] = lqr(A,B,Q,1) Nbar = rscale(pitch,K) sys_cl = ss(A-B*K,B*Nbar,C,D); lsim (sys_cl,de,t,yo) 56.7 -0.426 56.7 0; 0; 0];

Now the steady-state error has been eliminated and all design requirements are satisfied. If you are interested in running an animation of the pitch controller example based on the control techniques used in this tutorial please go to the Pitch Controller Animation page.

State-Space Examples
5

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Control | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink | Animation

Pitch Control Examples Tutorials

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Solution to the Ball & Beam Problem Using the State-space Design Method
Full-State Feedback Controller Reference Input
The state-space representation of the ball and beam example is given below:

Unlike the previous examples where we controlled the gear's angle to control the beam and ball, here we are controlling alpha-doubledot. By doing this we are essentially controlling a torque applied at the center of the beam by a motor. Therefore, we do not need a gear and lever system. The design criteria for this problem are: Settling time less than 3 seconds Overshoot less than 5% To see the derivation of the state-space equations for this problem refer to the ball and beam modeling page. If you are interested in running an animation of this example based on the control techniques used in the state-space tutorial please go to the Ball & Beam Animation Page after completing this tutorial.

Full-State Feedback Controller


We will design a controller for this physical system that utilizes full-state feedback control. A schematic of this type of system is shown below:

Recall, that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-BK)), where s is the Laplace variable. For our system the A and B*K matrices are both 4x4. Hence, there should be four poles for our system. In designing our full-state feedback controller we can move these poles anywhere we want. For our design we desire an overshoot of less than 5% which corresponds to a zeta of 0.7 (please refer to your textbook for the relationship between overshoot and damping ratio). On a root locus this criterion is represented as a 45 degree line emanating from the origin and extending out into the left-half plane. We want to place our desired poles on or beneath this line. Our next criterion is a settling time less than 3 seconds, which corresponds to a sigma = 4.6/Ts = 4.6/3 = 1.53, represented by a vertical line at -1.53 on the root locus. Anything beyond this line in the left-half plane is a suitable place for our poles. Therefore we will place our poles at -2+2i and -2-2i. We will place the other poles far to the left for now, so that they will not affect the response too much. To start with place them at -20 and -80. Now that we have our poles we can use MATLAB to find the controller (K matrix) by using the place command. Copy the following code to an m-file to model the system and find the K matrix:
m R g J = = = = 0.111; 0.015; -9.8; 9.99e-6;

H = -m*g/(J/(R^2)+m); A = [0 1 0 0 0 0 H 0 0 0 0 1 0 0 0 0]; B = [0;0;0;1]; C = [1 0 0 0]; D = [0]; ball = ss(A,B,C,D); p1 p2 p3 p4 = = = = -2+2i; -2-2i; -20; -80;

K = place(A,B,[p1,p2,p3,p4])

Run your m-file and you should get the following output for the K matrix:
place: ndigits= 15

K = 1.0e+03 * 1.8286 1.0286 2.0080 0.1040

After adding the K matrix, the state space equations now become:

We can now simulate the closed-loop response to a 0.25m step input by using the lsim command. Add the following to your m-file:
T = 0:0.01:5; U = 0.25*ones(size(T)); sys_cl = ss(A-B*K,B,C,D); [Y,T,X] = lsim(sys_cl,U,T); plot(T,Y)

Run your m-file and you should get the following plot:

From this plot we see that there is a large steady state error, to compensate for this, we will need to add a reference input compensation (explained in next section). However, the overshoot and settling time criteria are met. If we wanted to reduce the overshoot further, we could make the imaginary part of the pole smaller than the real part. Also, if we wanted a faster settling time we would move the poles further in the left-half plane. Feel free to experiment with the pole positions to see these trends.

Reference Input
Now we want to get rid of the steady-state error. In contrast to the other design methods, where we feedback the output and compare it to the reference input to compute an error, with a full-state feedback controller we are feeding back both states. We need to compute what the steady-state value
3

of the states should be, multiply that by the chosen gain K, and use a new value as our reference for computing the input. This can be done by adding a constant gain Nbar after the reference. The schematic below shows this relationship:

Nbar can be found using the user-defined function rscale (copy it to the directory that your m-file is in). Copy the following to your m-file and run it to view the step response with Nbar added.
Nbar=rscale(ball,K) T = 0:0.01:5; U = 0.25*ones(size(T)); [Y,T,X]=lsim(Nbar*sys_cl,U,T); plot(T,Y)

Your output should be:


place: ndigits= 15 Nbar = 1.8286e+03

Now the steady-state error has been eliminated and all the design criteria are satisfied.

Note: A design problem does not necessarily have a unique answer. Using this method (or any other) may result in many different compensators. For practice you may want to go back and try to change the pole positions to see how the system responds. If you are interested in running an animation of the ball & beam example based on the control techniques used in this tutorial please go to the Ball & Beam Animation Page.

State-space Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

Ball & Beam Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink | Animation

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

iu khin s (Digital Control)


Introduction Zero-order hold equivalence Conversion using c2dm Stability and transient response Discrete Root-Locus
Key MATLAB Commands used in this tutorial are: c2d pzmap zgrid step rlocus

Introduction
The figure below shows the typical continuous feedback system that we have been considering so far in this tutorial. Almost all of the continuous controllers can be built using analog electronics.

The continuous controller, enclosed in the dashed square, can be replaced by a digital controller, shown below, that performs same control task as the continuous controller. The basic difference between these controllers is that the digital system operates on discrete signals (or samples of the sensed signal) rather than on continuous signals.

Different types of signals in the above digital schematic can be represented by the following plots.

The purpose of this Digital Control Tutorial is to show you how to use MATLAB to work with discrete functions either in transfer function or state-space form to design digital control systems.

Zero-order hold equivalence


In the above schematic of the digital control system, we see that the digital control system contains both discrete and the continuous portions. When designing a digital control system, we need to find the discrete equivalent of the continuous portion so that we only need to deal with discrete functions. For this technique, we will consider the following portion of the digital control system and rearrange as follows.

The clock connected to the D/A and A/D converters supplies a pulse every T seconds and each D/A and A/D sends a signal only when the pulse arrives. The purpose of having this pulse is to require that Hzoh(z) have only samples u(k) to work on and produce only samples of output y(k); thus, Hzoh(z) can be realized as a discrete function. The philosophy of the design is the following. We want to find a discrete function Hzoh(z) so that for a piecewise constant input to the continuous system H(s), the sampled output of the continuous system equals the discrete output. Suppose the signal u(k) represents a sample of the input signal. There are techniques for taking this sample u(k) and holding it to produce a continuous signal uhat(t). The sketch below shows that the uhat(t) is held constant at u(k) over the interval kT to (k+1)T. This operation of holding uhat(t) constant over the sampling time is called zero-order hold.

The zero-order held signal uhat(t) goes through H2(s) and A/D to produce the output y(k) that will be the piecewise same signal as if the discrete signal u(k) goes through Hzoh(z) to produce the discrete output y(k).

Now we will redraw the schematic, placing Hzoh(z) in place of the continuous portion.

By placing Hzoh(z), we can design digital control systems dealing with only discrete functions. Note: There are certain cases where the discrete response does not match the continuous response due to a hold circuit implemented in digital control systems. For information, see Lagging effect associated with the hold.

Conversion using c2d


There is a MATLAB function called c2d that converts a given continuous system (either in transfer function or state-space form) to a discrete system using the zero-order hold operation explained above. The basic command for this c2d is one of the following.
sys_d = c2d(sys,Ts,'zoh')

Note: sys can be generated from either tf or ss. Also discrete system can be generated with system data as following. For more information on MATLAB representation, please refer to MATLAB representation. The sampling time (Ts in sec/sample) should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency.

1. Transfer function
Suppose you have the following continuous transfer function

M = 1 kg b = 10 N.s/m k = 20 N/m F(s) = 1 Assuming the closed-loop bandwidth frequency is greater than 1 rad/sec, we will choose the sampling time (Ts) equal to 1/100 sec. Now, create an new m-file and enter the following commands.
M = 1; b = 10; k = 20; num = 1; den = [M b k]; sys = tf(num,den); Ts = 1/100; sys_d = c2d(sys,Ts,'zoh')

Running this m-file in the command window should give you the following result.
Transfer function: 4.837e-05 z + 4.678e-05 ----------------------z^2 - 1.903 z + 0.9048 Sampling time: 0.01

Note: The numerator and denominator matrices are represented in descending powers of z. For more information on MATLAB representation, please refer to MATLAB representation. Now you have the transfer function in discrete form.

2. State-Space
Suppose you have the following continuous state-space model

All constants are same as before The following m-file converts the above continuous state-space to discrete state-space.
M = 1; b = 10; k = 20;

A = [0 -k/M B = [ 0; 1/M]; C = [1 0]; D = [0]; Ts = 1/100;

1; -b/M];

sys = ss(A,B,C,D); sys_d = c2d(sys,Ts,'zoh')

Create an new m-file and copy the above commands. Running this m-file in the MATLAB command window should give you the following results.
a = x1 x2 b = x1 x2 c = y1 d = y1 Sampling time: 0.01 Discrete-time system. x1 0.99903 -0.19026 x2 0.0095131 0.9039

u1 4.8366e-05 0.0095131

x1 1

x2 0

u1 0

From these matrices, the discrete state-space can be written as

Now you have the discrete time state-space model. Note: For more information on the discrete state-space, please refer to Discrete State-Space.

Stability and transient response


For continuous systems, we know that certain behaviors results from different pole locations in the
6

s-plane. For instance, a system is unstable when any pole is located to the right of the imaginary axis. For discrete systems, we can analyze the system behaviors from different pole locations in the z-plane. The characteristics in the z-plane can be related to those in the s-plane by the expression

T = Sampling time (sec/sample) s = Location in the s-plane z = Location in the z-plane The figure below shows the mapping of lines of constant damping ratio (zeta) and natural frequency (Wn) from the s-plane to the z-plane using the expression shown above.

If you noticed in the z-plane, the stability boundary is no longer imaginary axis, but is the unit circle |z|=1. The system is stable when all poles are located inside the unit circle and unstable when any pole is located outside. For analyzing the transient response from pole locations in the z-plane, the following three equations used in continuous system designs are still applicable.

where

zeta = Damping ratio Wn = Natural frequency (rad/sec) Ts = Settling time Tr = Rise time Mp = Maximum overshoot Important: The natural frequency (Wn) in z-plane has the unit of rad/sample, but when you use the equations shown above, the Wn must be in the unit of rad/sec. Suppose we have the following discrete transfer function

Create an new m-file and enter the following commands. Running this m-file in the command window gives you the following plot with the lines of constant damping ratio and natural frequency.
numDz = 1; denDz = [1 -0.3 0.5]; sys = tf(numDz,denDz,-1); % the -1 indicates that the sample time is undetermined pzmap(sys) axis([-1 1 -1 1]) zgrid

From this plot, we see poles are located approximately at the natural frequency of 9pi/20T (rad/sample) and the damping ratio of 0.25. Assuming that we have a sampling time of 1/20 sec (which leads to Wn = 28.2 rad/sec) and using three equations shown above, we can determine that this system should have the rise time of 0.06 sec, a settling time of 0.65 sec and a maximum overshoot of 45% (0.45 more than the steady-state value). Let's obtain the step response and see if these are correct. Add the following commands to the above m-file and rerun it in the command window. You should get the following step response.
sys = tf(numDz,denDz,1/20); step(sys,2.5);

As you can see from the plot, the rise time, settling time and overshoot came out to be what we expected. This shows how you can use the locations of poles and the above three equations to analyze the transient response of the system. For more analysis on the pole locations and transient response, see Transient Response.

Discrete Root-Locus
The root-locus is the locus of points where roots of characteristic equation can be found as a single gain is varied from zero to infinity. The characteristic equation of an unity feedback system is

where G(z) is the compensator implemented in the digital controller and Hzoh(z) is the plant transfer function in z. The mechanics of drawing the root-loci are exactly the same in the z-plane as in the s-plane. Recall from the continuous Root-Locus Tutorial, we used the MATLAB function called sgrid to find the root-locus region that gives an acceptable gain (K). For the discrete root-locus analysis, we will use the function zgrid that has the same characteristics as sgrid. The command zgrid(zeta, Wn) draws lines of constant damping ratio (zeta) and natural frequency (Wn). Suppose we have the following discrete transfer function

and the requirements are a damping ratio greater than 0.6 and a natural frequency greater than 0.4 rad/sample (these can be found from design requirements, sampling time (sec/sample) and three equations shown in the previous section). The following commands draw the root-locus with the lines of constant damping ratio and natural frequency. Create an new m-file and enter the following commands. Running this m-file should give you the following root-locus plot.
numDz = [1 -0.3]; denDz = [1 -1.6 0.7]; sys = tf(numDz,denDz,-1); rlocus(sys)

axis([-1 1 -1 1]) zeta = 0.4; Wn = 0.3; zgrid(zeta,Wn)

From this plot, you should realize that the system is stable because all poles are located inside the unit circle. Also, you see two dotted lines of constant damping ratio and natural frequency. The natural frequency is greater than 0.3 outside the constant-Wn line, and the damping ratio is greater than 0.4 inside the constant-zeta line. In this example, we do have the root-locus drawn in the desired region. Therefore, a gain (K) chosen from one of the loci in the desired region should give you the response that satisfies design requirements.

Digital Control Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

10

Digital Control Example: Designing Cruise Control using Root-Locus method


Discrete transfer function Root-Locus in z-plane Compensation using a digital controller
In this digital control version of the cruise control problem, we are going to use the root-locus design method to design the digital controller. If you refer to the Cruise Control: Modeling page, the open-loop transfer function was derived as

where m = 1000 b = 50 U(s) = 10 Y(s) = velocity output The design requirements are Rise time: Less than 5 seconds Overshoot: Less than 10% Steady-state error: Less than 2%

Discrete transfer function


The first step in performing a discrete analysis of a system is to find the discrete equivalent transfer function of the continuous portion. We will convert the above transfer function (Y(s)/U(s)) to the discrete transfer function using the MATLAB function called c2d. To use this c2d, you need to specify three arguments: system, sampling time (Ts), and the 'method'. You should already be familiar with how to enter num and den matrices. The sampling time (Ts), in the unit of sec/sample, should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency. For the method, we will use the zero-order hold ('zoh'). Let the sampling time equal 1/50 sec; this is sufficiently fast assuming that the bandwidth frequency is 1 rad/sec. Now enter the following commands into an m-file and run it in the command window.
num = [1]; den = [1000 50]; cruise = tf(num,den); Ts = 1/50; cruise_d = c2d(cruise,Ts,'zoh')

The following matrices should be returned to the command window.


Transfer function: 1.999e-05 --------z - 0.999 Sampling time: 0.02

Root-Locus in z-plane
Recall from the Digital Control Tutorial, the MATLAB function called zgrid can be used to find an acceptable region of the discrete root-locus that gives the desired gain (K). The zgrid command requires two arguments: Natural frequency (Wn) and damping ratio (zeta). These two arguments can be found from the rise time and the overshoot requirements and the following two equations.

where Wn=Natural frequency (rad/sec) zeta=Damping ratio Tr=Rise time Mp=Maximum overshoot Since our rise time and overshoot requirements are 5 seconds and 10%, respectively, we can determine that the natural frequency (Wn) must be greater than 0.36 rad/sec and the damping ratio (zeta) must be greater than 0.6. Let's generate the root-locus and use the zgrid to find the acceptable region of the root-locus. But before doing that, if you refer to the Digital Control Tutorial, the natural frequency argument for zgrid needs to be in the unit of rad/sample, so let Wn = 0.36*Ts = 0.0072 rad/sample. Now add the following commands to the above m-file and rerun it. You should get the following plot.
Wn = 0.0072; zeta = 0.6; rlocus(cruise_d) zgrid(zeta, Wn) axis ([-1 1 -1 1])

The region of the complex plane which interests us is that which is near the point (1,0), so you should zoom in on this point. Rerun using the axis command below you should now see
axis ([0.95 1 -.1 .1])

The dotted line on the right, indicates the locations of constant natural frequency (Wn), and the natural frequency is greater than 0.0072 outside the line. The other dotted line indicates the locations of constant damping ratio (zeta), and the damping ratio is greater than 0.6 inside the line. The jagged vertical line is a portion of the unit circle which is calculated at low resolution (hence the jaggedness). In the above plot, you see that part of the root-locus is inside the desired region. Let's find a gain (K) using the MATLAB function rlocfind and obtain the corresponding step response. Add the following commands to the above m-file and rerun it in the MATLAB command window.
[K,poles] = rlocfind(cruise_d)

In the command window, you should see the prompt asking you to select a point on the root-locus. Remember that if you choose a pole which is too far inside the unit circle, then the step response will be too fast indicating a physically unreasonable acceleration. Therefore you should choose the pole which is near the intersection of the constant natural frequency and the real axis. The gain (K) and the pole location should be returned to the command window. Selecting the point at 0.99 gives the following root locus graph.

To see the step response, add the following to your m-file.


sys_cl = feedback(K*cruise_d,1); U = 10; figure step(U*sys_cl,10);

This response satisfies the rise time and overshoot requirements. But the steady state error is about 11%. To obtain the desired steady state error, we will modify the discrete controller.
4

Compensation using a digital controller


Recall from the continuous Cruise Control: Root-Locus page, the lag controller was added to the system to obtain the desired response. In this digital control version of the cruise control problem, we will modify the existing digital controller by adding a function of the form

There is a guideline to design digital lead and lag compensators and a guideline for design continuous time lead and lag compensators. The discrete design method described says that the zero of the lag compensator should be chosen to (approximately) cancel one of the plant poles, as long as it is stable. Thus, we choose the zero to be at Zo = 0.999. To reduce the steady state error, we note that the low frequency gain of the discrete time control system with a lag compensator is increased by a factor of of (1 - Zo)/(1 - Po). To reduce the steady state error by a factor of 5, we choose Po = 0.9998. To have a a gain of 1 at zero frequency, the numerator is multiplied by Kd = (1 - Zp)/(1 - Zo) = 0.2 before using the root locus. Note that the whole compensator is multiplied by the gain determined from the root locus. Now we have the discrete compensator transfer function. Let's generate the root-locus and obtain the step response. Create a new m-file and enter the following commands.
num = [1]; den = [1000 50]; cruise = tf(num,den) Ts = 1/50; cruise_d = c2d (cruise,Ts,'zoh'); contr = tf(0.2*[1 -0.999],[1 -0.9998],Ts); Wn = 0.0072; zeta = 0.6; rlocus(contr*cruise_d) zgrid(zeta, Wn) axis([0.98 1 -0.01 0.01]) [K,poles] = rlocfind(contr*cruise_d) sys_cl = feedback(K*contr*cruise_d,1); U = 10; figure step(U*sys_cl,10);

Running this m-file in the command window give you the following root-locus.

In the command window, you should be asked to pick a point on the root-locus. Click on the locus near +0.99. You should now have the step response similar to the one shown below.

This response rises about as fast as before, but the steady state error has been reduced to 2%. This system satisfies all design requirements with the reasonable control effort. Note: A design problem does not necessarily have a unique answer. For practice, you may try other compensators to obtain a better response than the one shown above.

Digital Control Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Cruise Control Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Digital DC Motor Speed Control with PID Control


Continuous to Discrete Conversion PID Controller
In this page, we will consider the digital control version of DC motor speed problem. A digital DC motor model can be obtained from conversion of the analog model, as we will describe. The controller for this example will be designed by a PID method. From the Modeling a DC Motor Page, the open-loop transfer function for DC motor's speed was derived as:

Where: *electrical resistance (R) = 1 ohm *electrical inductance (L) = 0.5 H *electromotive force constant (Ke=Kt) = 0.01 Nm/Amp *moment of inertia of the rotor (J) = 0.01 kg*m^2/s^2 *damping ratio of the mechanical system (b) = 0.1 Nms *input (V): Source Voltage *output (theta dot): Rotating speed *The rotor and shaft are assumed to be rigid The design requirements for 1 rad/sec step input are Settling time: Less than 2 seconds Overshoot: Less than 5% Steady-state error: Less than 1%

Continuous to Discrete Conversion


The first step in designing the discrete control system is to convert the continuous transfer function to a discrete transfer function. The MATLAB command c2d will do this for you. The c2d command requires the following three arguments: system, the sampling time (Ts) and method for conversion. In this example, we will use the zero-order hold ('zoh') method. From the design requirement, let the sampling time, Ts equal 0.12 seconds, which is 1/10 the time constant of a system with a settling time of 2 seconds. Let's create a new m-file and enter the following commands:
R = 1; L = 0.5;

Kt = 0.01; J = 0.01; b = 0.1; num = Kt; den = [(J*L) (J*R)+(L*b) (R*b)+(Kt^2)]; motor = tf(num,den) Ts = 0.12; motor_d= c2d(motor,Ts,'zoh')

Running this m-file should return the following:


Transfer function: 0.009201 z + 0.005709 ---------------------z^2 - 1.088 z + 0.2369 Sampling time: 0.12

After you have done this, let's see what the closed-loop step response looks like with a proportional gain of 1. The step command will do this for you. Click here for more information. Add the following MATLAB code to the end of previous m-file and rerun it.
sys_cl = feedback(motor_d,1) [Y,T] = step(sys_cl,12); stairs(T,Y); xlabel('Time (s)') ylabel('Velocity (rad/s)') title('Stairstep Response:Original')

You should see the following plot:

PID Controller
2

Recall that the continuous-time transfer function for a PID controller is:

There are several ways for mapping from the s-plane to z-plane. The most accurate one is . We cannot obtain PID transfer function in this way because the discrete-time transfer function would have more zeroes than poles, which is not realizable. Instead we are going to use the bilinear transformation shown below.

Thus we can derive the discrete PID controller with bilinear transformation mapping. For more detail on the derivation of the discrete PID controller, see the Discrete PID Controller Page. Equivalently, the c2d command in MATLAB will help you to convert the continuous-time PID compensator to discrete-time PID compensator by using the "tustin" method. The "tustin" method use the bilinear approximation to convert to discrete time. According to the PID Design Method for the DC Motor page, Kp = 100, Ki = 200 and Kd = 10 satisfy the design requirement. We will use all of these gains in this example. Now add the following MATLAB commands to your previous m-file and rerun it in MATLAB window.
% Discrete PID controller with bilinear approximation Kp = 100; Ki = 200; Kd = 10; PID = tf([Kd Kp Ki],[1 0]); contr = c2d(PID,Ts,'tustin');

Let's see if the performance of the closed-loop response with the PID compensator satisfies the design requirements. Now add the following code to the end of your m-file and rerun it. You should get the following closed-loop stairstep response.
sys_cl = feedback(contr*motor_d,1); [x2,T] = step(sys_cl,12); stairs(T,x2) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response:with PID controller')

As you can see from the above plot, the closed-loop response of the system is unstable. Therefore there must be something wrong with compensated system. So we should take a look at root locus of the compensated system. Let's add the following MATLAB command into the end of your m-file and rerun it.
rlocus(contr*motor_d) title('Root Locus of Compensated System')

From this root-locus plot, we see that the denominator of the PID controller has a pole at -1 in the z-plane. We know that if a pole of a system is outside the unit circle, the system will be unstable. This compensated system will always be unstable for any positive gain because there are an even number of poles and zeroes to the right of the pole at -1. Therefore that pole will always move to the left and outside the unit circle. The pole at -1 comes from the compensator, and we can change its location by changing the compensator design. We choose it to cancel the zero at -0.62. This will make the system
4

stable for at least some gains. Furthermore we can choose an appropriate gain from the root locus plot to satisfy the design requirements using rlocfind. Enter the following MATLAB code to your m-file.
contr.den = conv([1 -1],[1/.62 1]); rlocus(contr*motor_d) title('Root Locus of Compensated System'); [K,poles] = rlocfind(contr*motor_d) sys_cl = feedback(K*contr*motor_d,1); [x3,T] = step(sys_cl,12); stairs(T,x3) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response:with PID controller')

Note how the first line of code above allows you to change the denominator of the system transfer function directly, without having to redefine the system. The new closed loop poles will have a pole at -0.625 instead of -1, which almost cancels the zero of uncompensated system. In the MATLAB window, you should see the command asking you to select the point on the root-locus plot. You should click on the plot as the following:

Then MATLAB will return the appropriate gain and the corresponding compensated poles, and it will plot the closed-loop compensated response as follows.

The plot shows that the settling time is less than 2 seconds and the percent overshoot is around 3%. In addition, the steady state error is zero. Also, the gain, K, from root locus is 0.2425 which is reasonable. Therefore this response satisfies all of the design requirements.

Digital Control Examples Motor Speed Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Root Locus Design for Digital DC Motor Position Control


Continuous to Discrete Conversion Root Locus Design
In this example, the controller will be designed by a Root Locus method. A digital DC motor model can obtain from conversion of analog DC motor model, as we will describe. According to the Modeling a DC Motor Page, the open-loop transfer function for DC motor's position is as shown.

where: *electric resistance (R) = 4 ohm *electric inductance (L) = 2.75E-6 H *electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp *moment of inertia of the rotor (J) = 3.2284E-6 kg*m^2/s^2 *damping ratio of the mechanical system (b) = 3.5077E-6 Nms *input (V): Source Voltage *output (sigma dot): Rotating speed *The rotor and shaft are assumed to be rigid The design requirements are: Settling time: Less than 0.04 seconds Overshoot: Less than 16% Steady-state error: 0 with a step disturbance input

Continuous to Discrete Conversion


The first step in the design of a discrete-time system is to convert a continuous transfer function to a to discrete transfer function. MATLAB can be used to convert the above transfer function discrete transfer function by using the c2d command. The c2d command requires three arguments: system, a sampling time (T) and a type of hold circuit. In this example we will use the zero-order hold (zoh). Refer to the Digital Control Tutorials page for more information. From the design requirement, let the sampling time, T equal to 0.001 seconds, which is 1/100 of the required time constant or 1/40 of the required settling time. Let's create a new m-file and add the following MATLAB code:
R L K J = = = = 4; 2.75E-6; 0.0274; 3.2284E-6;

b = 3.5077E-6; num = K; den = [(J*L) (J*R)+(L*b) (R*b)+(K^2) 0]; motor = tf(num,den) Ts = 0.001; motor_d = c2d(motor, Ts, 'zoh') [numd,dend] = tfdata(motor_d,'v')

MATLAB should return the following:


Transfer function: 0.001039 z^2 + 0.001021 z + 9.454e-10 ------------------------------------z^3 - 1.942 z^2 + 0.9425 z Sampling time: 0.001 numd = 0 dend = 1.0000 -1.9425 0.9425 0 0.0010 0.0010 0.0000

As noticed above, both the numerator and the denominator of the discrete transfer function have one extra root at z = 0. Also, we can get rid of the leading zero coefficient in the numerator. To do this add the following code to cancel out extra pole and zero to avoid numerical problems in MATLAB. Otherwise it will consider both the numerator and denominator to be fourth-order polynomials.
numd = numd(2:3); dend = dend(1:3); motor_d = tf(numd,dend,Ts)

Therefore, the discrete-time transfer function from the motor position output to the voltage input is:

We would like to see what the closed-loop response of the system looks like when no controller is added. First, we have to close the loop of the transfer function by using the feedback command. After closing the loop, let's see how the closed-loop stairstep response performs by using the stepand stairs commands. The step command will provide the vector of discrete step signals and stairs command will connect these discrete signals (click here for more information). Add the following MATLAB code at the end of previous m-file and rerun it.
sys_cl = feedback(motor_d,1); [x1,t] = step(sys_cl,.5); stairs(t,x1) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Response:Original')

You should see the following plot:

Root Locus Design


The main idea of root locus design is to obtain the closed-loop response from the open-loop root locus plot. By adding zeroes and poles to the original system, the root locus can be modified, to a new closed-loop response. First let's see the root-locus for the system itself. In your m-file, add the following commands and rerun it. You should get the root-locus plot as shown below.
rlocus(motor_d) title('Root Locus of Original System') zgrid(0,0) axis([-2,2,-2,2])

To get the zero steady-state error from the closed-loop response, we have to add an integral control.

Recall that the integral control in the continuous- time is 1/s. If we use the backward difference approximation for mapping from the s-plane to the z-plane as described by s = 1/(z-1), one pole will be added at 1 on the root locus plot. After adding the extra pole at 1, the root locus will have three poles near 1. Therefore the root locus will move out to the right, and the closed-loop response will be more unstable. Thus, we must add one zero near 1, inside the unit circle, to cancel with one pole and pull the root locus in. We will add a zero at z = 0.95. In general, we must at least add as many poles as zeroes for the controller to be causal. Now add the following MATLAB commands in to your m-file.
numi = [1 -0.95]; deni = [1 -1]; icontr = tf(numi,deni,Ts);

Recall from the Digital Control Tutorial page, the zgrid command can be used to find the desired region (satisfying the design requirements) on the discrete root locus plot. The zgrid command requires two arguments: the natural frequency (Wn) and the damping ratio (zeta). From the design requirement, the settling time is less than 0.04 seconds and the percent overshoot is less than 16%. We know the formulas for finding the damping ratio and natural frequency as shown:

where: OS: the percent overshoot Ts: the settling time The required damping ratio is 0.5 and the natural frequency is 200 rad/sec, but the zgrid command requires a non-dimensional natural frequency. Therefore Wn = 200*Ts = 0.2 rad/sample. Add the following MATLAB code into the end of your m-file and rerun it.
rlocus(icontr*motor_d); zgrid(0.5,0.2) title('Root Locus of system with integral control') axis([-2,2,-2,2])

You should get the following plot:

From the above root locus plot, we can see that system is unstable at all gains because the root locus is outside the unit circle. Moreover, the root locus should be in the region where the damping ratio line and natural frequency cross each other to satisfy the design requirements. Thus we have to pull the root locus in further by first canceling the zero at approximately -0.98, since this zero will add overshoot to the step response. Then we have to add one more pole and two zeroes near the desired poles. After going through some trial and error, one more pole is added at 0.61 and two zeroes are added at 0.76. Add the following commands to your m-file and rerun it in the MATLAB window.
numc = conv([1 -0.76],[1 -0.76]); denc = conv([1 0.9831],[1 -0.61]); contr = tf(numc,denc,Ts); rlocus(icontr*contr*motor_d); zgrid(0.5,0.2) title('Root Locus of Compensated System') axis([-2,2,-2,2])

The system will have a pole at 0.61 instead of -0.98. You should get the following root locus plot:

From the above root locus plot, we see that the root locus is in the desired region. Let's find a gain, K, on the root locus plot by using the rlocfind command and obtain the stairstep response with the selected gain. Enter the following commands at the end of your m-file and rerun it.
K = rlocfind(icontr*contr*motor_d) sys_cl = feedback(K*icontr*contr*motor_d,1); [x2,t] = step(sys_cl,.05); stairs(t,x2) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Response of Compensated System')

In the MATLAB window, you should see the command asking you to select a point on the root-locus plot. You should click on the plot as the following:

The selected gain should be around 330, and it will plot the closed-loop compensated response as follows.

From the above closed-loop response, the settling time is about 0.05 seconds which is satisfy the requirement, but the percent overshoot is 22% which is too large due to the zeroes. If we select the gain to be larger, the requirements will be satisfied. On the other hand, the problem will be unrealistic and a huge actuator is needed, you can try this yourself by picking a larger gain on the previous root locus plot which will yield an unrealistically sudden step response. So we have to move both one pole and two zeroes a little further to the right to pull root locus in a little bit more. The new pole will be put at 0.7 and two zeroes will be at 0.85. Go back to your m-file and change only numc and denc as shown below and rerun it in MATLAB window.
numc = conv([1 -0.85],[1 -0.85]); denc = conv([1 0.9831],[1 -0.7]); 7

Then you should see the following root locus plot.

On the new root locus, you should click on the plot to select a new gain as the following:

The selected gain should be around 450, and then it will plot the closed-loop compensated response as follows:

Now we see that the settling time and percent overshoot meet the design require ments of the system. The settling time is 0.04 seconds and the percent overshoot is about 10%. Now let's take a look at a disturbance response of the closed-loop system. We need to cancel the selected gain, the integral transfer function and controller's transfer function from the closed-loop transfer function. So add the following code into your m-file and rerun it.
dist_cl=feedback(motor_d,K*icontr*contr); [x4,t] = step(dist_cl,.25); stairs(t,x4) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Response of Compensated System')

MATLAB should return the following plot:

We can see that a response to the disturbance is small (3.3% of the disturbance ) and settles within 2% of the disturbance after 0.04 seconds and eventually reaches zero.

Digital Control Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Position Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

10

Example: State Space Design for Digital Bus Suspension Control


Sampling Time Selection Continuous to Discrete Conversion Designing the Controller Simulating the Closed-Loop Response
In this example, we will design a digital state space controller for the bus suspension control example. First we will convert the continuous time model to a discrete time model, and then use the pole placement method to design the controller. From the bus suspension state space modeling page, the state space model of the system is:

Where: * body mass (m1) = 2500 kg, * suspension mass (m2) = 320 kg, * spring constant of suspension system (k1) = 80,000 N/m, * spring constant of wheel and tire (k2) = 500,000 N/m, * damping constant of suspension system (b1) = 350 Ns/m. * damping constant of wheel and tire (b2) = 15,020 Ns/m. * control force (u) = force from the controller we are going to design. The design requirements are: Overshoot: Output (X1-X2) less than 5% of disturbance (W) Settling time: Less than 5 seconds

Sampling Time Selection


The first step in the design of a discrete-time controller is to convert the continuous plant to its discrete time equivalent. First, we need to pick an appropriate sampling time, T. In this example, selection of sampling time is very important since a step in the road surface very quickly affects the output. Physically, what happens is the road surface suddenly lifts the wheel, compressing the spring, K2, and the damper, b2. Since the suspension mass is relatively low, and the spring fairly stiff, the suspension mass rises quickly, increasing X2

almost immediately. Since the controller can only see the effect of the disturbance after a complete sampling period, we have to pick a sampling time, T, short enough so that the output (X1-X2) does not exceed the 5% requirement in one sampling period. To pick the sampling period, we need to closely examine the beginning of the step response. If you remember from the modeling page, the output quickly goes negative in response to a step disturbance, and then begins to oscillate. We will simulate just the beginning of this response by setting the time vector input to the step function to range from 0 to .005. The response to a .1m step input is simulated by multiplying the B matrix by .1. Create a new m-file and enter the following code:
m1 m2 k1 k2 b1 b2 = = = = = = 2500; 320; 80000; 500000; 350; 15020; 1 0 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) 0 -((b1/m1)+(b1/m2)+(b2/m2)) 0 -((k1/m1)+(k1/m2)+(k2/m2)) 0 (b1*b2)/(m1*m2) -(b2/m2) -(k2/m2)]; 0 -(b1/m1) 1 0];

A = [0 -(b1*b2)/(m1*m2) b2/m2 k2/m2 B = [0 1/m1 0 (1/m1)+(1/m2) C = [0 0 1 0]; D = [0 0];

sus = ss(A,B,C,D); step(sus*[0;1]*.1,0:0.0001:.005);

This plot shows that the spring, K1 compresses very quickly, and exceeds our requirement of 5mm in response to a .1m step after only a little more than 0.001s. Therefore, we will set T=.0005s in order to give the controller a chance to respond.

Continuous to Discrete Conversion


Now that we have selected a sampling time, we can convert the plant to discrete time. MATLAB can be used to convert the above state space model, sus, to a discrete state space model, sus_d, by using the c2d command. The c2d command takes three arguments: the continuous time system, the sampling time, T, and the type of
2

hold circuit. In this example we will use zero-order hold ('zoh'). Refer to the Digital Control Tutorials page for more information. Add the following code to your m-file:
T = .0005; sus_d = c2d(sus,T,'zoh')

MATLAB should return the following:


a = x1 x2 x3 x4 b = x1 x2 x3 x4 c = y1 d = y1 Sampling time: 0.0005 Discrete-time system. x1 1 -0.0034605 0.02338 0.77045 x2 0.0005 1 5.8525e-06 0.00019351 x3 -3.1263e-06 -0.012444 0.97598 -0.91116 x4 -1.8022e-08 -7.3126e-05 0.00049399 0.99977

u1 4.9989e-11 1.9994e-07 4.3748e-10 1.7624e-06

u2 8.5058e-07 0.0034605 -0.02338 -0.77045

x1 0

x2 0

x3 1

x4 0

u1 0

u2 0

which represent the new discrete-time state space model.

Adding an Integrator
In this example, we will need to add an integrator to the system in order to drive the steady-state response to zero. We will add this integrator in series with the plant. This will have the effect of adding another state to the plant. An integrator in discrete time state space can be represented as a trapezoidal approximation of integration over each sample period as follows:

To add this, add the following commands in your m-file:


Ai = 1; Bi = T; Ci = 1; Di = T/2; [As,Bs,Cs,Ds]=ssdata(sus_d); Aa = [As, zeros(4,1); Bi*Cs, Ai]; Ba = [Bs; 0,0];

Ca = [Cs, Da = Ds;

0];

sus_di = ss(Aa,Ba,Ca,Da,T); [Ad,Bd,Cd,Dd] = ssdata(sus_di);

Designing the Controller


The structure of the controller is similar to the structure of the continuous-time state space controller. We will now use the place command to compute the gain matrix, K, which will, in feedback, give us the desired closed-loop poles. We first need to decide where to place the closed-loop poles. Since we get to place all five of the closed-loop poles, we can be very selective about where they go. In particular, we can place them to cancel all of the plant zeros, as well as give us the desired response. First, we will find the plant zeros by converting the plant's digital state equations to a transfer function, and then finding the roots of the numerator. We will use the tfdata command which takes a system (sus_d) as its argument and outputs a transfer function numerator and denominator. Since the suspension system (sus_d) has two inputs, only one input must be selected. Notice how the first input is selected in the following code. Add the following code to your m-file:
sus1 = sus_di*[1;0]; %select the first input [num,den] = tfdata(sus1,'v'); z = roots(num)

MATLAB will return the following:


zeros = -0.9929 1.0000 0.9986 + 0.0065i 0.9986 - 0.0065i

We will select these three zeros as three of our desired closed-loop poles. One of the other two will be selected at .9992 since a pole there settles in approximately 10000 samples (or 5 seconds). The last pole will be selected at z=.5 since this is sufficiently fast to be insignificant. Add the following code to your m-file:
p1 p2 p3 p4 p5 = = = = = z(1); z(3); z(4); .9992; .5;

K=place(Ad,Bd*[1;0],[p1 p2 p3 p4 p5])

MATLAB will return the following:


place: ndigits= 15 K = 1.0e+09 * 0.0534 0.0000 1.0898 0.0011 1.8286

Simulating the Closed-Loop Response


We can use the step command to simulate the closed-loop response. Since multiplying the state vector by K in
4

our controller only returns a single signal, u, we need to add a row of zeros to K by multiplying it by [1 0]T. This is identical to what was done in the continuous design to compensate for the fact that there are two inputs to the plant, but only one is a control input. We will simulate with a negative .1m step disturbance in the road to give us a positive deflection of the bus for aesthetic reasons. Enter the following code into your m-file:
sys_cl = ss(Ad-Bd*[1;0]*K,Bd,Cd,Dd,T); step(-.1*sys_cl*[0;1],5);

You should see the following plot.

We can see in this plot, that the overshoot is less than 5mm, and the response settles well within 5 seconds.

Digital Control Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Digital Control Example: Inverted Pendulum using State-Space method


Discrete state-space Controllability and Observability Control design via pole placement Reference input Observer design
In this digital control version of the inverted pendulum problem, we will use the state-space method to design the digital controller. If you refer to the Inverted Pendulum Modeling page, the state-space equations were derived as

where M mass of the cart 0.5 kg m mass of the pendulum 0.5 kg b friction of the cart 0.1 N/m/sec l length to pendulum center of mass 0.3 m I inertia of the pendulum 0.006 kg*m^2 u step force applied to the cart x cart position coordinate phi pendulum angle from vertical Outputs are the cart displacement (x in meters) and the pendulum deflection angle (phi in radians). The design requirements are Settling time for x and phi less than 5 seconds Rise time for x of less than 1 second Overshoot of phi less than 0.35 rad (20 deg) Steady-state error of x and phi less than 2%

Discrete state-space
The first thing to do here is to convert the above continuous state-space equations to discrete statespace. To do this, we are going to use the MATLAB function called c2d. To use this c2d, we need to specify three arguments: state-space system, sampling time (Ts in sec/sample), and the 'method'. You should already be familiar with how to construct a state-space system from A, B, C, and D matrices. The sampling time should be smaller than 1/(30*BW) sec, where BW is the closed-loop bandwidth frequency. The method we will use is the zero-order hold ('zoh'). Assuming that the closed-loop bandwidth frequencies are around 1 rad/sec for both the cart and the pendulum, let the sampling time be 1/100 sec/sample. Now we are ready to use c2d. Enter the following commands to an m-file.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matrices A = [0 1 0 -(i+m*l^2)*b/p 0 0 0 -(m*l*b)/p B = [ 0; (i+m*l^2)/p; 0; m*l/p]; C = [1 0 0 0; 0 0 1 0]; D = [0; 0]; Ts = 1/100; pend = ss(A,B,C,D); pend_d = c2d(pend,Ts,'zoh') 0 (m^2*g*l^2)/p 0 m*g*l*(M+m)/p 0; 0; 1; 0];

Running this m-file in the MATLAB command window gives you the following four matrices.
a = x1 x2 x3 x4 b = x1 x2 x3 x4

x1 1 0 0 0

x2 0.0099909 0.99818 -2.2719e-05 -0.0045437

x3 0.00013359 0.026717 1.0016 0.31192

x4 4.4532e-07 0.00013359 0.010005 1.0016

u1 9.0859e-05 0.018167 0.00022719 0.045437

c = y1 y2 d = y1 y2 Sampling time: 0.01 Discrete-time system.

x1 1 0

x2 0 0

x3 0 1

x4 0 0

u1 0 0

Now we have obtained the discrete state-space model of the form

Controllability and Observability


The next step is to check the controllability and the observability of the system. For the system to be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

must also have the rank of n.

Since our controllability matrix and observability matrix are '4x4', the rank of both matrices must be 4. The function rank can give you the rank of each matrix. In an new m-file, enter the following commands and run it in the command window.
F = [1.0000 0 0 0 G = [0.0001 0.0182; 0.0002; 0.0454]; H = [1 0 J = [0; 0]; Ts = 1/100; pend_d = ss(F,G,H,J,Ts); co = ctrb(pend_d); ob = obsv(pend_d); Controllability = rank(co) Observability = rank(ob) 0 0 0 1 0; 0]; 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

In the command window, you should see


Controllability = 4 Observability = 4

This proves that our discrete system is both completely state controllable and completely state observable.

Control design via pole placement


The schematic of a full-state feedback system is shown below.

The next step is to assume that all four states are measurable, and find the control matrix (K). If you refer to the continuous Inverted Pendulum: State-Space page, the Linear Quadratic Regulator (LQR) method was used to find the control matrix (K). In this digital version, we will use the same
4

LQR method. This method allows you to find the optimal control matrix that results in some balance between system errors and control effort. Please consult your control textbook for details. To use this LQR method, we need to find three parameters: Performance index matrix (R), state-cost matrix (Q), and weighting factors. For simplicity, we will choose the performance index matrix equal 1 (R=1), and the state-cost matrix (Q) equals H' x H. The weighting factors will be chosen by trial and error. The state-cost matrix (Q) has the following structure
Q = 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

The element in the 1,1 position will be used to weight the cart's position and the element in the 3,3 position will be used to weight the pendulum's angle. The weighting factors for the cart's position and the pendulum's angle will be chosen individually. Now we are ready to find the control matrix (K) and see the response of the system. Enter the following commands to an new m-file and run it in the MATLAB command window.
T = 0:0.01:5; U = 0.2*ones(size(T)); F = [1.0000 0 0 0 G = [0.0001; 0.0182; 0.0002; 0.0454]; H = [1 0 J = [0; 0]; x = 1; y = 1; Q = [x 0 0 0 R = 1; K = dlqr(F,G,Q,R) Ts = 1/100; sys_cl = ss(F-G*K,G,H,J,Ts); [Y,T,X] = lsim(sys_cl,U); stairs(T,Y) legend('Cart (x)','Pendulum (phi)') 0 0 0 0 %weighting factor for the cart position %weighting factor for the pendulum angle 0 0 y 0 0; 0; 0; 0]; 0 0 0 1 0; 0]; 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

You should see the following step response.

The curve in green represents the pendulum's angle, in radians, and the curve in blue represents the cart's position in meters. The pendulum's and cart's overshoot appear fine, but their settling times need improvement, and the cart's rise time needs to be decreased. Also the cart has, in fact, moved in the opposite direction. For now, we will concentrate on improving the settling times and the rise times, and fix the steady-state error later. Let's increase the weighting factors (x and y) and see if both the settling and rise times decrease. Go back to your m-file and change the x and y to x=5000 and y=100. Running this m-file in the command window gives you the following new step response.

From this plot, we see that all design requirements are satisfied except the steady-state error of the cart position (x). We can easily correct this by introducing a feedforward scaling factor (Nbar).

Reference input
6

Unlike other design methods, the full-state feedback system does not compare the output to the reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference (see the schematic shown above). Thus, we should not expect to see the output equal to the input. To obtain the desired output, we need to scale the reference input so that the output equals the reference. This can be easily done by introducing a feedforward scaling factor called Nbar. The basic schematic with Nbar is shown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar because it was defined for continuous-time single-output systems. But certainly we can find it by trial and error. After several trials, Nbar equals -61.55 provided a satisfactory response. Try the following m-file and obtain the step response shown below.
T = 0:0.01:5; U = 0.2*ones(size(T)); F = [1.0000 0 0 0 G = [0.0001; 0.0182; 0.0002; 0.0454]; H = [1 0 J = [0; 0]; x = 5000; y = 100; Q = [x 0 0 0 R = 1; K = dlqr(F,G,Q,R) Nbar = -61.55; Ts = 1/100; sys_cl = ss(F-G*K,G*Nbar,H,J,Ts); [Y,T,X] = lsim(sys_cl,U); stairs(T,Y) legend('Cart (x)','Pendulum (phi)') 0 0 0 0 0 0 y 0 0; 0; 0; 0]; %weighting factor for the cart position %weighting factor for the pendulum angle 0 0 0 1 0; 0]; 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

Notice that the steady-state error of the cart's position have been eliminated. Now we have designed a system that satisfies all design requirements.

Observer design
The above response satisfies all design requirements; however, it was found assuming all states are measurable. This assumption may not be valid for all systems. In this section, we develop a technique for estimating the states of a plant from the information that is available concerning the plant. The system that estimates the states of another system is called an observer. Thus, in this section we will design a full-order state observer to estimate those states that are not measurable. For further explanation on how an observer works, please consult your control textbook. A basic schematic of the plant-observer system is shown below.

To design the observer, first, we need to find the L matrix. To find the L matrix, we need to find the poles of the system without the observer (the poles of F-G*K). Copy the following commands to a new m-file and run it in the MATLAB command window.
F = [1.0000 0 0 0 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

G = [0.0001; 0.0182; 0.0002; 0.0454]; H = [1 0 J = [0; 0]; x = 5000; y = 100; Q = [x 0 0 0 R = 1; K = dlqr(F,G,Q,R); poles = eig (F-G*K) 0 0 0 0 0 0 y 0 0; 0; 0; 0]; %weighting factor for the cart position %weighting factor for the pendulum angle 0 0 0 1 0; 0];

In the command window, you should see


poles = 0.9156 0.9156 0.9535 0.9535 + + 0.0729i 0.0729i 0.0079i 0.0079i

We want to place observer poles so that the observer dynamics are a lot faster than the system without the observer. Let's place the observer poles far left of above poles, say, at [-0.3 -0.31 -0.32 -0.33]. These poles can be changed later, if necessary. We will use the MATLAB function place to find the L matrix. Enter the following commands to an new m-file and run it.
F = [1.0000 0 0 0 H = [1 0 0 0 0.0100 0.9982 0.0000 -0.0045 0 1 0; 0]; 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

P = [-0.3 -0.31 -0.32 -0.33]; L = place (F',H',P)'

You should see the following L matrix in the command window.


L = 2.6310 172.8146 -0.0129 -2.2954 -0.0105 -1.3468 2.6304 173.2787

Now we will obtain the overall system response including the observer. Once again, create a new m-file and copy the following code.
T = 0:0.01:5; U = 0.2*ones(size(T)); F = [1.0000 0 0 0 G = [0.0001; 0.0182; 0.0002; 0.0454]; H = [1 0 J = [0; 0]; x = 5000; y = 100; Q = [x 0 0 0; 10 %weighting factor for the cart position %weighting factor for the pendulum angle 0 0 0 1 0; 0]; 0.0100 0.9982 0.0000 -0.0045 0.0001 0.0267 1.0016 0.3119 0.0000; 0.0001; 0.0100; 1.0016];

0 0 0 0; 0 0 y 0; 0 0 0 0]; R = 1; K = dlqr(F,G,Q,R) Nbar = -61.55; L = [2.6310 172.8146 -0.0129 -2.2954 -0.0105; -1.3468; 2.6304; 173.2787]; G*K; (F-L*H)];

Fce = [F-G*K zeros(size(F)) Gce = [G*Nbar; zeros(size(G))];

Hce = [H zeros(size(H))]; Jce = [0;0]; Ts = 1/100; sys_cl = ss(Fce,Gce,Hce,Jce,Ts); [Y,T,X] = lsim(sys_cl,U); stairs(T,Y) legend('cart (x)','pendulum (phi)')

After running this m-file, you should get the following step response.

As you noticed, this response is identical to the as previous one since observer knows the plant exactly and has the same initial condition.

Digital Control Examples


11

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

12

Digital Control Example: Designing Pitch Controller using State-Space method


Discrete state-space Controllability and observability Control design via pole placement Reference input
In this digital control version of the pitch controller problem, we will use the state-space method to design the digital controller. If you refer to the Pitch Controller: Modeling page, the state-space model was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output is the pitch angle (theta). The design requirements are Overshoot: Less than 10% Rise time: Less than 2 seconds Settling time: Less than 10 seconds Steady-state error: Less than 2%

Discrete state-space
The first thing to do here is to convert the continuous state-space model to discrete state-space. To do this, we will use the MATLAB function c2d. To use c2d, we need to specify three arguments: state-space system, sampling time (Ts), and the 'method'. You should already be familiar with how to construct a system from A, B, C, and D matrices. The sampling time should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency. The method we will use is the zero-order hold. From the closed-loop Bode plot, the bandwidth frequency was determined to be approximately 2
1

rad/sec (see this yourself) . Thus, to be sure we have a small enough sampling time, we will use the sampling time of 1/100 sec/sample. Now we are ready to use the function c2d. Enter the following commands in an m-file.
A = [-0.313 -0.0139 0 B = [0.232; 0.0203; 0]; C = [0 0 1]; D = [0]; 56.7 -0.426 56.7 0; 0; 0];

pitch = ss(A,B,C,D); Ts=1/100; pitch_d = c2d(pitch,Ts,'zoh')

Running this m-file in the MATLAB command window gives you the following four matrices.
a = x1 x2 x3 b = x1 x2 x3 c = y1 d = y1 Sampling time: 0.01 Discrete-time system. x1 0.99684 -0.00013849 -3.9309e-05 x2 0.5649 0.99571 0.56579 x3 0 0 1

u1 0.0023738 0.0002024 5.7438e-05

x1 0

x2 0

x3 1

u1 0

Now we have obtained the discrete state-space model of the form

Controllability and observability


The next step is to check the controllability and the observability of the system. For the system to be completely state controllable, the controllability matrix

must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

must also have the rank of n. Since our controllability matrix and observability matrix are 3x3, the rank of both matrices must be 3. The MATLAB function rank can give you the rank of each matrices. In an new m-file, enter the following commands and run it.
F = [0.9968 -0.0001 0 G = [0.0024; 0.0002; 0.0001]; H = [0 0 1]; J = [0]; 0.05649 0.9957 0.5658 0 0 1];

Ts = 1/100; pitch_d = ss(F,G,H,J,Ts); co = ctrb(pitch_d); ob = obsv(pitch_d); Controllability = rank(co) Observability = rank(ob)

In the command window, you should see

Controllability = 3 Observability = 3

This proves that our discrete system is both completely state controllable and completely state observable.

Control design via pole placement


The schematic of a full-state feedback system is shown below.

where K=Control matrix x=State matrix (alpha, q, theta) de=-Kx=input R=Reference In the continuous Pitch Controller: State-Space page, the Linear Quadratic Regulator (LQR) method was used to find the control matrix (K). In this digital version, we will use the same LQR method. This method allows you to find the optimal control matrix that results in some balance between system errors and control effort. Please consult your control textbook for details. To use this LQR method, we need to find three parameters: Performance index matrix (R), state-cost matrix (Q), and weighting factor (p). For simplicity, we will choose the performance index matrix equals 1 (R=1), and the state-cost matrix (Q) equals to H' x H. The weighting factor (p) will be chosen by trial and errors. The state-cost matrix (Q) has the following structure
Q = 0 0 0 0 0 0 0 0 1

Now we are ready to find the control matrix (K) and see the response of the system. First, let the weighting factor (p) equal 50. Enter the following commands into a new m-file and run it in the MATLAB command window.
t = 0:0.01:10; de = 0.2*ones(size(t)); F = [0.9968 -0.0001 0 0.05649 0.9957 0.5658 0 0 1];

G = [0.0024; 0.0002; 0.0001]; H = [0 0 1]; J = [0]; p = 50; Q = [0 0 0 0 0 0 0 0 p]; [K] = dlqr(F,G,Q,1) Ts = 1/100; sys_cl = ss(F-G*K,G,H,J,Ts); [Y,T] = lsim(sys_cl,de); stairs(T,Y)

After you run this m-file, you should see the control matrix (K) in the command window and the step response similar to the one shown below.

The rise time, the overshoot, and the settling time look satisfactory. However, there is a large steady-state error. This can be easily corrected by introducing the feedforwarding scaling factor (Nbar).

Reference input
Unlike other design methods, the full-state feedback system does not compare the output to the reference; instead, it compares all states multiplied by the control matrix (K*x) to the reference (see the schematic shown above). Thus, we should not expect to see the output equals to the input. To obtain the desired output, we need to scale the reference input so that the output equals to the reference. This can be easily done by introducing a feedforwarding scaling factor called Nbar. The basic schematic with the Nbar is shown below.

Unfortunately, we can not use our user-defined function rscale to find Nbar because it is defined for continuous systems. But we can find it by trial and error. After several trials, Nbar equals 6.95 provided a satisfactory response. Try the following m-file and obtain the stairstep response shown below.
t = 0:0.01:10; de = 0.2*ones(size(t)); F = [0.9968 -0.0001 0 G = [0.0024; 0.0002; 0.0001]; H = [0 0 1]; J = [0]; 0.05649 0.9957 0.5658 0 0 1];

p=50; Q = [0 0 0 0 0 0 0 0 p]; [K,S,E] = dlqr(F,G,Q,1) Nbar = 6.95; Ts = 1/100; sys_cl = ss(F-G*K,G*Nbar,H,J,Ts); [x] = lsim(sys_cl,de); stairs(t,x)

From this plot, we see that the Nbar factor eliminates the steady-state error. Now all design requirements are satisfied.

Note: Assuming all states are measurable, an observer design will not be explained here.

Digital Control Examples Pitch Control Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Digital Control Example: Ball and Beam problem using PID Control
Digital PID controller Discrete transfer function Open-loop response Proportional control Proportional-Derivative control
In this digital control version of the ball and beam experiment, we are going to use the PID control method to design the digital controller. If you refer to the Ball and Beam Modeling page, the open-loop transfer function was derived as

m mass of the ball 0.11 kg g gravitational acceleration 9.8 m/s^2 d lever arm offset 0.03 m L length of the beam 1.0 m R radius of the ball 0.015 m J ball's moment of inertia 9.99e-6 kgm^2 R(s) ball position coordinate (m) theta(s) servo gear angle 0.25 rad The design criteria for this problem are: Settling time less than 3 seconds Overshoot less than 5%

Digital PID controller


If you refer to any of the PID control problem for continuous systems, the PID transfer function was expressed as

As you noticed the above transfer function was written in terms of s. For the digital PID control, we use the following transfer function in terms of z.

Discrete transfer function


The first thing to do here is to convert the above continuous system transfer function to an equivalent discrete transfer function. To do this, we will use the MATLAB function c2d. To use c2d, we need to specify three arguments: system, sampling time (Ts), and the 'method'. You should already be familiar with how to create a system from numerator and denominator matrices. The sampling time should be smaller than 1/(30*BW) sec, where BW is the closed-loop bandwidth frequency. The method we will use is the zero-order hold ('zoh'). Assuming that the closed-loop bandwidth frequency is around 1 rad/sec, let the sampling time be 1/50 sec/sample. Now we are ready to use c2d. Enter the following commands to an m-file.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6; %simplifies input

K = (m*g*d)/(L*(J/R^2+m)); num = [-K]; den = [1 0 0]; ball = tf(num,den); Ts = 1/50; ball_d = c2d(ball,Ts,'zoh')

Running this m-file in the MATLAB command window gives you the following matrices.
Transfer function: 4.2e-05 z + 4.2e-05 ------------------z^2 - 2 z + 1 Sampling time: 0.02

Open-loop response
Now we will observe the ball's response to a step input of 0.25 m. To do this, enter the following commands into a new m-file and run it in the command window. You should see the following response.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Ts = 1/50; ball_d = tf(numDz,denDz,Ts); [x,t] = step(0.25*ball_d,5); stairs(t,x)

From this plot, it is clear that the open-loop system is unstable causing the ball to roll off the end of the beam.

Proportional Control
Now we will add proportional control (Kp) to the system and obtain the closed-loop system response. For now let Kp equal 100 and see what happens to the response. Enter the following commands into a new m-file and run it in the command window.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Ts = 1/50; ball_d = tf(numDz,denDz,Ts); Kp=100; sys_cl = feedback(Kp*ball_d,1); [x,t] = step(0.25*sys_cl,5); stairs(t,x)

As you can see, the addition of proportional control does not make the system stable. You may try to increase the proportional gain (Kp) and confirm that the system remains unstable.

Proportional-Derivative control
Now we will add a derivative term to the controller. Keep the proportional gain (Kp) equal to 100, and let the derivative gain (Kd) equal to 10. Copy the following code to an new m-file and run it to view the system response.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Ts = 1/50; ball_d = tf(numDz,denDz,Ts); Kp=100; Kd=10; numpd = [Kp+Kd -(Kp+2*Kd) Kd]; 3

denpd = [1 1 0]; contr = tf(numpd,denpd,Ts); sys_cl = feedback(contr*ball_d,1); [x,t] = step(0.25*sys_cl,5); stairs(t,x)

Now the system is stable, but the rise time is too long. From the PID Tutorial page, we see that the increasing the proportional gain (Kp) will decrease the rise time. Let's increase the proportional gain (Kp) to 1000 and see what happens. Change Kp in the above m-file from 100 to 1000 and rerun it in the command window. You should see the following step response.

As you can see, all of the design requirements are satisfied. For this particular problem, no implementation of an integral control was needed. But remember there is more than one solution for a control problem. For practice, you may try different P, I and D combinations to obtain a satisfactory response.

Digital Control Examples Ball & Beam Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Simulink C s
Bt u vi Simulink Cc File m hnh Cc phn t c bn Chy m phng Xy dng h thng Simulink l phn m rng ca MATLAB dng m hnh ho v m phng h thng. Trong Simulink,

h thng c v di dng s cc block. C rt nhiu phn t c bn trong s block c dng nh cc hm truyn, summing junctions, v.v., cng vi cc thit b vo-ra o nh cc b to hm v osilo. Simulink c tch hp trong MATLAB v d liu c truyn qua li d dng gia cc chng trnh. Trong phn tutor ny, chng ta p dng Simulink cho cc v d trong phn m hnh cc h thng, xy dng cc b iu khin v m phng h thng . Simulink c h tr trn mi trng Unix, Macintosh, v Windows; v gm c phin bn cho sinh vin chy trn my tnh c nhn. bit thm thng tin v Simulink, xin lin h vi MathWorks.

tng ng sau phn tutorial ny l bn c th xem xt mi th trong 1 ca s ring trong khi chng trnh Simulink ang c chy trong 1 ca s khc. Cc file m hnh ca h thng ta c th download d dng t phn tutorial v m chng trong phn mm Simulink. Bn c th chnh sa hoc m rng h thng trong qu trnh hc s dng Simulink m hnh ha h thng, iu khin, v m phng. Bn khng c lm ln xn cc ca s, biu tng v cc menu trong ca s thc hin Simulink. Cc hnh nh trong phn tutorial l nh ct chn - chng ch n gin cho bn thy nhng g s c c trong ca s lm vic ca Simulink. Tt c cc thao tc v Simulink s c thc hin trong ca s Simulink.

Bt u vi Simulink
Simulink c khi ng trong MATLAB bng cch nh lnh sau trong ca s lnh:
simulink

Vi la chn khc, bn c th nhn v nt New Simulink Model trong ca s lnh ca MATLAB nh sau:

Khi bt u, Simulink em n hai ca s. u tin l ca s main Simulink, nh sau:

Ca s th hai l phn trng, khng c mc v l ca s m hnh. y l ca s ta dng v m hnh h thng.

Cc File m hnh
Trong Simulink, mt m hnh l 1 s t hp cc block biu din cho 1 h thng. Hn na, v cc m hnh trong ca s trng ta c th m cc file m hnh lu trc t menu File trong ca s lnh ca MATLAB. V d, ta download file m hnh sau bng cch nhn vo lin kt sau v lu file trong th mc chy chng trnh MATLAB.
simple.mdl

M file ny trong Simulink bng cc lnh sau trong ca s lnh ca MATLAB. (Theo cch khc, ta c th m file ny bng cch dng chc nng Open trong menu File, hoc n Ctrl+O trong Simulink.)
simple

Ca s m hnh sau s xut hin.

M hnh mi c th c to ra bng cch chn New t menu File trong bt c ca s Simulink no (hoc n Ctrl+N).

Cc phn t c bn
C 2 lp item chnh trong Simulink: khi v ng ni. Cc khi c dng pht, iu ch, t hp, xut ra v hin th cc tn hiu. Cc ng ni c dng truyn tn hiu gia cc khi vi nhau.

Cc khi
C mt s lp cc khi tng qut nh sau: Cc khi ngun tn hiu: Dng pht cc tn hiu khc nhau Cc khi Sink: Dng xut tn hiu hoc hin th Cc khi ri rc: Cc phn t tuyn tnh, ri rc v thi gian trong h thng (cc hm truyn, m hnh khng gian trng thi, v.v..) Cc khi tuyn tnh: Cc khi v cc lin kt tuyn tnh, lin tc v thi gian (summing junctions, cc h s, v.v..) Cc khi phi tuyn: Cc ton t phi tuyn (cc hm ngu nhin, tp trung, tr, v.v..) Cc khi lin kt: Multiplex, Demultiplex, cc Macro h thng, v.v.. Cc block c cc im gia i vi cc im u vo v im u ra. Cc im u vo ng c biu din bng hnh tam gic nh h. u ra ng c biu din bi hnh tam gic nh. Hnh khi sau c 1 u vo ng bn tri v 1 u ra ng bn phi.

Cc ng ni
Cc ng ni truyn tn hiu theo hng mi tn. Cc ng ni lun lun truyn tn hiu t u ra ca khi ny n u vo ca khi khc. Trong 1 s trng hp 1 ng c th c chia nhnh t ng khc, v tn hiu c truyn ti c hai khi ch, nh sau (kch vo hnh minh ho download file m hnh split.mdl).

Cc ng th khng bao gi truyn tn hiu vo ng khc; cc ng ni phi c kt hp thng qua cch dng cc khi v d nh summing junction. Mt tn hiu c th l v hng hoc dng vecto. Vi cc h thng Single-Input, Single-Output(SISO),

tn hiu v hng thng xuyn c s dng. Vi cc h thng Multi-Input, Multi-Output(MIMO), tn hiu dng vecto li hay dng, v chnh l bao gm 2 hoc hn 2 tn hiu v hng. Cc ng ni dng truyn tn hiu v hng v tn hiu dng vecto l ng nht. Dng tn hiu trn 1 ng ni c xc nh bi cc khi hai im u v cui ca ng ni .

V d n gin

M hnh n gin trn (ly t phn file m hnh ) bao gm ba khi: Step, Hm truyn, v Scope. Step l khi ngun m t 1 xung u vo step c to ra. Tn hiu ny c truyn qua ng ni theo hng mi tn n hm truyn l 1 khi tuyn tnh. Hm truyn ny nhn tn hiu u vo v cho ra tn hiu u ra mi n Scope. Scope l 1 sink block dng n biu din 1 tn hiu no ging nh osilo. C rt nhiu kiu khi trong Simulink, chgng s c bn n sau. Ngay by gi, chng ta ch kho st 3 khi m ta dng trong m hnh trn.

iu chnh cc khi
Mt khi c th c iu chnh bng cch kch p ln n. V d, nu kch p ln khi "hm truyn" trong m hnh n gin trn, bn s thy hp thoi nh sau.

Hp thoi bao gm cc trng cho t s v mu s ca khi hm truyn. Bng cch nhp vo cc h s mong mun ca a thc t s v mu s, ta s c hm truyn nh mong mun. V d, thay i mu s thnh s^2+2s+1, ta nhp gi tr cho cc trng ca mu s nh sau:
[1 2 1]

v kch vo nt close, ca s m hnh s thay i nh di y ,

v iu ny nh hng n mu s ca hm truyn. Khi "step" cng c th c kch p cho php ta thay i thng qua hp thoi sau.

Cc gi tr tham s mc nh trong hp thoi ny cho ra 1 hm step ti thi im 1 giy, c gi tr thay i t mc 0 n mc 1. (ni cch khc l mt step n v ti thi im t=1). Mi tham s c th thay i c. Nh ng hp thoi trc khi tip tc. Khi phc tp nht trong ba khi chnh l khi "Scope". Kch p chut c mn hnh osilo trng.

Khi mt ln m phng c thc hin, tn hiu no cung cp cho scope s c biu din trong ca s ny. Cc thao tc chi tit ca scope s khng c bn nhiu trong phn ny. Ta ch dng 1 hm duy nht l nt autoscale, biu tng ca n ging nh 1 cp ng nhm trong phn pha trn ca ca s.

Chy m phng

chy m phng, ta s lm vic vi file m hnh sau:


simple2.mdl

Download v m file ny trong Simulink theo hng dn nu. Ta s c ca s m hnh nh sau.

Trc khi chy m phng cho 1 m hnh, trc tin ta m ca s scope bng cch kch p vo khi scope. Sau khi ng m phng c th nhn nt Start trong menu Simulation (nh bn di) hoc n Ctrl-T trong ca s m hnh.

M phng s chy rt nhanh v cho ra ca s scope nh di y.

Ch l u ra ca m phng (c mu vng) mc rt thp so vi cc trc to ca scope. chnh li, n nt autoscale (hnh cp ng nhm), n s chnh li t l cc trc to nh sau.

Ch rng p ng n v cha c bt u cho n khi t=1. Ta c th iu chnh c bng cch kch p vo khi "step". By gi ta s thay i cc tham s cho h thng v m phng li ln na. Kch p ln khi "Hm truyn" trong ca s m hnh v thay mu s thnh
[1 20 400]

Chy m phng li (n Ctrl-T) v bn s thy nhng g xut hin ch l 1 ng bng phng trn ca s scope. n nt autoscale, v bn s thy ca s scope nh sau.

Nh rng nt autoscale ch thay i trc tung. V hm truyn ny c p ng rt nhanh, n c dn vo 1 khong rt hp trong ca s scope. iu ny khng c vn g i vi scope, nhng vi chnh bn thn m phng th c. Simulink m phng h thng trong 10 giy mc d h thng t trng thi n nh ch sau khong 1 giy. sa li, bn phi thay i li cc tham s ca chnh bn thn m phng. Trong ca s m hnh, chn Parameters t menu Simulation. Bn s thy hp thoi sau.

C rt nhiu mc la chn cho tham s; chng ta ch quan tm ti thi gian bt u v kt thc, iu ny s cho Simulink thc thi m phng trong khong thi gian no. Thay i Start time t 0.0 thnh 0.8 (v xung step cha xut hin n khi t=1.0). Thay i Stop time t 10.0 thnh 2.0, gn ngay sau khi h thng n nh. ng ca s ny li v chy m phng li. Sau khi n nt autoscale, ca s scope s cho ta biu din p ng n v tt hn nh sau.

Xy dng h thng
Trong phn ny, bn s hc cch xy dng h thng trong Simulink bng cch dng cc khi trong Th vin khi ca Simulink. Ta s xy dng h thng sau.

Nu bn mun download m hnh hon chnh, nhn vo y. Trc tin bn phi tp hp c tt c cc khi cn thit t th vin cc khi. Sau phi chnh li cc khi cho tng ng vi cc khi trong m hnh nh mong mun. Cui cng, bn s kt ni cc khi bng cc ng ni to nn 1 h thng hon chnh. Sau bc ny, bn s m phng h thng kim nghim li xem n hot ng ra sao.

Tp hp cc khi
Thc hin cc bc nh sau chn cc khi cn thit: To mt m hnh mi (New t menu File hoc Ctrl-N). Bn s c ca s m hnh trng.

Kch p vo biu tng Sources trong ca s chnh ca Simulink.

Thao tc ny s cho ta ca s Sources cha cc th vin Source. Cc khi Source c dng pht tn hiu. bit thm v th vin cc khi Kch vo y.

Ko khi Step trong ca s sources t vo pha bn tri ca ca s m hnh.

Kch p vo biu tng Linear trong ca s chnh ca Simulink m ca s th vin Linear Block Library. Ko cc khi Sum, Gain, v 2 khi Hm Truyn (ko 2 ln) vo ca s m hnh v sp xp gn c nh sau. S sp xp chnh xc khng cn thit lm v sau ny ta s chnh li. Ch cn xc inh ng v tr tng i ca cc khi. Ch l hm truyn th hai c s 1 sau tn ca n. V hai khi khng th c cng 1 tn, nn Simulink t ng thm cc ch s vo sau tn ca cc khi phn bit chng vi nhau.

10

Kch p vo biu tng Sinks trong ca s chnh ca Simulink m ca s cc Sink. Ko khi Scope vo pha bn phi ca ca s m hnh.

Chnh li cc khi
Tin hnh cc bc sau chnh li cc khi trong m hnh. Kch p vo khi Sum. V ta mun u vo th 2 l php tr, nhp +- vo danh sch cc trng du. ng hp thoi li. Kch p vo khi Gain block. Thay h s t l thnh 2.5 v ng hp thoi li. Kch p vo Hm truyn gn bn tri nht. Thay i t thnh [1 2] v mu thnh [1 0]. ng hp thoi li. Kch p vo Hm truyn gn bn phi nht. Gi nguyn t l [1], Nhng thay mu thnh [1 2 4]. ng hp thoi li. M hnh ca ta s c dng sau:

11

Thay tn ca hm truyn th nht bng cch kch vo ch "Transfer Fcn". Mt hp v du nhc s xut hin trn tn ca khi nh sau. Dng bn phm (dng chut vn hu ch) xo tn hin ti v nhp tn mi cho vo l "PI Controller". Kch vo bt c u bn ngoi hp tn hon tt.

Tng t, ta thay tn ca hm truyn th hai t "Transfer Fcn1" thnh "Plant". By gi, tt cc khi u c c tn thch hp. M hnh ca ta s nh sau:

Kt ni cc khi bng cc ng
By gi cc khi c b tr thch hp, ta s lin kt chng vi nhau. Tin hnh theo cc bc sau. Ko chut t u ra ca khi Step n u vo pha trn (dng) ca khi Sum. Th chut khi no nhp ng vo im u vo. Khng nn quan tm n dng ca ng ni khi ang v,
12

ng ni s t ng chy. Bn s thy nh sau.

Kt qu l ng ni s c 1 mi tn t m u. Nu mi tn l h, nh di y, th c ngha l n khng ni ti u c.

Bn c th tip tc vi ng ni khng hon thin bng cch coi nh mi tn h l mt im u ra v li v li nh ban ny. Tip na, nu bn mun v li ng ni hoc ng ni lin kt sai, bn nn xo n i v v li n. xo 1 ng (hay bt c i tng no), kch chut chn chng sau nhn phm delete. Ko mt ng ni t u ra khi Sum n u vo khi Gain. Cng v tip 1 ng t khi Gain n b iu khin PI, 1 ng t b iu khin PI n khi Plant, v 1 ng t khi chp hnh ti Scope. Bn s c nh sau.

ng ni cn li cn v l tn hiu phn hi t u ra ca khi Plant v u vo m ca khi Sum. ng ni ny s khc bi hai l l. Th nht, v ng ny i vng v khng i theo ng ngn nht (gc vung) nn n c v qua 1 s giai on. Th hai, v khng c im
13

output bt u nn ng ny phi bm vo 1 ng no c sn. bm vo 1 ng output, gi Ctrl trong khi ko chut t im mong mun trn ng c sn. Trong trng hp ny, ta bt u t bn phi ca khi Plant. Ko n gc tri pha dc ca ng tn hiu phn hi nh hnh sau.

By gi mi tn h ca ng ni c coi nh l 1 im output. V 1 ng t n ti im m ca khi Sum nh thng thng.

By gi bn c th cn chnh li cc khi cho gn gng hn. Khi sp xp li, v tr ca cc khi khng l vn lm, nhng r rng l s d c hn khi chng c sp hng li. di chuyn mi khi, ko n bng chut. Cc ng s vn gi lin kt v t ni li. Cc on gia v gc ca cc ng c th c ko n v tr khc. Bt u t bn tri, ko tng khi 1 sao cho ng ni nm thng ngang. Cng lc cng iu chnh lun khong cch gia cc khi 1 cch hp l c ch cho cc nhn tn hiu. Bn s c nh sau:

14

Cui cng, bn s t cc nhn xc nh cc tn hiu tng ng. t 1 nhn bt c u trong m hnh, kich p vo im bn mun t nhn. Ta bt u bng cch kch p vo ng dn t khi Step. Bn s c mt hp vn bn trng vi du nhc nh di y.

l ta t nhn cho tn hiu reference sau kch ra ngoi kt thc. t nhn cho tn hiu sai s (e), tn hiu iu khin (u), v tn hiu u ra (y) nh thng thng. M hnh cui cng ca ta s c dng nh sau:

nh r vo

lu li m hnh, chn Save As trong menu File v nh tn theo bn. M hnh hon chnh s c khi kch vo y.

M phng
By gi m hnh hon thin, bn c th cho chy m phng n. Chn Start t menu Simulation chy. Kch p vo khi Scope xem tn hiu output. Nhn vo nt autoscale (hnh ng nhm) v
15

bn s thy nh sau.

Ly bin t Matlab
Trong mt s trng hp, cc tham s nh h s chng hn, c th c tnh ton trong MATLAB sau s dng trong 1 m hnh Simulink. Nu c trng hp ny th ta khng cn thit phi nhp trc tip kt qu tnh ton t MATLAB vo Simulink. V d, gi s ta tnh ton h s thng qua bin K trong Matlab. a bin ra bng cch thc hin lnh sau trong ca s lnh ca MATLAB.
K=2.5

Bin ny c th c dng trong khi Gain ca Simulink. Trong m hnh Simulink ca bn, kch p chut vo khi Gain v nhp K vo trng Gain.
K

ng hp thoi li. Ch rng khi Gain trong m hnh Simulink s biu din K ch khng phi l 1 con s.

16

By gi bn chy m phng li v xem tn hiu output trn Scope. Kt qu s nh sau.

By gi nu bt c 1 php tnh no c thc hin trong MATLAB lm thay i gi tr ca bin trong m hnh Simulink th b m phng s dng cc gi tr mi ny chy. th iu ny, trong MATLAB, thay gi tr ca K bng cch nh lnh sau.
K=5

Khi ng m phng li ln na, a ca s Scope ra va n nt autoscale. Bn s thy u ra khc phn nh gi tr mi ln hn ca h s.

17

Bn cnh cc bin th tn hiu, v ngay c ton b h thng c th c trao i qua li gia MATLAB v Simulink. bit thm, kch vo y.

Tutorials

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

18

To m hnh trong Simulink


H thng xe la Biu gii phng lin kt v nh lut Newton Cu trc m hnh Chy thc thi m hnh Obtaining MATLAB Model
Trong Simulink, rt d dng biu din 1 cch thng thng mt h vt l hoc mt m hnh. Tu chung li, mt h ng lc hc c th c xy dng t cc nh lut vt l c bn. Chng ta s minh ho iu ny thng qua mt v d.

H thng xe la
Trong v d ny, chng ta xem xt 1 chic xe la chi bao gm 1 ng c v 1 xe ko. Gi s rng xe la ch di chuyn theo 1 hng, chng ta cn iu khin sao cho xe la khi ng v dng li tht m, theo l chuyn ng thng vi vn tc khng i. Trng lng ca ng c v xe ko biu din l M1 v M2 tng ng. Hai vt c ni vi nhau bi 1 l xo c cng tng ng l k. F l lc ko ca ng c, v ch ci Hy lp muy (biu din l u) l h s ma st ln.

Biu gii phng lin kt v nh lut Newton


H thng c th c biu din thng qua Biu gi phng lin kt sau.

T nh lut Newton, ta bit rng tng cc lc tc dng ln 1 khi lng bng tch ca khi lng v gia tc ca n. Trong trng hp ny, cc lc tc dng ln M1 l lc n hi l xo, lc ma st v lc ko ca ng c. Cc lc tc dng vo M2 l lc n hi l xo v lc ma st. Theo phng thng ng, trng lc cn bng vi phn lc ca mt t, do vy khng c gia tc theo phng thng ng.
1

Chng ta s bt du xy dng m hnh bng m t n gin:


Sum(forces_on_M1)=M1*x1'' Sum(forces_on_M1)=M1*x1''

Xy dng m hnh
Tp cc phng trnh ny c th c m t bng ho, khng cn thm thao tc no khc. Trc tin, ta xy dng hai bn copy (cho mi khi lng) cho biu thc tng_F=Ma hay l a=1/M*tng_F. M mt ca s m hnh mi, ko hai khi Sum (t th vin Linear), mt khi pha trn khi kia. nh nhn cho cc khi Sum l "Sum_F1" and "Sum_F2".

Cc u ra output ca mi khi Sum biu din cho tng cc lc tc dng ln mi khi lng. Khi nhn vi 1/M s cho ta gi tr gia tc. Ko hai khi Gain vo m hnh v ni chng vi u ra ca cc khi Sum.

Cc khi Gain ny s l 1/M tng ng vi tng khi lng. Chng ta s ly cc bin M1 v M2 t MATLAB, nh vy ta ch cn nhp bin cho mi khi Gain. Kch p ln khi Gain pha trn v nhp vo trng Gain thnh.
1/M1

Tng t cho khi Gain th hai thnh.


1/M2

By gi bn c th thy rng h s khng thy hin ln trn khi Gain, v ch c hnh "-K-" hin ln. iu ny bi v hnh khi ca chng qu nh hin th c 1/M2 ln trn. Cc khi c th chnh c kch thc sao cho h s c th nhn thy c. chnh li kch thc cho 1 khi, kch chut chn n. Cc hnh vung nh s hin ln 4 gc. Ko cc hnh vung nh m rng khi.

Khi kch thc khi Gain hin th h s, hy sp xp li chng thng theo ng tn hiu. Cng th, nh nhn cho hai khi Gain "a1" v "a2".

u ra ca cc khi Gain l gia tc ca mi khi lng. Chng ta quan tm ti c vn tc v v tr ca ca cc khi lng. V vn tc l tch phn ca gia tc, nn chng ta c th to ra cc tn hiu bng cch s dng cc khi tch phn. Ko hai khi tch phn vo m hnh i vi hai tn hiu gia tc. Kt ni chng bng cc ng thnh hai chui ging nh sau. nh nhn cho cc khi tch phn l "v1", "x1", "v2", v "x2" v chng l cc tn hiu m cc khi ny to ra.

By gi, ko hai khi Scope t th vin Sinks vo m hnh v lin kt chng vi cc u ra ca cc khi tch phn ny. nh nhn chng l "View_x1" v "View_x2".

By gi chng ta sn sng thm cc lc tc dng vo mi khi lng. Trc tin, bn cn tnh ton cc u vo cho mi khi Sum mt cch chnh xc (cc k hiu s bn sau). C 3 tc ng ln M1, nn ta thay i hp thoi ca khi Sum_F1 thnh:
+++

Ch c 2 lc tc dng ln M2, nn ta vn gi nguyn khi Sum_F1.

Lc u tin tc dng ln M1 l lc u vo F. Ko khi Signal Generator t th vin Sources v lin kt n vi u vo pha trn cng ca khi Sum_F1. nh nhn cho Signal Generator l "F".

Lc tip theo tc dng ln M1 l lc ma st. Lc ny tnh bng:


F_friction_1=mu*g*M1*v1

to ra tn hiu ny, chng ta phi trch tn hiu vn tc v nhn vi mu*g*M1. Ko thm 1 khi Gain vo m hnh. Trch tn hiu t khi tch phn v1 v ni n vo u vo ca khi Gain (c th phi qua 1 s thao tc v nu cn). Lin kt u ra ca khi Gain ny vi u vo th hai ca khi Sum_F1. Thay i h s ca khi Gain thnh.
mu*g*M1

Chnh li kch thc ca khi Gain c th hin th c h s v nhn l Friction_1.

Lc ny l lc tc ng ngc chiu i vi x1. Do vy u vo khi Sum_F1 ca n phi c du -. Thay i cc du ca khi Sum_F1 thnh
+-+

Lc cui cng tc dng vo M1 l lc n hi ca l xo lp gia hai khi lng. Lc ny bng:


k*(x1-x2)

Trc ht, ta phi to ra tn hiu (x1-x2) sau nhn vi k to ra lc n hi. Ko thm 1 khi Sum vo phn cn li ca m hnh. nh nh n l "(x1-x2)" v thay i du tn hiu u vo thnh
-+

V tng ny thc hin t phi sang tri nn ta phi xoay khi ny li. Chn khi bng cch kch chut vo n v chn Flip t menu Format (hoc nhn Ctrl-F). Bn s thy nh sau.

By gi trch tn hiu x2 v ni n vi u vo - ca khi (x1-x2). Trch tn hiu x1 v ni n vi u vo + tip theo. ng tc ny s lm cho cc ng ni ct nhau. Cc ng c th ct nhau, nhng chng thc s lin kt vi nhau ch c cc khi nh (v d nh im trch).

By gi chng ta c th nhn gi tr sai lch v tr ny vi hng s ca l xo c lc n hi. Ko mt khi Gain vo pha tri ca khi Sum. Thay h s ca n thnh k v nh nhn l "spring". Lin kt u ra ca khi (x1-x2) vi u vo ca khi spring, v u ra ca khi spring vi u vo th 3 ca khi Sum_F1. i du u vo khi Sum_F1 thnh +--.

By gi chng ta c th t cc lc vo M2. Lc th nht chnh l lc l xo m chng ta va to ra, ch khc l chiu tc ng l m. n gin ch cn trch tn hiu u ra ca khi spring v ni n vi u vo th nht ca khi Sum_F2.

Lc cui cng ta cn thm vo l lc ma st tc ng ln M2. iu ny ta thc hin ging nh lm i vi M1, trch tn hiu v2, nhn vi mu*g*M2 v lin kt vo Sum_F2 vi du l m. Sau khi thc hin xong, bn s c m hnh nh sau.

By gi m hnh hon tt. By gi chng ta ch cn cho ng tn hiu vo v quan st u ra. u vo ca h thng l lc pht ng F ca ng c. Chng ta c hm to tn hiu u vo. u ra ca h thng l vn tc ca u my. Ko 1 khi Scope t th vin Sinks vo m hnh ca bn. Trch tn hiu ra "v1" t khi tch phn cho vo khi xem c u ra vn tc. nh nhn khi scope mi l "View_v1".

By gi m hnh xong, ghi li di tn file no tu bn. Bn cng c th download m hnh hon chnh y.

Chy thc thi m hnh


Trc khi cho chy m hnh, chng ta cn xc nh cc gi tr tham bin dng trong m hnh. Vi m hnh xe la, cho M1 = 1 kg M2 = 0.5 kg k = 1 N/sec

F= 1 N u = 0.002 sec/m g = 9.8 m/s^2

To 1 m-file mi c cc lnh sau.


M1=1; M2=0.5; k=1; F=1; mu=0.002; g=9.8;

Chy m-file ny xc nh cc gi tr ny. Simulink s nhn ra cc bin ca MATLAB v s dng chng trong m hnh. By gi chng ta cn to ra u vo gn ng cho u my. Kch p vo khi to hm (khi F). Chn dng sng vung vi tn s 0 .001Hz v bin l -1 (v bin dng s to ra mc m trc mc dng).

Bc cui cng trc khi chy m phng l chn thi gian m phng. quan st 1 chu k ca tn s 0.001Hz , chng ta phi m phng trong 1000 giy. Chn Parameters t menu Simulation v thay i trng Stop Time thnh 1000. Sau ng hp thoi li. By gi hy chy m phng v m View_v1 xem u ra vn tc (n autoscale). u vo l sng vung vi hai bc, 1 m v 1 dng. Nhn t kha cnh vt l, ta thy u my ban u di chuyn tin ln, sau li li. u ra vn tc phn nh r iu ny.

10

Obtaining M ATLAB Model


Chng ta cng c th to ra 1 m hnh trong MATLAB (v d nh m hnh khng gian trng thi hoc hm truyn) t m hnh simulink. lm iu ny, xo khi scope View_v1 thay vo l 1 khi Out (Trong th vin Connections). Cng th, xa khi to hm F v thay vo l 1 khi In (Trong th vin Connections). Cc khi vo v ra ny xc inh u vo v u ra ca h thng m ta cn to. bit chi tit v cch x l v cc cch tch hp MATLAB v Simulink vi nhau kch vo y.

Lu m hnh di dng file train2.mdl hoc download m hnh y. By gi chng ta c th to ra m hnh trong MATLAB. nh lnh sau trong ca s lnh ca MATLAB xut ra m hnh khng gian trng thi.
[A,B,C,D]=linmod('train2')

Bn s thy n cho ra cc ma trn xc nh 1 m hnh khng gian trng thi tng ng vi m hnh trong Simulink nh sau.
A = -0.0196 0 0 -0.0196 1.0000 -2.0000 -1.0000 2.0000 11

0 1.0000 B = 1 0 0 0 C = 1 D = 0 0

1.0000 0

0 0

0 0

c m hnh hm truyn, nh cc lnh sau trong MATLAB.


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

Bn s thy n cho ra t v mu ca hm truyn tng ng.


num = 0 den = 1.0000 0.0392 3.0004 0.0588 0.0000 1.0000 0.0196 2.0000 0.0000

Cc m hnh ny u tng ng (mc d cch pht biu khc nhau) vi m hnh xy dng bng tay trong phn MATLAB.

Simulink Examples Tutorials

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

12

V d: M hnh xe trong Simulink


Ci t cc thng s vt l v cc phng trnh h thng Xy dng m hnh p ng ca h h Truy xut m hnh Thc thi b iu khin PI p ng ca h kn

Ci t cc thng s vt l v cc phng trnh h thng


M hnh ca h thng iu khin xe theo 1 cch no l tng i n gin. Nu momen qun tnh ca bnh c b qua, v gi s ma st (t l vi vn tc ca xe) l lc duy nht cn tr chuyn ng ca xe, th bi ton c rt gn thnh h khi lng v gim chn n gin nh sau.

Dng nh lut Newton, cc phng trnh ca h nh sau: (1) y u l lc pht ng ca ng c. Vi v d ny, gi s rng

m = 1000kg b = 50Nsec/m u = 500N

Xy dng m hnh
H thng ny c th c m hnh bng cch tnh tng cc lc tc dng vo khi lng v tch phn gia tc tnh ra vn tc. M Simulink v m 1 ca s m hnh mi. Trc tin, chng ta s m hnh ho tch phn gia tc

Thm 1 Khi tch phn (t th vin Linear) v v cc ng vo v ra cho khi. nh nhn ng vo l "vdot" v ng ra l "v" nh bn di. thm 1 nhn, kch p vo vng trng pha trn ng tng ng.

V gia tc (dv/dt) bng tng cc lc chia cho khi lng, chng ta s chia tn hiu u vo cho khi lng. Thm 1 khi Gain (t th vin Linear) v ni n vi ng vo ca khi tch phn v v 1 ng vo cho u vo khi gain . Thay i li khi gain bng cch kch p ln n v thay gi tr thnh "1/m". Thay nhn ca khi Gain thnh "inertia" bng cch kch vo t "Gain" pha di khi .

By gi, chng ta s thm cc lc nh trong phng trnh (1). Trc tin, chng ta thm lc cn. Gn thm 1 khi Sum (t th vin Linear) vo ng dn ti khi qu tnh. Thay du ca khi Sum ny thnh "+-". Thm 1 khi gain vo pha di khi qun tnh, chn n bng cch kch n, v chn Flip t menu Format (hoc n Ctrl-F) lt n t bn tri qua bn phi. t gi tr h s l "b" v i tn khi thnh "damping". Trch 1 ng (Gi Ctrl khi v) t u ra ca khi tch phn v ni vi u vo ca khi damping. V 1 ng t u ra khi damping ti u vo - ca khi Sum.

Lc th hai tc dng ln khi lng l lc u vo iu khin, u. Chng ta s cho n l u vo step. Thm 1 khi Step (t th vin Sources) v ni n vi u vo + ca khi Sum. xem vn tc u ra, thm 1 khi Scope (t th vin Sinks) v ni n vi u ra ca khi tch phn.

c u vo step gn ng bng 500 ti t=0, kch p vo khi Step v t Step Time bng "0" v Final Value l "u".

p ng ca h h
mo phng h thng trn. Trc ht, ta phi chn thi gian m phng hp l. Chn Parameters t menu Simulation v nhp "120" vo trng Stop Time. 120 giy l di
3

xem p ng ca h h.

Cc thng s vt l c chn. Thc hin cc dng lnh sau trong ca s lnh ca MATLAB:
m=1000; b=50; u=500;

Cho chy m phng (n Ctrl-t hoc chn Start trn menu Simulation). Khi kt thc m phng, kch p vo khi scope v nhn vo nt autoscale. Bn s c u ra nh sau.

Truy xut M hnh tuyn tnh vo M ATLAB


Mt m hnh tuyn tnh ca 1 h thng ( dng khng gian trng thi hoc dng hm truyn) c th truy xut t Simulink vo MATLAB. iu c thc hin thng qua cc khi lin kt In v Out v hm linmod.

Thay cc khi Step Block v Scope bng khi In Connection v Out Connection tng ng (cc khi ny ly t th vin Connections). N xc nh u vo v ra ca h thng c th truy xut ra.

Lu m hnh di dng file "ccmodel.mdl" (chn Save As t menu File). MATLAB s truy xut ra m hnh tuyn tnh t file m hnh ch khng cn phi lm t 1 ca s m hnh. Trong ca s lnh MATLAB, thc hin cc lnh sau:
[A,B,C,D]=linmod('ccmodel') [num,den]=ss2tf(A,B,C,D)

Bn s thy lnh trn cho ra cc thng s v m hnh h thng di c dng khng gian trng thi v hm truyn.
A = -0.0500 B = 1.0000e-003 C = 1 D = 0 num = 0 den = 1.0000 0.0500 0.0010

kim nghim li vic truy xut, chng ta s ly p ng step ca h h i vi hm truyn ny trong MATLAB. Chng ta nhn t s hm truyn vi 500 m phng u vo step 500N. Thc hin lnh sau trong MATLAB.
5

step(500*num,den);

Bn s thy th cho ra tng ng vi th ca Scope.

Thc thi b iu khin PI


Trong v d v iu khin xe 1 b iu khin PI c thit k vi Kp=800 v Ki=40 t c p ng mong mun. Chng ta s th thi n trong Simulink bng cch a m hnh lp trc vo phn ny di dng 1 khi h thng con. To 1 ca s m hnh mi. Ko 1 khi Subsystem t th vin Connections vo ca s m hnh ca bn.

Kch p vo khi . Bn s thy 1 ca s trng biu din ni dung ca h thng con (hin thi cha c g). M M hnh iu khin xe lu trc ra, ccmodel.mdl. Chn Select All t menu Edit (hoc Ctrl-A), sau chn Copy t menu Edit(hoc Ctrl-C). Chn ca s h thng con rng v chn Paste t menu Edit (hoc Ctrl-V). Bn s thy h thng gc trong ca s h thng con mi. ng ca s li.

Bn s thy xut hin u vo v u ra ca khi Subsystem. t tn khi ny l "plant model".

By gi chng ta s xy dng 1 b iu khin PI cho plant model. Trc tin, chng ta phi ly tn hiu phn hi ca u ra. V 1 ng ko ra t u ra ca khi plant. Thm 1 khi Sum v t "+-" cho cc u vo ca n. Trch 1 ng t u ra v ni n vi u vo - ca khi Sum.

u ra ca khi Sum s cho bit tn hiu v sai s. T , chng ta to lp cc thnh phn t l v tch phn. Thm 1 khi tch phn vo pha sau khi sum v ni chng vi nhau. Thm v lin kt 1 khi gain vo sau khi tch phn, l h s ca khu tch phn. nh nhn cho khi h s tch phn l Ki v gn gi tr h s l Ki. Thm 1 khi Gain v ni n vi 1 ng trch t u ra ca khi Sum. nh nhn cho n l Kp v gn gi tr h s l Kp.

By gi chng ta c cc thnh phn t l v tch phn, ta s cho tng ca chng vo vo khu chp hnh. Thm 1 khi Sum vo gia khi Ki v khu chp hnh sau ni cc u ra ca 2 khi gain vo cc u vo ca khi Sum mi. Ni u ra ca khi Sum mi ti u vo ca khu chp hnh.

Cui cng, chng ta s a vo tn hiu step v quan sat u ra trn scope. Gn 1 khi step vi u vo cn li ca khi Sum tn hiu phn hi. Gn 1 khi Scope vi u ra ca khu chp hnh. Kch p ln khi Step v t Step Time l "0" v Final Value l "u". iu ny cho php ta c th thay i bin t bn ngoi simulink.

Bn c th download h thng kn ca chng ti bng cch kch vo y here.

Trong v d ny, chng ta thit lp1 b iu khin PI t cc khi c bn. V sau ny, chng ta dng 1 khi hm truyn (t th vin Linear) thc hin lun trong 1 bc, nh di y.

Bn c th download m hnh bng cch kch vo y here.

p ng ca h kn
m phng h thng, trc tin ta cn chn khong thi gian m phng sao cho hp l. Chn Parameters t menu Simulation v nhp "10" vo trng Stop Time. Yu cu thit k bao gm thi gian qu nh hn 5 giy, nn chng ta m phng trong 10 c th xem u ra nh th no. Cc tham s vt l cn c chn. Thc hin cc lnh sau trong MATLAB:
m=1000; b=50; u=10; Kp=800; Ki=40;

Chy m phng (n Ctrl-t hoc Start t menu Simulation). Khi m phng kt thc, kch p vo khi scope v n nt autoscale. Bn s thy u ra nh sau.

Simulink Examples

iu khin xe | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch

Controller | Ball and Beam

Cc v d v iu khin xe Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

10

V d: M Hnh ng C 1 Chiu trong Simulink


Ci t cc thng s vt l v cc phng trnh h thng Xy dng m hnh p ng ca h h Truy xut m hnh Thc thi b iu khin PI p ng ca h kn

Ci t cc thng s vt l v phng trnh h thng


Mt trong nhng thit b chp hnh thng dng trong h thng iu khin l ng c in 1 chiu. N trc tip to ra chuyn ng quay v kt hp vi cc bnh dn hoc tang trng c cp truyn chuyn ng. Mch in phn ng v biu tch lin kt ca roto c cho nh sau:

Vi v d ny, gi thit cc thng s vt l c gi tr nh sau. * momen qun tnh ca roto (J) = 0.01 kg.m^2/s^2 * h s cn do h c kh (b) = 0.1 Nms * hng s lc in ng (K=Ke=Kt) = 0.01 Nm/Amp * in tr (R) = 1 ohm * in khng (L) = 0.5 H * u vo (V): ngun in p * u ra (theta): gc quay ca trc * roto v trc coi nh l khng bin dng Momen quay T c quan h vi dng in phn ng i bi h s Kt. Sc in ng e quan h vi vn tc quay c biu din bi cc phng trnh sau:

Trong n v SI (ta s dng), Kt (hng s phn ng) bng Ke (hng s ca ng c).

Xy dng m hnh
H thng ny c m hnh bng cch ly tng cc momen quay tc dng ln v ly tch phn gia tc cho ra vn tc. ng thi, ta s p dng nh lut Kirchoff vo mch phn ng. M Simulink v m 1 ca s m hnh mi. Trc ht, ta s m hnh tch phn ca gia tc quay v bin i ca dng phn ng.

Thm 1 khi tch phn (t th vin Linear) v v cc ng vo v ra cho cc u vo ra ca n. nh nhn ng vo l "d2/dt2(theta)" v ng ra l "d/dt(theta)" nh bn di. thm cc nhn , kch p vo vng trng pha trn cc ng. Thm 1 khi tch phn vo pha trn khi trc v cng v cc ng vo v ra cho cc u vo ra ca n. nh nhn cho ng vo l "d/dt(i)" v ng ra l "i".

Tip theo, chng ta s m hnh c nh lut Newton v Kirchoff. Cc nh lut ny khi p dng vo h thng ng c cho ta cc phng trnh sau:

Gia tc gc bng 1/J nhn vi tng cc i lng (1dng, 1 m.). Tng t, o hm ca dng in bng 1/L nhn vi tng ca 3 i lng (1 dng, 2 m.). Thm 2 khi Gain vo m hnh, (t th vin Linear) mi khi c gn vo tng khi tch phn. Thay gi tr h s ca khi gain tng ng vi gia tc gc "1/J". Thay nhn cho khi Gain ny thnh "inertia". Tng t, thay gi tr cho khi gain cn li l "1/L" v nhn l Inductance. Thm hai khi Sum (t th vin Linear), mi khi gn vi tng khi Gain. Thay du u vo ca khi Sum tng ng vi gc quay l "+-" c 1 i lng l dng, 1 i lng l m. Thay du u vo khi Sum cn li thnh "-+-" biu din cho cc du trong phng trnh Kirchoff.

By gi, chng ta s thm cc momen quay trong phng trnh Newton. Trc ht, ta thm momen cn. Thm 1 khi gain vo pha di khi qun tnh, kch chut chn n v chn Flip trong menu Format (hoc nhn Ctrl-F) to flip it left-to-right. Set the gain value to "b" and rename this block to "damping". Tap a line (hold Ctrl while drawing) off the rotational integrator's output and connect it to the input of the damping gain block. Draw a line from the damping gain output to the negative input of the rotational Sum block. Next, we will add in the torque from the armature. Insert a gain block attached to the positive input of the rotational Sum block with a line. Edit it's value to "K" to represent the motor constant and Label it "Kt".

Continue drawing the line leading from the current integrator and connect it to the Kt gain block.

Now, we will add in the voltage terms which are represented in Kirchoff's equation. First, we will add in the voltage drop across the coil resistance. Insert a gain block above the inductance block, and flip it left-to-right. Set the gain value to "R" and rename this block to "Resistance". Tap a line (hold Ctrl while drawing) off the current integrator's output and connect it to the input of the resistance gain block. Draw a line from the resistance gain output to the upper negative input of the current equation Sum block. Next, we will add in the back emf from the motor. Insert a gain block attached to the other negative input of the current Sum block with a line. Edit it's value to "K" to represent the motor constant and Label it "Ke". Tap a line off the rotational integrator output and connect it to the Ke gain block.

The third voltage term in the Kirchoff equation is the control input, V. We will apply a step input. Insert a Step block (from the Sources block library) and connect it with a line to the positive input of the current Sum block. To view the output speed, insert a Scope (from the Sinks block library) connected to the output of the rotational integrator. To provide a appropriate unit step input at t=0, double-click the Step block and set the Step Time to "0".

You can download a model file for the complete system here.

Open-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the

Simulation menu and enter "3" in the Stop Time field. 3 seconds is long enough to view the open-loop response. The physical parameters must now be set. Run the following commands at the MATLAB prompt:
J=0.01; b=0.1; K=0.01; R=1; L=0.5;

Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Extracting a Linear Model into MATLAB


A linear model of the system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. First, replace the Step Block and Scope Block with an In Connection Block and an Out Connection Block, respectively (these blocks can be found in the Connections block library). This defines the input and output of the system for the extraction process.

Save your file as "motormod.mdl" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands:
[A,B,C,D]=linmod('motormodel') [num,den]=ss2tf(A,B,C,D)

You should see the following output, providing both state-space and transfer function models of the system.
A = -10.0000 -0.0200 B = 0 2 C = 1 D = 0 num = 0 den = 1.0000 12.0000 20.0200 0.0000 2.0000 0 1.0000 -2.0000

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. Enter the following command in MATLAB.
step(num,den);

You should see the following plot which is equivalent to the Scope's output.

Implementing Lag Compensator Control


In the motor speed control root locus example a Lag Compensator was designed with the following transfer function.

To implement this in Simulink, we will contain the open-loop system from earlier in this page in a Subsystem block. Create a new model window in Simulink. Drag a Subsystem block from the Connections block library into your new model window.

Double click on this block. You will see a blank window representing the contents of the subsystem (which is currently empty). Open your previously saved model of the Motor Speed system, motormod.mdl. Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or Ctrl-C). Select the blank subsystem window from your new model and select Paste from the Edit menu (or Ctrl-V). You should see your original system in this new subsystem window. Close this window. You should now see input and output terminals on the Subsystem block. Name this block "plant model".

Now, we will insert a Lag Compensator into a closed-loop around the plant model. First, we will feed back the plant output. Draw a line extending from the plant output. Insert a Sum block and assign "+-" to it's inputs. Tap a line of the output line and draw it to the negative input of the Sum block.

The output of the Sum block will provide the error signal. We will feed this into a Lag Compensator. Insert a Transfer Function Block after the summer and connect them with a line. Edit this block and change the Numerator field to "[50 50]" and the Denominator field to "[1 0.01]". Label this block "Lag Compensator".

Finally, we will apply a step input and view the output on a scope. Attach a step block to the free input of the feedback Sum block and attach a Scope block to the plant output. Double-click the Step block and set the Step Time to "0".

You can download our version of the closed-loop system here.

10

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "3" in the Stop Time field. The design requirements included a settling time of less than 2 sec, so we simulate for 3 sec to view the output. The physical parameters must now be set. Run the following commands at the MATLAB prompt:
J=0.01; b=0.1; K=0.01; R=1; L=0.5;

Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Simulink Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Speed Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

11

Example: DC Motor Position Modeling in Simulink


Physical setup Building the model Open-loop response Extracting a digital model Implementing digital control Closed-loop response

Physical setup
A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide transitional motion. The electric circuit of the armature and the free body diagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab. * moment of inertia of the rotor (J) = 3.2284E-6 kg.m^2/s^2 * damping ratio of the mechanical system (b) = 3.5077E-6 Nms * electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp * electric resistance (R) = 4 ohm * electric inductance (L) = 2.75E-6 H * input (V): Source Voltage * output (theta): position of shaft * The rotor and shaft are assumed to be rigid The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf, e, is related to the rotational velocity by the following equations:

In SI units (which we will use), Kt (armature constant) is equal to Ke (motor constant).

Building the Model


This system will be modeled by summing the torques acting on the rotor inertia and integrating the acceleration to give the velocity, and integrating velocity to get position. Also, Kirchoff's laws will be applied to the armature circuit. Open Simulink and open a new model window. First, we will model the integrals of the rotational acceleration and of the rate of change of armature current.

Insert an Integrator block (from the Linear block library) and draw lines to and from its input and output terminals. Label the input line "d2/dt2(theta)" and the output line "d/dt(theta)" as shown below. To add such a label, double click in the empty space just above the line. Insert another Integrator block attached to the output of the previous one and draw a line from its output terminal. Label the output line "theta". Insert a third Integrator block above the first one and draw lines to and from its input and output terminals. Label the input line "d/dt(i)" and the output line "i".

Next, we will start to model both Newton's law and Kirchoff's law. These laws applied to the motor system give the following equations:

The angular acceleration is equal to 1/J multiplied by the sum of two terms (one pos., one neg.). Similarly, the derivative of current is equal to 1/L multiplied by the sum of three terms (one pos., two neg.). Insert two Gain blocks, (from the Linear block library) one attached to each of the leftmost integrators. Edit the gain block corresponding to angular acceleration by double-clicking it and changing its value to "1/J". Change the label of this Gain block to "inertia" by clicking on the word "Gain" underneath the block. Similarly, edit the other Gain's value to "1/L" and it's label to Inductance. Insert two Sum blocks (from the Linear block library), one attached by a line to each of the Gain blocks. Edit the signs of the Sum block corresponding to rotation to "+-" since one term is positive and one is negative. Edit the signs of the other Sum block to "-+-" to represent the signs of the terms in Kirchoff's equation.

Now, we will add in the torques which are represented in Newton's equation. First, we will add in the damping torque. Insert a gain block below the inertia block, select it by single-clicking on it, and select Flip from the Format menu (or type Ctrl-F) to flip it left-to-right. Set the gain value to "b" and rename this block to "damping".
3

Tap a line (hold Ctrl while drawing) off the first rotational integrator's output (d/dt(theta)) and connect it to the input of the damping gain block. Draw a line from the damping gain output to the negative input of the rotational Sum block. Next, we will add in the torque from the armature. Insert a gain block attached to the positive input of the rotational Sum block with a line. Edit it's value to "K" to represent the motor constant and Label it "Kt". Continue drawing the line leading from the current integrator and connect it to the Kt gain block.

Now, we will add in the voltage terms which are represented in Kirchoff's equation. First, we will add in the voltage drop across the coil resistance. Insert a gain block above the inductance block, and flip it left-to-right. Set the gain value to "R" and rename this block to "Resistance". Tap a line (hold Ctrl while drawing) off the current integrator's output and connect it to the input of the resistance gain block. Draw a line from the resistance gain output to the upper negative input of the current equation Sum block. Next, we will add in the back emf from the motor. Insert a gain block attached to the other negative input of the current Sum block with a line. Edit it's value to "K" to represent the motor constant and Label it "Ke". Tap a line off the first rotational integrator's output (d/dt(theta)) and connect it to the Ke gain block.

The third voltage term in the Kirchoff equation is the control input, V. We will apply a step input. Insert a Step block (from the Sources block library) and connect it with a line to the positive input of the current Sum block. To view the output speed, insert a Scope (from the Sinks block library) connected to the output of the second rotational integrator (theta). To provide a appropriate unit step input at t=0, double-click the Step block and set the Step Time to "0".

You can download a model file for the complete system here.

Open-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the

Simulation menu and enter "0.2" in the Stop Time field. 0.2 seconds is long enough to view the open-loop response. Also in the Parameters dialog box, it is helpful to change the Solver Options method. Click on the field which currently contains "ode45 (Dormand-Prince)". Select the option "ode15s (stiff/NDF)". Since the time scales in this example are very small, this stiff system integration method is much more efficient than the default integration method.

The physical parameters must now be set. Run the following commands at the MATLAB prompt:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6;

Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Extracting a Digital Model into MATLAB


6

A linear digital model of this continuous-time system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. Conversion to a discrete-time (digital) system is done with Zero-Order Hold blocks on both the inputs and outputs of the system, which act as both D/A (sample-and-hold) and A/D devices. The extraction of a model makes use of In and Out Connection blocks and the MATLAB function dlinmod. We will start with the model which we just build. You can download a complete version here. We will first group all of the system components (except for the Step and Scope which aren't really part of the system) into a Subsystem block. Drag the mouse from one corner of your model window to the other to highlight all of the components. If possible, avoid highlighting the Step and Scope blocks, but if you do, hold the shift key and single click on either of the Step and Scope blocks to un-highlight them. Your model window should appear as shown below.

Select Create Subsystem on the Edit menu (or hit Ctrl-G). This will group all of the selected blocks into a single block. Your window should appear as shown below.

Change the label of the Subsystem block to "Continuous Plant". If you like, you can resize this block so the words "In1" and "Out1" inside of it don't overlap. To resize a block, highlight it by single clicking it and drag the corners to the desired size. Replace the Step Block and Scope Block with Zero Order Hold blocks (from the Discrete block library). One Zero Order Hold block is used to convert a discrete-time signal to a stepwiseconstant continuous signal. The other Zero Order Hold block is used to take discrete samples of the output from the plant. Edit the Zero Order Hold blocks and set the Sample Time fields to 0.001 (this is fast compared to the desired step response in the MATLAB tutorial.) Connect an In Connection Block to the input of the first Zero Order Hold block, and an Out Connection Block to the output of the second Zero Order Hold block. (these blocks can be found in the Connections block library). This defines the input and output of the system for the extraction process. Drag each block in your model so that they are arranged in a line.

Save your file as "motorpos.mdl" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands:
[A,B,C,D]=dlinmod('motorposmodel',.001) [num,den]=ss2tf(A,B,C,D)

The extra parameter in dlinmod provides the sample time for the discrete conversion. You should see the following output, providing discrete time models of the system both in state-space and transfer function form.
A = 1.0000 0 0 B = 0.0010 0.2359 2.0589 C = 1 0 0 0.0000 0.0000 0.0055 0.0010 -0.0065 0.9425

D = 0 num = 0 den = 1.0000 -1.9425 0.9425 0 0.0010 0.0010 0.0000

As noticed in above results, both numerator and denominator of the discrete transfer function have one extra root at z = 0. These cancel each other, and the discrete-time transfer function to the motor position output from the voltage input is:

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. Enter the following commands in MATLAB.
[x1] = dstep(num,den,201); t=0:0.001:0.2; stairs(t,x1)

You should see the following plot which is equivalent to the Scope's output.

Implementing Digital Control


In the motor speed control digital example a digital controller was designed with the following transfer

function.

Bring up the model window containing the digital system which was just extracted into MATLAB. (You can download our version here) Delete the "In" and "Out" blocks.

We will first feed back the plant output. Insert a Sum block and assign "+-" to it's inputs. Tap a line of the output line of the output Zero Order Hold line and draw it to the negative input of the Sum block.

The output of the Sum block will provide the error signal. We will feed this into the digital controller. Insert a Discrete Transfer Function Block (from the Discrete block library) after the summer and connect them with a line. Edit this block and change the Numerator field to "450*conv([1 -.85],[1 -.85])", the Denominator field to "conv([1 .98],[1 -.7])", and the Sample Time to ".001". Label this block "Controller" and resize it to view the entire contents.

10

Finally, we will apply a step input and view the output on a scope. Attach a Step block to the free input of the feedback Sum block and attach a Scope block to the plant output. Double-click the Step block and set the Step Time to "0".

You can download our version of the closed-loop system here.

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "0.05" in the Stop Time field. The design requirements included a settling time of less than 0.04 sec, so we simulate for 0.05 sec to view the output. The physical parameters must now be set. Run the following commands at the MATLAB prompt:
J=3.2284E-6; b=3.5077E-6; K=0.0274; R=4; L=2.75E-6;

Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

11

Simulink Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Position Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

12

Example: Bus Suspension Modeling in Simulink


Physical setup Building the model Open-loop response Extracting the Model Implementing Full State Feedback Closed-loop response

Physical setup
Designing an automatic suspension system for a bus turns out to be an interesting control problem. When the suspension system is designed, a 1/4 bus model (one of the four wheels) is used to simplify the problem to a one dimensional spring-damper system. A diagram of this system is shown below:

Where: * body mass (m1) = 2500 kg, * suspension mass (m2) = 320 kg, * spring constant of suspension system(k1) = 80,000 N/m, * spring constant of wheel and tire(k2) = 500,000 N/m, * damping constant of suspension system(b1) = 350 Ns/m. * damping constant of wheel and tire(b2) = 15,020 Ns/m. * control force (u) = force from the controller we are going to design.

Design requirements:
A good bus suspension system should have satisfactory road holding ability, while still providing comfort when riding over bumps and holes in the road. When the bus is experiencing any road disturbance (i.e. pot holes, cracks, and uneven pavement),the bus body should not have large oscillations, and the oscillations should dissipate quickly. Since the distance X1-W is very difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the distance X1-X2 instead of X1-W as the output in our problem. Keep in mind that this is an estimation.

The road disturbance (W) in this problem will be simulated by a step input. This step could represent the bus coming out of a pothole. We want to design a feedback controller so that the output (X1-X2) has an overshoot less than 5% and a settling time shorter than 5 seconds. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and return to a smooth ride within 5 seconds.

Building the Model


This system will be modeled by summing the forces acting on both masses (body and suspension) and integrating the accelerations of each mass twice to give velocities and positions. Newton's law will be applied to each mass. Open Simulink and open a new model window. First, we will model the integrals of the accelerations of the masses.

Insert an Integrator block (from the Linear block library) and draw lines to and from its input and output terminals. Label the input line "a1" (for acceleration) and the output line "v1" (for velocity) To add such a label, double click in the empty space just above the line. Insert another Integrator block connected to the output of the first. Draw a line from its output and label it "x1" (for position). Insert a second pair of Integrators below the first with lines labeled "a2", "v2", and "x2".

Next, we will start to model Newton's law. Newton's law for each of these masses can be expressed as:

These equations can be represented with gain blocks (for 1/M1 and 1/M2) and two summation blocks. Insert two Gain blocks, (from the Linear block library) one attached to the inputs of each of the integrator pairs. Edit the gain block corresponding to M1 by double-clicking it and changing its value to "1/m1". Change the label of this Gain block to "Mass 1" by clicking on the word "Gain" underneath the block. Similarly, edit the other Gain's value to "1/m2" and it's label to "Mass 2". (You may want to resize the gain blocks to view the contents. To do this, single click on the block to highlight it, and drag one of the corners to the desired size.) There are three forces acting on M1 (one spring, one damper, and the input, u) and five forces acting on M2 (two springs, two dampers, and the input, u). Insert two Sum blocks (from the Linear block library), one attached by a line to each of the Gain blocks. Edit the signs of the Sum block corresponding to M1 to "+--" to represent the three forces (two of which will be negative) Edit the signs of the other Sum block to "++-++" to represent the five forces, one of which will be negative.

Now, we will add in the forces acting on each mass. First, we will add in the force from Spring 1. This force is equal to a constant, k1 times the difference X1-X2. Insert a sum block after the upper pair of integrators. Edit its signs to "+-" and connect the "x1" signal to the positive input and the "x2" signal to the negative input. Draw a line leading from the output of the Sum block.
3

Insert a Gain block above the "Mass1" block. Flip it left-to-right by single-clicking on it and selecting Flip Block from the Format menu (or hit Ctrl-F). Edit the value of this gain to "k1" and label the block "Spring 1". Tap a line off the output of the last Sum block and connect it to the input of this gain block. Connect the output of this gain block (the spring force) to the second input of the Mass 1 Sum block. This input should be negative since the Spring 1 pulls down on Mass 1 when X1 > X2. Tap a line off the spring force line and connect it to the second input of the Mass 2 Sum block. This input is positive since Spring 1 pulls up on Mass 2.

Now, we will add in the force from Damper 1. This force is equal to b1 times V1-V2. Insert a sum block below the Mass 1's first integrator. Flip it left-to-right, and edit it's signs to "+-". Tap a line off the "v1" line and connect it to the positive input of this Sum block. Tap a line off the "v2" line and connect it to the negative input of this Sum block. Insert a Gain block to the left of this Sum block and flip it left-to-right. Edit it's value to "b1" and label it "Damper 1". Connect the output of the new Sum block to the input of this gain block. Connect the output of this gain block (the damper force) to the third input of the Mass 1 Sum block. This input is negative, similar to Spring 1's force on Mass 1. Tap a line off Damper 1's force line and connect it to the first input (which is positive) of Mass 2's Sum block.

Now we will add in the force from Spring 2. This force acts only on Mass 2, but depends on the ground profile, W. Spring 2's force is equal to X2-W. Insert a Step block in the lower left area of your model window. Label it "W". Edit it's Step Time to "0" and it's Final Value to "0". (We will assume a flat road surface for now). Insert a Sum block to the right of the W Step block and edit its signs to "-+". Connect the output of the Step block to the positive input of this Sum block. Tap a line off the "x2" signal and connect it to the negative input of the new Sum block. Insert a Gain block to the right of this Sum block and connect the Sum's output to the new Gain's input. Change the value of the gain to "k2" and label it "Spring 2". Connect the output of this block (Spring 2's force) to the fourth input of Mass 2's Sum block. This force adds in in the positive sense.

Next, we will add in the force from Damper 2. This force is equal to b2 times V2-d/dt(W). Since there is no existing signal representing the derivative of W we will need to generate this signal. Insert a Derivative block (from the Linear block library) to the right of the W step block. Tap a line of the Step's output and connect it to the input of the Derivative block. Insert a Sum block after the Derivative block and edit it's signs to "+-". Connect the Derivative's output to the positive input of the new Sum block. Tap a line off the "v2" line and connect it to the negative input of this Sum block. Connect the output of this Sum block (Damper 2's force) to the fifth input of Mass 2's Sum block. This force also adds in with positive sign.

The last force in the input U acting between the two masses.

Insert a Step block in the upper left of the model window. Connect it's output to the remaining input of Mass 1's Sum block (with positive sign). Tap a line off this signal and connect it to the remaining input of Mass 2's Sum block (with negative sign). Edit this Step block's Step Time to "0" and leave its Final Value "1". Label this Step block "U". Finally, to view the output (X1-X2) insert a Scope connected to the output of the rightmost Sum block.

You can download a model file for the complete system here.

Open-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "50" in the Stop Time field. 50 seconds is long enough to view the open-loop response. The physical parameters must now be set. Run the following commands at the MATLAB prompt:
m1=2500; m2=320; k1=80000; k2=500000; b1 = 350; b2 = 15020;

Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Extracting a Linear Model into M ATLAB


A linear model of the system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. We will extract only the model from the input U to the output X1-X2. First, replace the U Step block with an In Connection Block. Also, replace the Scope block with an Out Connection Block. (These blocks can be found in the Connections block library). This defines the input and output of the system for the extraction process.

Save your file as "suspmod.mdl" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands:
[A,B,C,D]=linmod('suspmodel') [num,den]=ss2tf(A,B,C,D)

You should see the following output, providing both state-space and transfer function models of the system.

A = 1.0e+003 * 0 0 0.2500 -0.0320 B = 0 0 -0.0031 0.0004 C = 1 D = 0 num = 0 den = 1.0e+004 * 0.0001 0.0048 0.1851 0.1721 5.0000 0.0000 0.0035 0.0188 0.6250 -1 0 0 0 0 -1.8125 0.0320 0 0.0010 -0.0480 0.0001 0.0010 0 0.0011 -0.0001

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. Enter the following command in MATLAB.
step(num,den);

You should see the following plot which is equivalent to the Scope's output.

Implementing Full State Feedback


In the Bus Suspension Control State Space example a full-state feedback controller was designed feeding back the following five states: The controller used the following feedback gain matrix: To implement this in Simulink, we will contain the open-loop system from earlier in this page in a Subsystem block. Create a new model window. Drag a Subsystem block from the Connections block library into your new model window.

Double click on this block. You will see a blank window representing the contents of the subsystem (which is currently empty). Open your previously saved model of the Bus Suspension system, suspmod.mdl. Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or Ctrl-C). Select the blank subsystem window from your new model and select Paste from the Edit menu (or
10

Ctrl-V). You should see your original system in this new subsystem window (you may need to use the scroll bars to center on it). Label the In Connection block "U", and the Out Connection block "y1". Replace the W Step block with an In Connection block and label this block "W".

Now we will generate the other state outputs from the subsystem. Insert an Out block below the "y1" block and label it "d/dt(y1)", Tap a line off the line leading into the Damper 1 gain block (V1-V2) and connect it to the d/dt(y1) Out block. Insert another Out block below the "d/dt(y1)" Out block and label it "x1". Tap a line off the "x1" line and connect it to this Out block. Insert another Out block below the "x1" Out block and label it "d/dt(x1)". Tap a line off the "v1" line and connect it to this Out block.

11

The final, extra, state needs to be generated, which is the integral of Y1. Insert an Integrator block above the "y1" Out block and connect its input with a line tapped of the input to the "y1" Out block. Insert an Out block, label it "int(y1)", and connect it to the output of the new integrator.

Since the state outputs will be used to form a vector, it is important that they be numbered in the right order. Edit the "x1" Out block and change its Port Number to "1". Similarly, change the "d/dt(x1)" Out block's port number to "2", "y1" Out's port number to "3", "d/dt(y1)" Out's port number to "4", and "int(y1)" Out's port number to "5". The In blocks should be numbered such that "U" is "1" and "W" is "2". Some of these numbers may already be correct.

12

Close the Subsystem window. You should now see input and output terminals on the Subsystem block. Name this block "Suspension Model". You should resize this block so that you can read all of the labels. To do this, highlight it by singleclicking on it and drag one of the highlighted corners to the right size. Notice that the model has two inputs and five outputs. Each input and output is a scalar signal in this model.

Now, we will build a full-state feedback controller around the plant model. First, we need to create a vector signal out of the five scalar outputs in order to multiply by the feedback gain matrix K. Insert a Mux block (from the Connections block library) to the right of the Suspension Model block. The Mux takes multiple inputs and combines them into a vector signal. By default, the Mux has three inputs. Edit the Mux block and change the Number of Inputs to "5". Resize the Mux so that it is the same height as the Suspension Model block. Connect each of the Suspension Model's outputs to the Mux's inputs in order.

Now, we will close the loop. Insert a Matrix Gain block (from the Linear block library) below the Suspension Model block. Flip the Matrix Gain left-to-right and edit its value to "K". Insert a Sum block to the left of the Suspension Model block. Edit its signs to "+-". Connect the output of the Matrix Gain to the negative input of the Sum block. Connect the output of the Sum block to the "U" input of the Suspension Model.
13

Insert a Step block and connect it to the positive input of the Sum block. Label the step block "r" and edit its Step Time to "0" and its Final Value to "0" (we are commanding the bust to stay level). Insert a Step block and connect it to the "W" input of the Suspension Model. Edit its Step Time to "0" and its Final Value to "-.1" (we are now assuming a 10cm deep pothole). Insert a Scope block and tap a line off the "y1" output of the Suspension Model and connect it to the Scope.

You can download our version of the closed-loop system here.

Closed-loop response
To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "2" in the Stop Time field. The design requirements included a settling time of less than 5 sec, and the system actually settles in 2 sec. The physical parameters must now be set. Run the following commands at the MATLAB prompt:
m1=2500; m2=320; k1=80000; k2=500000; b1 = 350; b2 = 15020;

14

The last step is to assign values to the feedback gain matrix K. Execute the following command at the MATLAB prompt.
K= [ 0 2.3e6 5e8 0 8e6 ];

Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Simulink Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Bus Suspension Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

15

V d: M hnh Con lc ngc trong Simulink


Thit lp bi ton v yu cu thit k Phn tch lc v thit lp phng trnh ca h thng Xy dng m hnh p ng ca h h Xut m hnh c tuyn tnh ho Thc thi iu khin PID p ng ca h h

Thit lp bi ton v yu cu thit k


Xe v con lc ngc nh sau c y i vi 1 xung lc F. Xc nh cc phng trnh ng lc hc chuyn ng ca h thng, v tuyn tnh ho gc quay ca con lc, theta = 0 (ni cch khc, gi s con lc khng dch chuyn qu vi so vi phng thng ng tng ng vi gc l 0). Ta cn tm 1 b iu khin tho mn cc yu cu sau.

V d ta gi s rng M khi lng xe 0.5 kg m khi lng con lc 0.2 kg b ma st ca xe 0.1 N/m/sec l chiu di ti trng tm con lc 0.3 m I momen qun tnh con lc 0.006 kg*m^2 F ngoi lc tc ng vo xe x ta v tr xe theta gc quay ca con lc Trong v d ny, chng ta s thc thi 1 b iu khin PID ch p dng cho h thng mt vo-mt ra (SISO), nn chng ta cng s ch quan tm ti vic iu khin gc quay ca con lc. Do vy, ta khng

quan tm ti iu kin rng buc no v v tr ca xe. Chng ta s gi s rng h thng ban u v tr cn bng, v chu tci dng ca 1 xung lc 1N. Con lc s quay n v tr tn cng trn bn phi trong vng 5 giy v sau s khng bao gi dch chuyn qu0.05 rad so vi phng thng ng. Cc yu cu thit k t ra l: Thi gian n nh khng qu 5 giy. Con lc khng bao gi c quay qu 0.05 rad so vi phng thng ng.

Phn tch lc v thit lp phng trnh h thng


Di y l hai biu Gii phng lin kt ca h thng.

H thng trn i hi m hnh trong Simulink phi kh uyn chuyn bi s rng buc vt l (khp ni) gia xe v con lc lm cho h thng c 2 bcn t do. C xe v con lc u c 1 bc t do tng ng l X v theta. Chng ta s m hnh ho cc phng trnh Newton cho c 2 bc t do ny.

L cn thit khi ta a cc lc tng tc N v P gia xe v con lc vo m hnh ho h thng. Tng hp cc lc ny i hi cn c phng trnh ng lc hc theo phng x v y ca con lc theo theta. Trong phn MATLAB v v d m hnh ho con lc cc lc tng tc c gii bng phng php i s. Tm li ay, chng ta mun s dng sc mnh m hnh ho ca Simulink v cho m phng lm cc cng vic tnh ton i s. Do vy, chng ta s m hnh ho hai phng trnh theo phng x v y ca con lc.

Tuy nhin, xp v yp l cc hm ca theta. Do vy, chng ta c th biu din vi phn ca chng thng qua vi phn ca theta.

Biu thc trn c th c thay th cho biu thc ca N v P. Thay v tip tc cc php bin i i s y, chng ta s biu din cc phng trnh trn trong Simulink. Simulink c th tnh ton trc tip trn cc phng trnh phi tuyn, do vy chng ta khng cn tuyn tnh ha cc phng trnh nh thc hin trong phn MATLAB tutorials.

Xy dng m hnh trong Simulink


Trc ht, chng ta s m hnh cc trng thi ca h thng thng qua theta v x. We will represent Newton's equations for the pendulum rotational inertia and the cart mass. M 1 ca s m hnh mi trong Simulink, sau chnh li kch thc cho (v y l 1 m hnh rng). Thm 2 khi tch phn vo (t th vin Linear) gn pha di ca m hnh v lin kt chng vi nhau thnh chui. V 1 ng t khi tch phn th 2 v nh nhn l "theta". ( chn nhn vo, kch p ln v tr m bn mun t.) nh nhn ng ni gia 2 khi tch phn l "d/dt(theta)". V 1 ng ni ti khi tch phn th hai v nh nhn l "d2/dt2(theta)". Thm 1 khi Gain (t th vin Linear) vp pha tri ca khi tch phn th nht v lin kt u ra ca n ti ng d2/dt2(theta).
3

Thay gi tr h s ca khi thnh "1/I". Thay nhn ca khi thnh "Pendulum Inertia" bng cch kch p vo "Gain". (bn c th chn thm 1 ng mi vo nhn bng cch n return). Thm 1 khi Sum (t th vin Linear) vo pha tri ca khi Pendulum Inertia v ni u ra ca n vi u vo ca Pendulum Inertia. Thay nhn ca khi ny thnh Sum Torques on Pend. Xy dng 1 lot cc khi khc ging nh th pha trn m hnh cc nhn "x" thay cho "theta". Khi gain c gi tr l "1/M" vi nhn l "Cart Mass", v khi Sum c nhn l "Sum Forces on Cart". Thay i khi Sum Forces thnh "-+-" biu th cho cc lc tc ng ngang ln xe.

By gi ta s thm hai lc tc dng vo xe. Thm 1 khi Gain vo pha trn khi Cart Mass. Thay gi tr ca n thnh "b" v nh nhn l "damping". Xoay khi ny sang phi bng cch kch chut chon n ri chn Flip Block t menu Format (hoc n Ctrl-F). Trch tn hiu t ng d/dt(x) (gi Ctrl trong khi v) and connect it to the input of the damping block. Connect the output of the damping block to the topmost input of the Sum Forces block. The damping force then has a negative sign. Insert an In block (from the Connections block library) to the left of the Sum Forces block and change its label to "F".

Connect the output of the F in block to the middle (positive) input of the Sum Forces block.

Now, we will apply the forces N and P to both the cart and the pendulum. These forces contribute torques to the pendulum with components "N L cos(theta) and P L sin(theta)". Therefore, we need to construct these components. Insert two Elementary Math blocks (from the Nonlinear block library) and place them one above the other above the second theta integrator. These blocks can be used to generate simple functions such as sin and cos. Edit upper Math block's value to "cos" and leave the lower Math block's value "sin". Label the upper (cos) block "Vertical" and the lower (sin) block "Horizontal" to identify the components. Flip each of these blocks left-to-right. Tap a line off the theta line and connect it to the input of the cos block. Tap a line of the line you just drew and connect it to the input of the sin block. Insert a Gain block to the left of the cos block and change its value to "l" (lowercase L) and its label to "Pend. Len." Flip this block left-to-right and connect it to the output of the cos block. Copy this block to a position to the left of the sin block. To do this, select it (by single-clicking) and select Copy from the Edit Menu and then Paste from the Edit menu (or hit Ctrl-C and Ctrl-V). Then, drag it to the proper position. Connect the new Pend. Len.1 block to the output of the sin block. Draw long horizontal lines leading from both these Pend. Len. blocks and label the upper one "l

cos(theta)" and the lower one "l sin(theta)".

Now that the pendulum components are available, we can apply the forces N and P. We will assume we can generate these forces, and just draw them coming from nowhere for now. Insert two Product blocks (from the Nonlinear block library) next to each other to the left and above the Sum Torques block. These will be used to multiply the forces N and P by their appropriate components. Rotate the left Product block 90 degrees. To do this, select it and select Rotate Block from the Format menu (or hit Ctrl-R). Flip the other product block left-to-right and also rotate it 90 degrees. Connect the left Product block's output to the lower input of the Sum Torques block. Connect the right Product block's output to the upper input of the Sum Torques block. Continue the l cos(theta) line and attach it to the right input of the left Product block. Continue the l sin(theta) line and attach it to the right input of the right Product block. Begin drawing a line from the open input of the right product block. Extend it up and the to the right. Label the open end of this line "P". Begin drawing a line from the open input of the left product block. Extend it up and the to the right. Label the open end of this line "N". Tap a line off the N line and connect it to the open input of the Sum forces block.

Next, we will represent the force N and P in terms of the pendulum's horizontal and vertical accelerations from Newton's laws. Insert a Gain block to the right of the N open ended line and change its value to "m" and its label to "Pend. Mass". Flip this block left-to-right and connect it's to N line. Copy this block to a position to the right of the open ended P line and attach it to the P line. Draw a line leading to the upper Pend. Mass block and label it "d2/dt2(xp)". Insert a Sum block to the right of the lower Pend. Mass block. Flip this block left-to-right and connect its output to the input of the lower Pend. Mass block. Insert a Constant block (from the Sources block library) to the right of the new Sum block, change its value to "g" and label it "Gravity". Connect the Gravity block to the upper (positive) input of the newest Sum block. Draw a line leading to the open input of the new Sum block and label it "d2/dt2(yp)".

Now, we will begin to produce the signals which contribute to d2/dt2(xp) and d2/dt2(yp). Insert a Sum block to the right of the d2/dt2(yp) open end. Change the Sum block's signs to "--" to represent the two terms contributing to d2/dt2(yp). Flip the Sum block left-to-right and connect it's output to the d2/dt2(yp) signal. Insert a Sum block to the right of the d2/dt2(xp) open end. Change the Sum block's signs to "++-" to represent the three terms contributing to d2/dt2(xp). Flip the Sum block left-to-right and connect it's output to the d2/dt2(xp) signal. The first term of d2/dx2(xp) is d2/dx2(x). Tap a line off the d2/dx2(x) signal and connect it to the topmost (positive) input of the newest Sum block.

Now, we will generate the terms d2/dt2(theta)*lsin(theta) and d2/dt2(theta)*lcos(theta). Insert two Product blocks next to each other to the right and below the Sum block associated with d2/dt2(yp). Rotate the left Product block 90 degrees. Flip the other product block left-to-right and also rotate it 90 degrees. Tap a line off the l sin(theta) signal and connect it to the left input of the left Product block. Tap a line off the l cos(theta) signal and connect it to the right input of the right Product block. Tap a line off the d2/dt2(theta) signal and connect it to the right input of the left Product block. Tap a line of this new line and connect it to the left input of the right Product block.

Now, we will generate the terms (d/dt(theta))^2*lsin(theta) and (d/dt(theta))^2*lcos(theta). Insert two Product blocks next to each other to the right and slightly above the previous pair of Product blocks. Rotate the left Product block 90 degrees. Flip the other product block left-to-right and also rotate it 90 degrees. Tap a line off the l cos(theta) signal and connect it to the left input of the left Product block. Tap a line off the l sin(theta) signal and connect it to the right input of the right Product block. Insert a third Product block and insert it slightly above the d/dt(theta) line. Label this block "d/dt(theta)^2". Tap a line off the d/dt(theta) signal and connect it to the left input of the lower Product block. Tap a line of this new line and connect it to the right input of the lower Product block. Connect the output of the lower Product block to the free input of the right upper Product block. Tap a line of this new line and connect it to the free input of the left upper Product block.

10

Finally, we will connect these signals to produce the pendulum acceleration signals. In addition, we will create the system outputs x and theta. Connect the d2/dt2(theta)*lsin(theta) Product block's output to the lower (negative) input of the d2/dt2(yp) Sum block. Connect the d2/dt2(theta)*lcos(theta) Product block's output to the lower (negative) input of the d2/dt2(xp) Sum block. Connect the d/dt(theta)^2*lcos(theta) Product block's output to the upper (negative) input of the d2/dt2(yp) Sum block. Connect the d/dt(theta)^2*lsin(theta) Product block's output to the middle (positive) input of the d2/dt2(xp) Sum block. Insert an Out block (from the Connections block library) attached to the theta signal. Label this block "Theta". Insert an Out block attached to the x signal. Label this block "x". It should automatically be numbered 2.

11

Now, save this model as pend.mdl.

Open-loop response
To generate the open-loop response, it is necessary to contain this model in a subsystem block. Create a new model window (select New from the File menu in Simulink or hit Ctrl-N). Insert a Subsystem block from the Connections block library. Open the Subsystem block by double clicking on it. You will see a new model window labeled "Subsystem". Open your previous model window named pend.mdl. Select all of the model components by selecting Select All from the Edit menu (or hit Ctrl-A). Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit Ctrl-C). Paste the model into the Subsystem window by selecting Paste from the Edit menu (or hit Ctrl-V) in the Subsystem window Close the Subsystem window. You will see the Subsystem block in the untitled window with one input terminal labeled F and two output terminals labeled Theta and x. Resize the Subsystem block to make the labels visible by selecting it and dragging one of the corners. Label the Subsystem block "Inverted Pendulum".

12

Now, we will apply a unit impulse force input, and view the pendulum angle and cart position. An impulse can not be exactly simulated, since it is an infinite signal for an infinitesimal time with time integral equal to 1. Instead, we will use a pulse generator to generate a large but finite pulse for a small but finite time. The magnitude of the pulse times the length of the pulse will equal 1. Insert a Pulse Generator block from the Sources block library and connect it to the F input of the Inverted Pendulum block. Insert a Scope block (from the Sinks block library) and connect it to the Theta output of the Inverted Pendulum block. Insert a Scope block and connect it to the x output of the Inverted Pendulum block. Edit the Pulse Generator block by double clicking on it. You will see the following dialog box.

Change the Period value to "10" (a long time between a chain of impulses - we will be interested in only the first pulse). Change the Duty Cycle value to ".01" this corresponds to .01% of 10 seconds, or .001 seconds. Change the Amplitude to 1000. 1000 times .001 equals 1, providing an approximate unit impulse. Close this dialog box. You system will appear as shown below.

13

We now need to set an appropriate simulation time to view the response. Select Parameters from the Simulation menu. Change the Stop Time value to 2 seconds. Close this dialog box You can download a version of the system here. Before running it, it is necessary to set the physical constants. Enter the following commands at the MATLAB prompt.
M m b i g l = = = = = = .5; 0.2; 0.1; 0.006; 9.8; 0.3;

Now, start the simulation (select Start from the Simulation menu or hit Ctrl-t). If you look at the MATLAB prompt, you will see some error messages concerning algebraic loops. Due to the algebraic constraint in this system, there are closed loops in the model with no dynamics which must be resolved completely at each time step before dynamics are considered. In general, this is not a problem, but often algebraic loops slow down the simulation, and can cause real problems if discontinuities exist within the loop (such as saturation, sign functions, etc.) Open both Scopes and hit the autoscale buttons. You will see the following for theta (left) and x (right).

14

Notice that the pendulum swings all the way around due to the impact, and the cart travels along with a jerky motion due to the pendulum. These simulations differ greatly from the MATLAB open loop simulations because Simulink allows for fully nonlinear systems.

Extracting the linearized model into MATLAB


Since MATLAB can't deal with nonlinear systems directly, we cannot extract the exact model from Simulink into MATLAB. However, a linearized model can be extracted. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. In the case of this example, will use the equivalent command linmod2, which can better handle the numerical difficulties of this problem. To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as pend.mdl. In this model, one input, F (the force on the cart) and two outputs, theta (pendulum angle from vertical) and x (position of the cart), were defined. When linearizing a model, it is necessary to choose an operating point about which to linearize. By default, the linmod2 command linearizes about a state vector of zero and zero input. Since this is the point about which we would like to linearize, we do not need to specify any extra arguments in the command. Since the system has two outputs, we will generate two transfer functions. At the MATLAB prompt, enter the following commands
[A,B,C,D]=linmod2('pend') [nums,den]=ss2tf(A,B,C,D) numtheta=nums(1,:) numx=nums(2,:)

You will see the following output (along with algebraic loop error messages) providing a state-space model, two transfer function numerators, and one transfer function denominator (both transfer functions share the same denominator).
A = 0 0 31.1818 2.6727 0 0 0.0000 0.0000 1.0000 0 0.0000 0.0000 0 1.0000 -0.4545 -0.1818

15

B = 0 0 4.5455 1.8182 C = 1 0 D = 0 0 nums = 0 0 den = 1.0000 numtheta = 0 numx = 0 0.0000 1.8182 0.0000 -44.5455 0.0000 4.5455 0.0000 0.0000 0.1818 -31.1818 -4.4545 0.0000 0.0000 0.0000 4.5455 1.8182 0.0000 0.0000 0.0000 -44.5455 0 1 0 0 0 0

To verify the model, we will generate an open-loop response. At the MATLAB command line, enter the following commands.
t=0:0.01:5; impulse(numtheta,den,t); axis([0 1 0 60]);

You should get the following response for the angle of the pendulum.

16

Note that this is identical to the impulse response obtained in the MATLAB tutorial pendulum modeling example. Since it is a linearized model, however, it is not the same as the fully-nonlinear impulse response obtained in Simulink.

Implementing PID control


In the pendulum PID control example, a PID controller was designed with proportional, integral, and derivative gains equal to 100, 1, and 20, respectively. To implement this, we will start with our open-loop model of the inverted pendulum. And add in both a control input and the disturbance impulse input to the plant. Open your Simulink model window you used to obtain the nonlinear open-loop response. (pendol.mdl) Delete the line connecting the Pulse Generator block to the Inverted Pendulum block. (single-click on the line and select Cut from the Edit menu or hit Ctrl-X). Move the Pulse Generator block to the upper left of the window. Insert a Sum block to the left of the Inverted Pendulum block. Connect the output of the Sum block to the Inverted Pendulum block. Connect the Pulse generator to the upper (positive) input of the Sum block.

17

Now, we will feed back the angle output. Insert a Sum block to the right and below the Pulse Generator block. Change the signs of the Sum block to "+-". Insert a Constant block (from the Sources block library) below the Pulse generator. Change its value to "0". This is the reference input. Connect the constant block to the upper (positive) input of the second Sum block. Tap a line off the Theta output of the Inverted Pendulum block and draw it down and to the left. Extend this line and connect it to the lower (negative) input of the second Sum block.

Now, we will insert a PID controller. Double-click on the Blocksets & Toolboxes icon in the main Simulink window. This will open a new window with two icons. In this new window, double-click on the SIMULINK extras icon. This will open a window with icons similar to the main Simulink window. Double-click on the Additional Linear block library icon. This will bring up a library of Linear blocks to augment the standard Linear block library. Drag a PID Controller block into your model between the two Sum blocks. Connect the output of the second Sum block to the input of the PID block. Connect the PID output to the first Sum block's free input.

18

Edit the PID block by doubleclicking on it. Change the Proportional gain to 100, leave the Integral gain 1, and change the Derivative gain to 20. Close this window.

You can download our version of the closed-loop system here

Closed-loop response
We can now simulate the closed-loop system. Be sure the physical parameters are set (if you just ran the open-loop response, they should still be set.) Start the simulation, double-click on the Theta scope and hit the autoscale button. You should see the following response:

19

This is identical to the closed-loop response obtained in the MATLAB tutorials. Note that the PID controller handles the nonlinear system very well because the angle is very small (.04 radians).

Simulink Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Inverted Pendulum Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

20

Example: Modeling a Pitch Controller in Simulink


Physical setup and system equations Building the State-Space Model Open-loop response Extracting the model into MATLAB Building a full-state feedback controller Closed-loop response

Physical setup and system equations


The equations governing the motion of an aircraft are a very complicated set of six non-linear coupled differential equations. However, under certain assumptions, they can be decoupled and linearized into the longitudinal and lateral equations. Pitch control is a longitudinal problem, and in this example, we will design an autopilot that controls the pitch of an aircraft. In this example, we will begin with the linearized state-space equation model which was obtained in the MATLAB Tutorials: pitch control example This model, with numerical values substituted in, is as follows:

in state-space form, the equations are as follows:

Which can be written as:

Building the state-space model


We will model these state equations explicitly in Simulink taking advantage of vector lines to carry the
1

state vector signal X. Vector lines are produced automatically in Simulink when blocks input or output vector signals. First, we will represent the derivative of the state. Open a new model window in Simulink. Insert an Integrator block from the Linear block library. Draw a line from the output of the Integrator and label it "X". To label a line, double click near the line where you want the label to appear. Draw a line leading to the input of the Integrator and label it "d/dt(X)"

Now, we will represent the first state equation, which is d/dt(X)=Ax+Bu. Insert a Matrix Gain block from the Linear block library below the integrator. Flip this block left-to-right. To do this, select it with the mouse (single-click) and select Flip Block from the Format menu (or hit Ctrl-F). Edit the Matrix Gain block (by double clicking). Change the Gain Matrix field to "A" (which we will define later in MATLAB) and close the dialog box. Change the label of this block from Matrix Gain to "A" by single clicking on the existing label. Tap a line off the state signal, X (hold Ctrl while drawing the line to tap) and connect it to the input of the A block. This creates the signal Ax. Insert a Sum block (from the Linear block library) to the left of the integrator and connect it's output to the line leading to the Integrator's input. Draw a line from the output of the A matrix block to the lower (positive) input of the Sum block. Insert another Matrix Gain block to the left of the Sum block. Change it's Matrix Gain value to "B" and change it's label to "B". Connect the B matrix output to the other (positive) input of the Sum block. Draw a line leading to the input of the Sum block. This is the input signal, u.

Now we will form the output signal which is equal to Cx+Du. Insert a Matrix Gain block to the right of the integrator and attach the integrator's output to the input of the Matrix Gain. Change the Matrix Gain value to "C" and change it's label to "C". Insert another Matrix Gain block below the "C" block, and change both it's value and label to "D". Tap a line off the u signal (the input line of the B matrix block) and attach it to the input of the "D" matrix block. Insert a Sum block to the right of the C matrix block. Connect the outputs of the C and D matrix blocks to the inputs of the Sum block.

Next, we will apply inputs and extract outputs from this system. We will use In and Out blocks for this purpose. These blocks allow the system to be extracted into MATLAB and they allow the system to be placed into a Subsystem block for use within another model. Insert an In block (from the Connections block library) and connect it to the open line which connects to the input of the B matrix block. Change the label of the In block to "deltac" (corresponding to the angle of the elevator). Insert an Out block (from the Connections block library) and connect it to the output of the rightmost Sum block. Change the label of this Out block to "theta" (corresponding to the pitch angle of the plane). Insert an Out block above the Theta block, and change it's label to "X" corresponding to the state vector of the system. (This will be used later to implement full-state feedback. Tap a line off the X signal (leading from the second integrator) and connect it to the X Out block.
3

Save this model as "pitch.mdl" (or download ours here.)

Open-loop response
To generate the open-loop response, it is first necessary to contain this model in a subsystem block. Create a new model window (select New from the File menu in Simulink or hit Ctrl-N). Insert a Subsystem block from the Connections block library. Open the Subsystem block by double clicking on it. You will see a new model window labeled "Subsystem". Open your previous model window named pitch.mdl. Select all of the model components by selecting Select All from the Edit menu (or hit Ctrl-A). Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit Ctrl-C). Paste the model into the Subsystem window by selecting Paste from the Edit menu (or hit Ctrl-V) in the Subsystem window Close the Subsystem window. You will see the Subsystem block in the untitled window with one input terminal labeled deltac and two output terminals labeled theta and x. Resize the Subsystem block to make the labels visible by selecting it and dragging one of the corners. Label the Subsystem block "Plane Pitch Model".

Insert a Step block (from the Sources block library) and connect it to the input of the Plane Pitch Model. Edit the Step block (by double clicking on it to bring up the dialog box) and change the Step Time value to 0. Close the Step block dialog box. Insert a Scope block (from the Sinks block library) and connect it to the theta output of the Plane Pitch Model.
4

Before obtaining a step response, we must set the constants in the matrices A, B, C, and D. Enter the following commands at the MATLAB prompt.
A=[-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0]; B=[0.232; 0.0203; 0]; C=[0 0 1]; D=[0];

We are now ready to run the simulation. If you like, you can download our version of the open-loop system here. Start the simulation by selecting Start from the Simulation menu (or hit Ctrl-t). When the simulation is finished, open the Scope by double clicking on it and hit the Scope's autoscale button. You will see the following response.

Extracting the model into M ATLAB


The Simulink model can be extracted into an equivalent state-space model in MATLAB. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as pitch.mdl. In this model, one input, deltac (the elevator angle) and two outputs, theta (pitch angle) and X (the state vector), were defined. We must truncate the output and feedthrough matrices (Co and Do) to provide only the first input. At the MATLAB prompt, enter the following commands
[Ao,Bo,Co,Do]=linmod('pitch');

Ao Bo Co=Co(1,:) Do=Do(1,:)

You will see the following output providing the open-loop model of the system.
Ao = -0.3130 -0.0139 0 Bo = 0.2320 0.0203 0 Co = 0 Do = 0 0 1 56.7000 -0.4260 56.7000 0 0 0

Note that the matrices Ao, Bo, Co, and Do are the same at the original matrices A, B,C, and D.

Building a full-state feedback controller


In the CTMS Example: Pitch Controller -- State Space Method page, a full-state feedback controller was designed using the LQR method. We will now construct this controller in Simulink. Bring up your open-loop Pitch model window (or download ours here) Insert a Matrix Gain block below the Plane Pitch Model and change both its Matrix Gain value and it's label to "K". Flip the K matrix block left-to-right. Insert a Sum block to the left of the Plane Pitch Model and change it's value to "+-". Connect the state vector X output of the Plane Pitch Model to the input of the K matrix block. Connect the output of the K matrix block to the second (negative) input of the Sum block. Connect the Step block to the first (positive) input of the Sum block. Connect the Sum block's output to the input of the Plane Pitch Model.

You can download our version of the closed-loop model here.

Closed-loop response
Before simulating the closed-loop system, we must first determine the gain matrix K using the LQR method. Execute the following commands at the MATLAB prompt.
p=50; Q=[0 0 0; 0 0 0; 0 0 p]; [K]= lqr (A,B,Q,1)

Start the simulation in Simulink. Open the scope window and hit the Autoscale button. You should see the following response.

Simulink Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Pitch Controller Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

Example: Modeling the Ball and Beam Experiment in Simulink


Problem Setup and System Equations Building the Simulink Model Open-Loop Response Extracting the Model into MATLAB Constructing a Lead Compensator Controller Closed-Loop Response

Problem Setup
A ball is placed on a beam, see figure below, where it is allowed to roll with 1 degree of freedom along the length of the beam. A lever arm is attached to the beam at one end and a servo gear at the other. As the servo gear turns by an angle theta, the lever changes the angle of the beam by alpha. When the angle is changed from the horizontal position, gravity causes the ball to roll along the beam. A controller will be designed for this system so that the ball's position can be manipulated.

For this problem, we will assume that the ball rolls without slipping and friction between the beam and ball is negligible. The constants and variab for this example are defined as follows: M R d g L mass of the ball 0.11 kg radius of the ball 0.015 m lever arm offset 0.03 m gravitational acceleration 9.8 m/s^2 length of the beam 1.0 m

J ball's moment of inertia r ball position coordinate alpha beam angle coordinate theta servo gear angle The design criteria for this problem are: Settling time less than 3 seconds Overshoot less than 5%

9.99e-6 kgm^2

The second derivative of the input angle alpha actually affects the second derivative of r. However, we will ignore this contribution. The Lagrangian equation of motion for the ball is then given by the following:

The beam angle (alpha) can be expressed in terms of the angle of the gear (theta).

Building the Model in Simulink


In this example, rather than express all the forces and geometric constraints (which is difficult to model in Simulink for dynamic systems with constraints) we will model the nonlinear Lagrangian equation of motion directly. This equation gives d/dt(r) as a function of the state and input variab, r, d/dt(r), alpha, and d/dt(alpha). We will make use of the Nonlinear Function Block to express this function. First, we must express the derivatives of the output, r. Open a new model window in Simulink. Insert an Integrator block from the Linear block library. Insert a second Integrator to the right of the first, and connect the two with a line. Label the line connecting the two "d/dt(r)". To label a line, double-click near the line where you want the label (in this case, just below the line) Draw a line from the second Integrator and label it "r". Insert an Out block from the Connections block library and connect it to the "r" signal line. This will form the output of the system. Change the label of the Out block to "r" by single-clicking on the existing "Out" label.

Now, we will insert the function which takes the vector [r d/dt(r) alpha d/dt(alpha)] and returns d/dt(r). Insert a Fcn block from the Nonlinear library and connect its output to the input of the first Integrator. Edit the Fcn block by double clicking it, and change it's function to the following:
(-1/(J/(R^2)+m))*(m*g*sin(u[3])-m*u[1]*(u[4])^2)

This function block takes an input vector, u, where each component is referred to as u[1], u[2], etc. In our case, u[1]=r, u[2]=d/dt(r), u[3]=alpha, and u[4]=d/dt(alpha).

Close the dialog box and change the label of the Fcn block to "Ball-Beam Lagrangian Model" (you can add newlines in the label by hitting return).

Now, we will begin to construct the function input vector u by feeding back the state signals from the integrators and forming a vector from them with a Mux block. Insert a Mux block from the Connections block library and connect its output to the input of the Ball-Beam block. Edit the Mux block (by double-clicking on it) and change its number of inputs to 4. The Mux block should now have four inputs. Tap a line off the d/dt(r) signal (hold Ctrl while drawing) and connect it to the second input of the Mux block.
3

Tap a line of the r signal and connect it to the first input of the Mux block.

Now we will construct the signals alpha and d/dt(alpha) from the input theta. Insert an In block on the left side of your model window. Change its label to "theta". Insert a Gain block and connect it to the theta block. Change its gain value (double-click on it) to "d/L". Connect the output of the gain block to the third input of the Mux block. Label this line "alpha". Insert a Derivative block from the Linear block library and place it underneath the alpha signal line. Tap a line off the output of the Gain block and connect it to the input of the Derivative block. Connect the output of the Derivative block to the fourth input off the Mux block.

Save your model as "ball.mdl". You can download ours here. Open Loop Response To generate the open-loop response, it is first necessary to contain this model in a subsystem block. Create a new model window (select New from the File menu in Simulink or hit Ctrl-N). Insert a Subsystem block from the Connections block library. Open the Subsystem block by double clicking on it. You will see a new model window labeled "Subsystem". Open your previous model window named ball.mdl. Select all of the model components by selecting Select All from the Edit menu (or hit Ctrl-A). Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit Ctrl-C). Paste the model into the Subsystem window by selecting Paste from the Edit menu (or hit Ctrl-V) in the Subsystem window Close the Subsystem window. You will see the Subsystem block in the untitled window with one input terminal labeled theta and one output terminal labeled r. Resize the Subsystem block to make the labels visible by selecting it and dragging one of the corners. Label the Subsystem block "Ball and Beam Model".

Insert a Step block (from the Sources block library) and connect it to the input of the Ball and Beam Model. Edit the Step block (by double clicking on it to bring up the dialog box) and change the Step Time value to 0. Close the Step block dialog box. Insert a Scope block (from the Sinks block library) and connect it to the output of the Ball and Beam Model.

Before obtaining a step response, we must set the physical parameters Enter the following commands at the MATLAB prompt.
m R g L d J = = = = = = 0.111; 0.015; -9.8; 1.0; 0.03; 9.99e-6;

We are now ready to run the simulation. If you like, you can download our version of the open-loop system here. Start the simulation by selecting Start from the Simulation menu (or hit Ctrl-t). When the simulation is finished, open the Scope by double clicking on it and hit the Scope's autoscale button. You will see the following response.

From this plot it is clear that the system is unstable in open-loop causing the ball to roll right off the end of the beam. Therefore, some method of controlling the ball's position in this system is required. Later in this tutorial, we will implement a lead compensator.

Extracting the Model into MATLAB


The Simulink model can be extracted into an equivalent state-space or transfer function model in MATLAB. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as ball.mdl. In this model, one input, theta (the input crank angle) and one output, r (ball position), were defined. At the MATLAB prompt, enter the following commands
[A,B,C,D]=linmod('ball') [num,den]=ss2tf(A,B,C,D)

You will see the following output providing the open-loop model of the system.
A = 0 0 B = 0 0.2100 C = 1 D = 0 0 1 0

num = 0 den = 1 0 0 0 0.2100

We can verify this model by obtaining an open-loop step response. Enter the following command at the MATLAB prompt:
step(num,den);

You will see the following open-loop response:

Building a Lead Compensator Controller


In the CTMS Example: Solution to the Ball & Beam Problem Using Root Locus Method a lead compensator was designed with a zero at -0.01 and a pole at -5, with a gain of 37.1. We will now construct this controller in Simulink. Bring up your open-loop Ball and Beam model window (or download ours here) Delete the line which connects the Step block to the Ball and Beam model block. Insert a Transfer Function block from the linear block library to the left of the Ball and Beam block, and connect its output to the input of the Ball and Beam block. Edit the Transfer Function block and change its numerator to "[1 0.01]" and its denominator to "[1 5]". Change the label of the Transfer Function block to "Lead Compensator". Insert a Gain block to the left of the Lead Compensator and connect its output to the Lead compensator's input. Change the Gain value to "37.1". Insert a Sum block to the left of the Gain block and change it's value to "+-". Connect the

output of the Sum to the input of the Gain block. Tap a line off the output of the Ball and Beam model and connect it to the negative input of the Sum. Connect the Step block to the positive input of the Sum block

You can download our version of the closed-loop model here.

Closed-Loop Response
Start the simulation in Simulink. Open the scope window and hit the Autoscale button. You should see the following response.

Simulink Examples

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Ball and Beam Examples Tutorials

Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples

You might also like