# 状态空间¶

## ABCD形式¶

$\begin{split}\dot{\vec{x}}(t) &= \vec{f}(\vec{x}(t), \vec{u}(t), t) \\ \vec{y}(t) &= \vec{g}(\vec{x}(t), \vec{u}(t), t)\end{split}$

$\begin{split}\dot{\vec{x}}(t) &= A(t) \vec{x}(t) + B(t) \vec{u}(t) \\ \vec{y}(t) &= C(t) \vec{x}(t) + D(t) \vec{u}(t)\end{split}$

$\begin{split}\dot{\vec{x}}(t) &= A \vec{x}(t) + B \vec{u}(t) \\ \vec{y}(t) &= C \vec{x}(t) + D \vec{u}(t)\end{split}$

## LTI模型¶

model LTI
"Equations written in ABCD form where matrices are also time-invariant"
parameter Integer nx=0 "Number of states";
parameter Integer nu=0 "Number of inputs";
parameter Integer ny=0 "Number of outputs";
parameter Real A[nx,nx]=fill(0,nx,nx);
parameter Real B[nx,nu]=fill(0,nx,nu);
parameter Real C[ny,nx]=fill(0,ny,nx);
parameter Real D[ny,nu]=fill(0,ny,nu);
parameter Real x0[nx]=fill(0,nx) "Initial conditions";
Real x[nx] "State vector";
Real u[nu] "Input vector";
Real y[ny] "Output vector";
initial equation
x = x0 "Specify initial conditions";
equation
der(x) = A*x+B*u;
y = C*x+D*u;
end LTI;


## 向量方程¶

initial equation
x = x0 "Specify initial conditions";


## LTI例子¶

model FirstOrder "Represent der(x) = 1-x"
extends LTI(nx=1,nu=1,A=[-1], B=[1]);
equation
u = {1};
end FirstOrder;


u = {1};


v = {1, 2, 3*4, 5*sin(time)};


model FirstOrder_Compact "Represent der(x) = 1-x"
extends LTI(nx=1,nu=1,A=[-1], B=[1], u={1});
end FirstOrder_Compact;


model NewtonCooling "NewtonCooling model in state space form"
parameter Real T_inf=27.5 "Ambient temperature";
parameter Real T0=20 "Initial temperature";
parameter Real hA=0.7 "Convective cooling coefficient * area";
parameter Real m=0.1 "Mass of thermal capacitance";
parameter Real c_p=1.2 "Specific heat";
extends LTI(nx=1,nu=1,A=[-hA/(m*c_p)],B=[hA/(m*c_p)],x0={20});
equation
u = {T_inf};
end NewtonCooling;


model RotationalSMD
"State space version of a rotational spring-mass-damper system"
parameter Real J1=0.4;
parameter Real J2=1.0;
parameter Real k1=11;
parameter Real k2=5;
parameter Real d1=0.2;
parameter Real d2=1.0;
extends LTI(nx=4, nu=0, ny=0, x0={0, 1, 0, 0},
A=[0, 0, 1, 0;
0, 0, 0, 1;
-k1/J1, k1/J1, -d1/J1, d1/J1;
k1/J2, -k1/J2-k2/J2, d1/J2, -d1/J2-d2/J2]);
equation
u = fill(0, 0);
end RotationalSMD;


$\begin{split}A &= \left| \begin{array}{cccc} 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ -\frac{k_1}{J_1} & \frac{k_1}{J_1} & -\frac{d_1}{J_1} & \frac{d_1}{J_1} \\ \frac{k_1}{J_2} & -\frac{k_1}{J_2}-\frac{k_2}{J_2} & \frac{d_1}{J_2} & -\frac{d_1}{J_2}-\frac{d_2}{J_2} \\ \end{array} \right|\end{split}$

$\begin{split}A &= \left| \begin{array}{cc} \left| \begin{array}{cc} 0 & 0 \\ 0 & 0 \end{array} \right| ~ \left| \begin{array}{cc} 1 & 0 \\ 0 & 1 \end{array} \right| \\ \left| \begin{array}{cc} -\frac{k_1}{J_1} & \frac{k_1}{J_1} \\ \frac{k_1}{J_2} & -\frac{k_1}{J_2}-\frac{k_2}{J_2} \end{array} \right| \left| \begin{array}{cc} -\frac{d_1}{J_1} & \frac{d_1}{J_1} \\ \frac{d_1}{J_2} & -\frac{d_1}{J_2}-\frac{d_2}{J_2} \end{array} \right| \end{array} \right|\end{split}$

model RotationalSMD_Concat
"State space version of a rotationals spring-mass-damper system using concatenation"
parameter Real J1=0.4;
parameter Real J2=1.0;
parameter Real k1=11;
parameter Real k2=5;
parameter Real d1=0.2;
parameter Real d2=1.0;
parameter Real S[2,2] = [-1/J1, 1/J1; 1/J2, -1/J2];
extends LTI(nx=4, nu=0, ny=0, x0={0, 1, 0, 0},
A=[zeros(2, 2), identity(2);
k1*S+[0,0;0,-k2/J2], d1*S+[0,0;0,-d2/J2]],
B=fill(0, 4, 0), C=fill(0, 0, 4),
D=fill(0, 0, 0));
equation
u = fill(0, 0);
end RotationalSMD_Concat;


## 使用部件¶

model RLC "State space version of an RLC circuit"
parameter Real Vb=24;
parameter Real L=1;
parameter Real R=100;
parameter Real C=1e-3;
LTI rlc_comp(nx=2, nu=1, ny=2, x0={0,0},
A=[-1/(R*C), 1/C; -1/L, 0],
B=[0; 1/L],
C=[1/R, 0; -1/R, 1],
D=[0; 0]);
equation
rlc_comp.u = {Vb};
end RLC;


rlc_comp.u = {Vb};