# Hydraulic System

#### Flow rates depend on pressure differences and geometry.

 In[1]:= XflowRate = (p1[t] - pB[t]) (\[Pi] pipeDia^4)/(128 \[Mu] pipeLenB); flowRate1 = (pB[t] - p2[t]) (\[Pi] pipeDia^4)/(128 \[Mu] pipeLen1); flowRate2 = (pB[t] - p3[t]) (\[Pi] pipeDia^4)/(128 \[Mu] pipeLen2);

#### Create a descriptor StateSpaceModel from the DAEs.

 In[2]:= XflowRateConstraint = (flowRate == flowRate1 + flowRate2); pressureEqns = {p1[t] == \[Rho] g h1[t], p2[t] == \[Rho] g h2[t], p3[t] == \[Rho] g h3[t]}; massConservation = {a1 h1'[t] == -flowRate + inflow[t], a2 h2'[t] == flowRate1, a3 h3'[t] == flowRate2 - a3 h3[t]/10};
 In[3]:= Xparams = {pipeLenB -> 1/10, pipeLen1 -> 1/10, pipeLen2 -> 1/5, pipeDia -> 1/5, \[Rho] -> 1/5, \[Mu] -> 2 10^-3, g -> 9.81, a1 -> 1, a2 -> 1, a3 -> 1};
 In[4]:= Xtanks = StateSpaceModel[ Join[pressureEqns, massConservation, {flowRateConstraint}] , {h1[t], h2[t], h3[t], pB[t], p1[t], p2[t], p3[t]}, {inflow[t]}, {h1[t], h2[t], h3[t]}, t] /. params
 Out[4]=

#### The tanks take over a minute to settle from a nonzero initial condition.

 In[5]:= Xtanks = StateSpaceModel[ Join[pressureEqns, massConservation, {flowRateConstraint}] , {h1[t], h2[t], h3[t], pB[t], p1[t], p2[t], p3[t]}, {inflow[t]}, {h1[t], h2[t], h3[t]}, t] /. params; OutputResponse[{tanks, {1, 0, 0, 0, 0, 0, 0}}, {0}, {t, 0, 60}]; Plot[%, {t, 0, 60}, PlotRange -> All, ImageSize -> Medium]
 Out[5]=

#### Find optimal state feedback gains based on cost matrices for the states and inputs. The resulting closed-loop system settles much faster.

 In[6]:= Xk = Chop@LQRegulatorGains[tanks, {\!\(\* TagBox[ RowBox[{"(", "", GridBox[{ {"10", "0", "0", "0", "0", "0", "0"}, {"0", "10", "0", "0", "0", "0", "0"}, {"0", "0", "10", "0", "0", "0", "0"}, {"0", "0", "0", "2", "0", "0", "0"}, {"0", "0", "0", "0", "2", "0", "0"}, {"0", "0", "0", "0", "0", "2", "0"}, {"0", "0", "0", "0", "0", "0", FractionBox["1", "100"]} }, GridBoxAlignment->{ "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}], "", ")"}], Function[BoxForm`e\$, MatrixForm[BoxForm`e\$]]]\), {{100}}}];
 In[7]:= XclosedloopTanks = SystemsModelStateFeedbackConnect[tanks, k];
 In[8]:= XclosedloopTanks = SystemsModelStateFeedbackConnect[tanks, k];; OutputResponse[{closedloopTanks, {100, 0, 0, 0, 0, 0, 0}}, {0}, {t, 0, 60}]; Plot[%, {t, 0, 60}, PlotRange -> All, ImageSize -> Medium]
 Out[8]=