How to get the output of a controller from MATLAB codes not simulink
    5 views (last 30 days)
  
       Show older comments
    
    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.
0 Comments
Accepted Answer
  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])
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)
The closed-loop system 
 is given by
Gcl = feedback(Gc*Gp, 1)
step(Gcl, 5)
And the output of the PI controller, 
 is given by
Gu  = feedback(Gc, Gp)
step(Gu, 5)
1 Comment
  Sam Chak
      
      
 on 16 Sep 2022
				For the controller output, 
 or 
 in s-domain, we know the compensator transfer function, 
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)
More Answers (2)
  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 16 Sep 2022
        1 Comment
  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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


