# How to get the output of a controller from MATLAB codes not simulink

14 views (last 30 days)
ojonugwa adukwu on 16 Sep 2022
Commented: ojonugwa adukwu on 22 Sep 2022
In simulink, it is easy to get the control output (or input to the actuator) by connecting a scope to the controller output. How do you get the output of the controller in MATLAB codes? I need the output to apply to another system as well as the plant infront of the controller.
Thanks.

Sam Chak on 16 Sep 2022
Since a sample plant is not provided, we will use a 1st-order plant as an example:
Gp = tf(1, [1 2])
Gp = 1 ----- s + 2 Continuous-time transfer function.
step(Gp, 5)
If we want to improve the settling time at 1 second and to eliminate the steady-state error, we can try:
kp = 5;
ki = 10;
Gc = pid(kp, ki)
Gc = 1 Kp + Ki * --- s with Kp = 5, Ki = 10 Continuous-time PI controller in parallel form.
The closed-loop system is given by
Gcl = feedback(Gc*Gp, 1)
Gcl = 5 s + 10 -------------- s^2 + 7 s + 10 Continuous-time transfer function.
step(Gcl, 5)
And the output of the PI controller, is given by
Gu = feedback(Gc, Gp)
Gu = 5 s^2 + 20 s + 20 ----------------- s^2 + 7 s + 10 Continuous-time transfer function.
step(Gu, 5)
Sam Chak on 16 Sep 2022
For the controller output, or in s-domain, we know the compensator transfer function,
... or .
To find the closed-loop transfer function relating the controller output to the input
we start from , and we need to find the error :
.
From the plant transfer function , we know that
.
Substituiting it back to , we get
.
Similarly, substituiting it back to , we get
Separating at one side and on another side
.
Rearranging that yields
This implies that is on the feedback loop, and thus, it can be obtained with the following syntax:
Gu = feedback(Gc, Gp)

ojonugwa adukwu on 16 Sep 2022
Oh really? Many thanks.
But why is it the transfer function (tf) of Gu and not GC? Because for simulink, it is the transfer function of the controller.
Many thanks
Sam Chak on 16 Sep 2022
You are welcome, @ojonugwa adukwu. If you find the solution in MATLAB code is helpful, please consider accepting ✔ and voting 👍 the Answer. Thanks!
I have added an explanation in the comment under my Answer. It's almost the same argument of why use to get the system output , but not the plant itself.

Paul on 17 Sep 2022
I suggest using interconnection functions to build a single system model that has the control input to the plant as one of the outputs from the overall system model
One approach, for example:
Define the plant
sysp = tf(1,[1 0],'InputName','u','OutputName','y');
Define the controller
sysc = tf(1,1,'InputName','e','OutputName','u');
Unitfy feedback to form the closed loop system. Specify the outputs from the closed loop system as the output and input to the plant
sysclosed = connect(sysc,sysp,sumblk('e = r - y'),'r',{'y' 'u'});
Now we can plot the step (or any other response)
step(sysclosed)
Also, sysclosed can then be connected to other systems to build a large model if desired.
ojonugwa adukwu on 22 Sep 2022
Thanks so much

### Categories

Find more on General Applications in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by