I'm trying to grasp the new control system functions in Mathematica 8. I'd like to connect a controller model to a model of a plant to simulate the behavior of the system.

I define a simple servo model:

servo = StateSpaceModel[{x''[t] == u[t] - x'[t]}, {{x''[t], 0}}, {{u[t], 0}}, x[t], t]

where x[t] is the position of the servo and u[t] is the input (voltage).

and a simple PID controller:

pid = TransferFunctionModel[5 + 0.01*s - 0.00001/s, s]

Now I assumed that I could link the PID controller to the servo model using SystemsModelFeedbackConnect:

loop = SystemsModelFeedbackConnect[TransferFunctionModel[servo], pid]

But the system doesn't behave as I would have expected:

input = UnitStep[t - 1] - 0.5 UnitStep[t - 10];
output = OutputResponse[loop, input, t];
Plot[{input, output}, {t, 0, 30}]


There's a lot of overshoot because the PID is not optimized at all, but I would have expected that the P part of the controller would pull the output to the (target) input eventually. But it seems as if the P factor scales the input, rather than the error.


1 Answer 1


You need to first do a series connection of the PID controler to the plant. This gives the open loop transfer function. Then do a unity feedback connect to close the loop, like this

plant = StateSpaceModel[{x''[t] == u[t] - x'[t]}, {{x''[t],0}}, {{u[t], 0}}, x[t], t];
kip = 5; ki = -0.00001; kid = 0.01;
pid = TransferFunctionModel[(kip*s + ki + kid*s^2)/s, s] ;
openLoop = SystemsModelSeriesConnect[TransferFunctionModel[plant], pid];
closedLoop = SystemsModelFeedbackConnect[openLoop];
input = UnitStep[t - 1] - 0.5 UnitStep[t - 10];
output = OutputResponse[closedLoop, input, t];
Plot[{input, output}, {t, 0, 30}]

enter image description here

fyi, here is a demo http://demonstrations.wolfram.com/SimulationOfFeedbackControlSystemWithControllerAndSecondOrde/

  • 1
    $\begingroup$ I'd like to point out that @Nasser's demonstration cited above includes the nicest technique for object-oriented programming in Mathematica that I have seen. $\endgroup$
    – Reb.Cabin
    Oct 22, 2017 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.