Thursday, 14 July 2011

Simulation of a closed loop system with a Controller

In the previous post, we have seen the response of the system was unbounded even for bounded input (a step signal). Here we will try to put a controller to stablize the output.

A feedback with controller
We will use a standard technique to stabilize the output known as feedback where output of the plant is fedback to the plant as an input. The Figure 1 illustrates an system and a derived system formed by connecting output to input through a controller. Controller is a small system which takes the difference between original input and the output and generates a controlled input which is fed to main system. This derived system is also known as closed loop system while the original is known as open loop system.
Fig 1 : Open and Closed Loop System

Building an Environment 
First we will make a controller which is just proportional controller with gain 10.
function v = controller1(e)

 Now build an modified environment which contains the closed loop model and its stimulus input. Let us take step signal as input for this example.
function dybydt = env2(t,y)
%Reference Input Signal
%Input to controller: Feedback from sys1
dybydt = sys1(t,y,v) ;

Simulating the system
The above build environment can be simulated using ode45. Create a blank script file and write the following in that.
%define timespan
tspan=[0 10];
%define initial value
[t y]= ode45(@env2,tspan,y0);
 The output is defined as y and can be plotted by
as in Fig 2.
Fig 2: Response of a Closed Loop System
 Now you can see, output is stabilized and bounded for step input. You can explore the effect of controller by changing the value of controller gain k and type of controller.


  1. Huge thanks for the example - it really helps to understand what is going on inside Simulink when it calculates linear systems.

    I'm just wondering if you can provide an example for the arbitrary system with transfer function, say, 1/(s^2+s). I mean how to explain this to the ode45 function.

    I should say that with this examples, you are doing a very good job explaining what happens inside Simulink. I'm going to design a controller for the rather complex system, and sometimes the response of Simulink is quite strange. That's why I think implementation in MATLAB is useful.

  2. One way to do this is to convert this problem in to a Time Domain Problem,
    A system with tf G(s)=1/(s^2+s)

    so let us take

    so the system becomes
    y1'(t)= y2(t)

    and your output is y1(t)
    Now since the system is second order, you need to have two states and both are passed on ode45
    as a vector (size 2x1) Y

    so sys1 will look like
    function dybydt = sys1(t,Y,u)
    dybydt =[-y2+u;y2] ;

  3. Let us create a env for this

    function dybydt = env1(t,Y)
    dybydt = sys1(t,Y,u) ;

    while simulation we will write,

    %define timespan
    tspan=[0 10];
    %define initial value
    y0=[8 ;0];
    [t Y]= ode45(@env1,tspan,y0);
    %plotting the first column only as output is y1(t)

  4. Thanks for your comments,
    Currently i am in search of topics/problems for blogs in MATLAB, Do tell me if you want to read a blog in a particular problem.

    In case of more queries, visit this page again