## Contents

```% Skills Assessment - Chap 10
```

## P10.1 Inverted pendulum

```clear variables
g=9.8;
disp('Sampling time:'), Ts=.1
A=[0 1; g 0]; B=[0; 1]; C=[1 0]; D=0;
IP=ss(A,B,C,D);
disp('Discrete-time model:')
IPz=c2d(IP,Ts);
u=rand(1,10); x=rand(2,1);
for i=1:length(u), x(:,i+1)=f1(x(:,i),u(i)); end
y=C*x;
disp('Random input:'), disp(u)
disp('Model output:'), disp(y)
```
```Sampling time:
Ts =
0.1000
Discrete-time model:
1.0494    0.1016
0.9961    1.0494
Bd =
0.0050
0.1016
Random input:
Columns 1 through 7
0.5578    0.3134    0.1662    0.6225    0.9879    0.1704    0.2578
Columns 8 through 10
0.3968    0.0740    0.6841
Model output:
Columns 1 through 7
0.4024    0.5250    0.7038    0.9546    1.3037    1.7898    2.4585
Columns 8 through 11
3.3722    4.6225    6.3318    8.6706
```

## P10.2 Airplane dynamics

```A=[-1 1;-7 -.5]; B=[-.1;-5];
C=eye(2); D=[0;0];
AP=ss(A,B,C,D);
disp('Sampling time:'), Ts=.01
APz=c2d(AP,Ts);
APz.y={'AOA','q'};
disp('Discrete-time model:')
disp('Transfer functions:')
tf(APz)
x=Bd;
for i=1:length(u), x(:,i+1)=f1(x(:,i),u(i)); end
disp('Model outputs:')
disp('AOA:')
disp(x(1,:))
disp('Pitch rate:')
disp(x(2,:))
```
```Sampling time:
Ts =
0.0100
Discrete-time model:
0.9897    0.0099
-0.0695    0.9947
Bd =
-0.0012
-0.0498
Transfer functions:

ans =

From input to output...
-0.001244 z + 0.0007424
AOA:  -----------------------
z^2 - 1.984 z + 0.9851

-0.04983 z + 0.04941
q:  ----------------------
z^2 - 1.984 z + 0.9851

Sample time: 0.01 seconds
Discrete-time transfer function.

Model outputs:
AOA:
Columns 1 through 7
-0.0012   -0.0036   -0.0018    0.0021    0.0103    0.0276    0.0534
Columns 8 through 11
0.0867    0.1318    0.1924    0.2757
Pitch rate:
Columns 1 through 7
-0.0498    0.0032    0.0316    0.0483    0.1160    0.2324    0.2887
Columns 8 through 11
0.3823    0.5279    0.6928    0.9882
```

## P10.3 DC motor

```R=1; L=.01; J=.01; b=.1; k=.02;
A=[R/L -k/L; k/J -b/J]; B=[1/L; 0];
C=[0 1]; D=[0];
DC=ss(A,B,C,D);
Ts=.01;
DCz=c2d(DC,Ts);
disp('Discrete-time model:')
disp('Pole placement controller:')
syms k1 k2 real
p=charpoly(Acl);
k=solve(p(2)+2*exp(-1), p(3)-exp(-2),k1,k2);
k=double([k.k1 k.k2])
DC1=DCz;
DC1.a=DCz.a-DCz.b*k;
k=solve(p(2), p(3),k1,k2);
k=double([k.k1 k.k2])
DC2=DCz;
DC2.a=DCz.a-DCz.b*k;
disp('Step responses:')
c=evalfr(DC1,1)/evalfr(DC2,1);
step(DC1, DC2)
```
```Discrete-time model:
2.7179   -0.0330
0.0330    0.9046
Bd =
1.7182
0.0139
Pole placement controller:
k =
1.6057    9.1701
k =
1.8968   26.0751
Step responses:
```

## P10.4 Flexible beam

```wn=1e2; zi=.05;
A=[0 1; -wn^2 -2*zi*wn]; B=[0; wn]; C=[wn 0];
FB=ss(A,B,C,0);
FBz=c2d(FB,.001)
disp('Controller design:')
k=place(FBz.a,FBz.b,exp(.25*[-1+j -1-j]))
FBz.a=FBz.a-FBz.b*k;
disp('Step response:')
step(FBz)
```
```FBz =

a =
x1         x2
x1      0.995  0.0009934
x2     -9.934     0.9851

b =
u1
x1  4.979e-05
x2    0.09934

c =
x1   x2
y1  100    0

d =
u1
y1   0

Sample time: 0.001 seconds
Discrete-time state-space model.

Controller design:
k =
879.2029    4.3001
Step response:
```

## P10.5 Airplane dynamics

```A=[-1 1;-7 -.5]; B=[-.1;-5];
C=eye(2); D=[0;0];
AP=ss(A,B,C,D);
AP.y={'AOA';'PR'};
Ts=.05;
APz=c2d(AP,Ts);
disp('Pole placement controller:')
H1=tf({k(1),k(2)},1);
T1=feedback(APz,H1);
syms k1 k2 real
p=charpoly(Acl);
k=solve(p(2), p(3),k1,k2);
k=double([k.k1 k.k2])
H2=tf({k(1),k(2)},1);
T2=feedback(APz,H2);
disp('Step responses:')
c=evalfr(T1(2),1)/evalfr(T2(2),1);
step(T1(1), T2(1))
title('AOA')
figure, step(T1(2), T2(2))
title('Pitch rate')
```
```Pole placement controller:
k =
-1.5336   -1.3747
k =
-77.1633   -4.3400
Step responses:
```

## P10.6 Human postural dynamics

```G=tf([10],[1 0 -10]);
G=ss(G);
Ts=.1;
disp('Discrete-time model:')
Gz=c2d(G,Ts);
disp('State feedback controller:')
%K=place(G.a,G.b,[-5+j -5-j]);
syms k1 k2 real
p=charpoly(Acl);
k=solve(p(2), p(3),k1,k2);
K2=double([k.k1 k.k2])
G1=Gz; G1.a=Gz.a-Gz.b*K1;
G2=Gz; G2.a=Gz.a-Gz.b*K2;
step(G1,G2)
```
```Discrete-time model: