### RRLC circuit modeling and simulation in Scilab and Xcos

For a given resistor-resistor-capacitor-capacitor (RRLC) circuit with the following input parameters:

- constant input voltage:
*u*_{IN}= 12 V - equivalent resistance:
*R*_{1}= 1.5 Ω - equivalent resistance:
*R*_{2}= 2.5 Ω - equivalent capacitance
*C*_{1}= 2 F - equivalent inductance
*L*_{1}= 3 H - simulation time
*t = 10 s* - initial time
*t*_{0}= 0 s - initial voltage across the capacitor
*u*_{C1}(t_{0}) = 0 V - initial voltage across the capacitor
*i*_{L1}(t_{0}) = 0 A

we’ll perform the following tasks:

- find the governing differential equation
- find the transfer function
- find the state-space model
- simulate in Scilab the differential equation
- simulate in Scilab the transfer function
- simulate in Scilab the state-space model
- simulate in Xcos the differential equation
- simulate in Xcos the circuit using Electrical library components
- simulate in Xcos the transfer function
- simulate in Xcos the state-space model

for each activity a plot will be generated for the output and some of the states.

Observation: For the equations to be more readable we are going to remove the time variable (t), from the electrical parameter, for example *i _{L1}(t)* will be

*i*and

_{L1}*u*will be

_{R1}(t)*u*. Also, bear in mind that lower case parameters are variable in time (e.g.

_{R1}*u(t)*,

*i(t)*), while upper case parameters are constant in time (e.g.

*R*,

_{1}*L*,

_{1}*C*).

_{1}### Differential equation of the RRLC circuit

For each loop, from Kirchhoff’s Current and Voltage Law (KCL and KVL) we can write the following equations:

**Loop u _{IN}-C_{1}-R_{1}**

**Loop R _{1}-L_{1}-R_{2}**

The voltage across the resistor *R _{1}* is:

The voltage across the resistor *R _{2}* is:

The current through the capacitor *C _{1}* is:

The voltage across the inductor *L _{1}* is:

From KCL, we can write that:

\[i_{C_{1}} = i_{R_{1}} + i_{L_{1}} \tag{7}\]Also, being part of the same loop, we have:

\[i_{R_{2}} = i_{L_{1}} \tag{8}\]Replacing (3) and (5) in (7), gives:

\[C_{1} \frac{du_{C_{1}}}{dt} = \frac{1}{R_{1}} u_{R_{1}} + i_{L_{1}} \tag{9}\]Replacing (4), (6) and (8) in (2), gives:

\[L_{1} \frac{di_{L_{1}}}{dt} = u_{R_{1}} – R_{2} i_{L_{1}} \tag{10}\]From (1) we get:

\[u_{R_{1}} = u_{IN} – u_{C_{1}} \tag{11}\]Replacing (11) in (9) and (10) gives the **system of differential equations governing the RRLC circuit**:

\frac{di_{L_{1}}}{dt} &=& – \frac{R_{2}}{L_{1}} i_{L_{1}} &-& \frac{1}{L_{1}} u_{C_{1}} &+& \frac{1}{L_{1}} u_{IN}\\

\frac{du_{C_{1}}}{dt} &=& – \frac{1}{R_{1}C_{1}} u_{C_{1}} &+& \frac{1}{C_{1}} i_{L_{1}} &+& \frac{1}{R_{1}C_{1}} u_{IN}

\end{matrix}\right.} \tag{12}\]

### Transfer function of the RRLC circuit

Applying Laplace’s transform to equations (12) gives:

\[s I_{L_{1}}(s) – I_{L_{1}}(t_{0}) = – \frac{R_{2}}{L_{1}} I_{L_{1}}(s) – \frac{1}{L_{1}} U_{C_{1}}(s) + \frac{1}{L_{1}} U_{IN}(s) \tag{13}\]and

\[s U_{C_{1}}(s) – U_{C_{1}}(t_{0}) = – \frac{1}{R_{1}C_{1}} U_{C_{1}}(s) + \frac{1}{C_{1}} I_{L_{1}}(s) + \frac{1}{R_{1}C_{1}} U_{IN}(s) \tag{14}\]From the initial data we also know the initial conditions for the capacitor voltage and inductor current, at the initial time, are:

\[\begin{split}U_{C_{1}}(t_{0}) = 0\\

I_{L_{1}}(t_{0}) = 0

\end{split}\]

After we replace the initial conditions in (13), we can extract:

\[I_{L_{1}}(s) = \frac{\frac{1}{L} \left ( U_{IN}(s) – U_{C_{1}}(s) \right )}{s + \frac{R_{2}}{L_{1}}} \tag{15}\]Replacing (15) in (14) and considering *U _{C1}(s)* as the output of the system, gives the

**transfer function of the RRLC circuit**as:

After we replace the initial conditions in (13), we can extract:

\[ U_{C_{1}}(s) = \frac{\frac{1}{C_{1}} I_{L_{1}}(s) + \frac{1}{R_{1}C_{1}} U_{IN}(s)}{s + \frac{1}{R_{1}C_{1}}} \tag{17}\]Replacing (17) in (13) and considering *I _{L1}(s)* as the output of the system, gives the

**transfer function of the RRLC circuit**as:

### State-space model of the RRLC circuit

After defining *u _{C1}(t)* and

*i*as the states of the system, we can write (12) in matrix multiplication form:

_{L1}(t)From (19) we can extract the *A* and *B* matrices of the state-space:

We also define the state variables as being the output of the system:

\[y(t)=\begin{Bmatrix}u_{C_{1}}\\

i_{L_{1}}

\end{Bmatrix} \tag{20}\]

From (20) we can extract the *C* and *D* matrices of the state-space:

If we define as output the voltage across the resistor uR2(t):

\[y(t) = u_{R_{2}} = R_2 i_{L_{1}} \tag{21}\]we will have the following C and D matrices:

\[\bbox[#FFFF9D]{C = \begin{bmatrix} 0 & R_{2} \end{bmatrix}, \quad D = \begin{bmatrix} 0 \end{bmatrix}}\]### RRLC circuit differential equation – Scilab simulation

One way of solving the differential equation of the RRLC circuit is by using Scilab `ode()`

function. In the Scilab instructions below we are defining the input parameters, the differential equations, initial parameters, solve the differential equation and plot the results.

// Clean figure, console and workspace variables clf() clc() clear() // Define input parameters uIN=12; // [V] R1=1.5; // [ohm] R2=2.5; // [ohm] C1=2; // [F] L1=3; // [H] // Define t [s] t0=0; tinc=0.001; tf=10; t=t0:tinc:tf; // Define differential equation function dx=f(t, x) dx(1)=(-R2/L1)*x(1)-(1/L1)*x(2)+(1/L1)*uIN; dx(2)=(-1/(R1*C1))*x(2)+(1/C1)*x(1)+(1/(R1*C1))*uIN; endfunction // Define initial conditions iL10=0; // [A] uC10=0; // [V] // Solve differential equation y=ode([iL10;uC10],t0,t,f); iL1 = y(1,:); uC1 = y(2,:); uR2 = iL1*R2; // Plot numeric solution subplot(3,1,1) title("x-engineer.org","Color",'b') plot(t,iL1,'b'), xgrid() ylabel('$\large{i_{L1}(t)\text{ [A]}}$','fontsize',2) subplot(3,1,2) plot(t,uC1,'r'), xgrid() ylabel('$\large{u_{C1}(t)\text{ [V]}}$','fontsize',2) subplot(3,1,3) plot(t,uR2,'m'), xgrid() ylabel('$\large{u_{R2}(t)\text{ [V]}}$','fontsize',2) xlabel('$\large{t\text{ [s]}}$','fontsize',2)

After running the Scilab instructions above, we get the following plot.

The plot represents the current through the inductor *i _{L1}(t)* (blue), the voltage across the capacitor

*u*(red) and the voltage across the resistor

_{C1}(t)*u*(magenta).

_{R2}(t)### RRLC circuit transfer function – Scilab simulation

The same results we are going to have using the transfer function. In the Scilab instruction below we are defining the system (RRLC circuit) as a transfer function using Scilab’s `syslin()`

function. After, we run a simulation for a step input of *u _{IN}* and time

*t*.

// Define transfer function for uC output s=poly(0,'s'); num1=L1*C1*s+C1*R2+C1*R1; den1=R1*L1*C1^2*s^2+(R1*R2*C1^2+L1*C1)*s+C1*R2+C1*R1; Hs1=[num1/den1]; sys=syslin('c',Hs1) // Run step response uC1 = uIN*csim('step',t,sys); figure(2) plot(t,uC1), xgrid() hf=gcf(); hf.background = -2; title("x-engineer.org","Color",'b') ylabel('$\large{u_{C1}(t) \text{ [V]}}$','fontsize',2) xlabel('$\large{t} \text{ [s]}$','fontsize',2) // Define transfer function for iL output s=poly(0,'s'); num2=L1*R1*C1*s; den2=L1^2*R1*C1*s^2+(L1^2+L1*R1*R2*C1)*s+L1*R2+L1*R1; Hs2=[num2/den2]; sys=syslin('c',Hs2) // Run step response iL1 = uIN*csim('step',t,sys); figure(3) plot(t,iL1), xgrid() hf=gcf(); hf.background = -2; title("x-engineer.org","Color",'b') ylabel('$\large{i_{L1}(t) \text{ [A]}}$','fontsize',2) xlabel('$\large{t} \text{ [s]}$','fontsize',2)

As expected, the results is the same as for the differential equation integration (simulation).

### RRLC circuit state-space – Scilab simulation

The same results we are going to have using the state-space model. In the Scilab instruction below we are defining the system (RRLC circuit) as a state-space model using Scilab’s `syslin()`

function. After, we run a simulation for a step input of *u _{IN}* and time

*t*.

// Define state-space model A = [-1/(R1*C1) 1/C1;-1/L1 -R2/L1]; B = [1/(R1*C1); 1/L1]; C = [0 R2]; D = [0]; X0 = [uC10;iL10]; sys = syslin('c',A,B,C,D,X0); // Run step response uR2 = uIN*csim('step',t,sys); figure(4) plot(t,uR2,'m'), xgrid() hf=gcf(); hf.background = -2; title("x-engineer.org","Color",'b') ylabel('$\large{u_{R2}(t) \text{ [V]}}$','fontsize',2) xlabel('$\large{t} \text{ [s]}$','fontsize',2)

As expected, the results is the same as for the differential equation integration (simulation).

### RRLC circuit differential equation – Xcos simulation

Equations (12) are modelled into Xcos using block diagram as:

Parameters `uIN`

, `R1`

, `R2`

, `L1`

and `C1`

are loaded into the Scilab workspace from the previous examples.

The simulation is run for `10 s`

and outputs the following plot.

The plot represents the current through the inductor *i _{L1}(t)* (blue) and the voltage across the capacitor

*u*(red).

_{C1}(t)### RRCC circuit electrical components – Xcos simulation

To verify that our differential equation, transfer function and state-space model are correct, we are going to model the RRLC circuit using the Electrical library blocks from Xcos.

Parameters `uIN`

, `R1`

, `R2`

, `L1`

and `C1`

are loaded into the Scilab workspace from the previous examples.

Running the simulation will output the same time variation for *u _{C1}(t)* and

*i*, which proves that the differential equation, transfer function and state-space model of the RRLC circuit are correct.

_{L1}(t)### RRLC circuit transfer function – Xcos simulation

In this approach we are going to use the transfer function of the RRLC circuit and simulate it in Xcos.

Parameters `uIN`

, `R1`

, `R2`

, `L1`

and `C1`

are loaded into the Scilab workspace from the previous examples.

Running the simulation will output the same time variation for *u _{C1}(t)* and

*i*, which is the same as the one in the previous examples.

_{L1}(t)### RRLC circuit state-space – Xcos simulation

In this approach we are going to use the state-space model of the RRLC circuit and simulate it in Xcos.

Parameters `uIN`

, `A`

, `B`

, `C`

, `D`

and `X0`

are loaded into the Scilab workspace from the previous examples.

Running the simulation will output the variation of the voltage across the resistor *u _{R2}(t)*, which is the same as the one in the previous examples.

If needed, the constant input voltage block can be replace with a step, ramp or pulse input. This will simulate the response of the circuit for various standard control system inputs.