## Thursday, 14 July 2011

### Simulation of a closed loop system with a Controller

In the previous post http://matlabbyexamples.blogspot.com/2011/07/simulation-of-system-described-by-its.html, 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)
k=10;
v=k*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
u=1*(t>0);
%Input to controller: Feedback from sys1
e=u-y;
%Controller
v=controller1(e);
%Plant
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
y0=8;
[t y]= ode45(@env2,tspan,y0);
The output is defined as y and can be plotted by
plot(t,y);
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.

## Wednesday, 13 July 2011

### Simulation of a system described by its Dynamics

The present post and following few posts will talk about the simulation of any system described by its dynamics in MATLAB. We will see how we can simulate an open system and closed system with some small examples.

System Dynamics
Any system is described by its dynamics, Dynamics, in simple words, means how the outputs are dependent on current input and some internal variables called state along with how derivatives of  these states are behaving. Take this example of an electronic circuit with a Resistance R, an inductance L and a capacitance C connected as in Figure 1. Output is the voltage across L and input is the voltage source. This system has two internal parameters Vc and i which affect the output as

$V_o = V-(V_c + iR)$
Now this Vc and i are dynamic and their derivatives are given as
$\frac{dV_c}{dt}=\frac{i}{C}$
$\frac{di}{dt}=\frac{1}{L}(V-(V_c + iR))$

As you can see derivatives of states are dependent on state and inputs and so is output. This type of representation is also called state space model.
 Fig 1: LCR System with two states

For simulation purposes, we will take two models- one is simple integrator system and second is cruise system.

System1-Integrator model
This system has only one state y which is its output too. State dynamics are given as
$\frac{dy}{dt}=u(t)$
where u(t) is the input to this system.

To create this model, we will make a function sys1 with input u, y and t and which returns derivative term of y. This function describes the dynamics of system, or in other words it represents the system itself.
function dybydt = sys1(t,y,u)
dybydt = u ;
Building an Environment
Now build an environment which contains the model and its stimulus input. Let us take step signal as input for this example.
function dybydt = env1(t,y)
u=1*(t>0);
dybydt = sys1(t,y,u) ;
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
y0=8;
[t y]= ode45(@env1,tspan,y0);
The output is defined as y and can be plotted by
plot(t,y);

as in Fig 2.
 Fig 2: Output of system 1