# Help with simulating a system model (inverted pendulum)

In this documentation article, Regulate an Inverted Pendulum, the author started by creating the following system.

pendulum =
StateSpaceModel[
{(M + m) x''[t] - m l Sin[θ[t]] θ'[t]^2 + m l Cos[θ[t]] θ''[t] ==
F[t] + d[t] Cos[θ[t]],
m x''[t] Cos[θ[t]] + m l θ''[t] == m g Sin[θ[t]] + d[t]},
{θ[t], θ'[t], x[t], x'[t]},
{F[t], d[t]},
{θ[t], x[t]},
t] /. {M -> 5.6, m -> 0.53, l -> 0.85, g -> 9.8};

poles = {-1 + 2 I, -1 - 2 I, -5, -7};


Then he got the feedback gains

k =
Join[
StateFeedbackGains[SystemsModelExtract[pendulum, 1], poles],
ConstantArray[0, 4]}]


and plotted the output response with x and θ, keeping θ close to zero, with the following

Plot[
OutputResponse[
SystemsModelStateFeedbackConnect[pendulum, k],
{0,UnitStep[t] - UnitStep[t - 1]},
{t, 4}] // Evaluate,
{t, 0, 4},
PlotRange -> All,
PlotStyle -> {Blue, Red},
Epilog ->
Inset[Column[{Style["x(t)", 10, Blue], Style["θ(t)", 10, Red]}], {3, 0.004}],
AxesLabel -> {"t"}]


My question is how to pass the desired position and angle he wants the system to stabilize at; i.e., x = 1, θ = π/6

• Is it even possible for this model to stabilize with the pendulum 30° from vertical? – m_goldberg Dec 12 '16 at 2:48
• I believe it is possible to stabilize at 30°, because one of the model inputs is the force d[t] applied at right angles to the pendulum. Expect d[t] to swing negative when $\theta$ is positive. Also, the system model has outputs $\{\theta [t], x[t]\}$ in that order, so the blue curve is $\theta$ and the red one is $x$, if I'm not mistaken. – LouisB Dec 12 '16 at 10:08
• Just pass it as the input at the OutputResponse. Instead of {0,UnitStep[t] - UnitStep[t - 1]} pass {0,1}. – Phab Dec 12 '16 at 14:50
• ok, i read the example: "to regulate an inverted pendulum about the upright position", so it should be stabilized at θ = 0 at any time. If you want to stabilize the pendulum at θ = π/6, you can't stabilize the position x=1. You would drive the car with constant speed in one direction (x grows). – Phab Dec 12 '16 at 15:11

StateFeedbackGains solves a regulator problem.

You need to try AsymptoticOutputTracker to track a reference trajectory (which are constants in your case).

pendulum = AffineStateSpaceModel[{(M + m) x''[t] -
m l Sin[θ[t]] θ'[t]^2 +
m l Cos[θ[t]] θ''[t] ==
F[t] + d[t] Cos[θ[t]],
m x''[t] Cos[θ[t]] + m l θ''[t] ==
m g Sin[θ[t]] + d[t]}, {θ[t], θ'[t], x[t],
x'[t]}, {F[t], d[t]}, {θ[t], x[t]}, t] /. {M -> 5.6,
m -> 0.53, l -> 0.85, g -> 9.8};

poles = {-1 + 2 I, -1 - 2 I, -5, -7};
fb = AsymptoticOutputTracker[pendulum, {π/6, 1}, poles];
csys = SystemsModelStateFeedbackConnect[pendulum, fb];

Plot[Evaluate@OutputResponse[csys, {0}, {t, 0, 4}], {t, 0, 4},
PlotLegends -> {θ[t], x[t]}]