Problem: Given the inverted pendulum shown below, use state space using one input (the force on the cart) and 2 outputs (the cart horizontal displacement, and the pendulum angle. Plot each output separately for the same input.
Mathematica
Remove["Global`*"]; a = {{0, 1, 0, 0}, {0, 0, ((-m)*g)/M, 0}, {0, 0, 0, 1}, {0, 0, ((M + m)*g)/(M*L), 0}};
b = {{0}, {1/M}, {0}, {-(1/(M*L))}}; MatrixForm[b]
c = {{1, 0, 0, 0}, {0, 0, 1, 0}}; MatrixForm[b]
sys=StateSpaceModel[{a,b ,c}]
It is now possible to obtain the response of the system as analytical expression or an interpolatingFunction.
It is much more efficient to obtain the response as interpolatingFunction. This requires that the time domain be given.
Here is example of obtaining the analytical expression of the response
sys=sys/.{g->9.8,m->1,M->20,L->1}; y=Chop[OutputResponse[sys,UnitStep[t],t]]
Plot[y[[1]],{t,0,3}, PlotLabel->"y(t) response", FrameLabel->{"time (sec)","y (meter)"}, Frame->True, PlotRange->All, ImageSize->300, GridLines->Automatic, GridLinesStyle->Dashed, RotateLabel->False]
Plot[ y[[2]],{t,0,3}, PlotLabel->"\[Theta](t) response", FrameLabel->{"time (sec)","\[Theta] (radians)"}, Frame->True, PlotRange->All, ImageSize->300, GridLines->Automatic, GridLinesStyle->Dashed, RotateLabel->False]
Matlab
clear all; close all; m=1; M=20; L=1; g=9.8; A=[0 1 0 0; 0 0 -m*g/M 0; 0 0 0 1; 0 0 (M+m)*g/(M*L) 0 ]; B=[0 1/M 0 -1/(M*L)]'; C=[1 0 0 0; 0 0 1 0]; D=[0]; sys=ss(A,B,C(1,:),0); step(sys,3); grid on; set(gcf,'Position',[10,10,320,320]); title('y(t) due to step input');
figure; sys=ss(A,B,C(2,:),0); step(sys,3); title('\theta(t) due to step input'); grid on; set(gcf,'Position',[10,10,320,320]);