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

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

- constant input voltage:
*u*_{IN}= 12 V - equivalent resistance:
*R*_{1}= 0.5 Ω - equivalent resistance:
*R*_{2}= 1.2 Ω - equivalent capacitance
*C*_{1}= 0.02 F - equivalent capacitance
*C*_{2}= 0.05 F - simulation time
*t = 0.5 s* - initial time
*t*_{0}= 0 s - initial voltage across the 2
^{nd}capacitor*u*_{C20}(t_{0}) = 0 V - initial voltage gradient across the 2
^{nd}capacitor*du*_{C20}/dt_{0}= 0 V/s

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 RRCC circuit

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

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

**Loop C _{1}-R_{2}-C_{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 current through the capacitor *C _{2}* is:

From KCL, we can write that:

\[i_{R_{1}} = i_{R_{2}} + i_{C_{1}} \tag{7}\]Replacing (6) in (4) and the result in (2) gives:

\[u_{C_{1}}=u_{C_{2}}+ R_{2} C_{2} \frac{du_{C_{2}}}{dt} \tag{8}\]Replacing (8) in (5) gives:

\[i_{C_{1}} = C_{1} \frac{du_{C_{2}}}{dt} + C_{1}R_{2}C_{2} \frac{d^2u_{C_{2}}}{dt^2} \tag{9}\]Replacing (7) in (1) gives:

\[u_{IN} = R_{1} \left ( i_{R_{2}} + i_{C_{1}} \right ) + u_{C_{1}} \tag{10}\]Being part of the same loop, we have:

\[i_{R_{2}} = i_{C_{2}} \tag{11}\]Replacing (11) in (10) gives:

\[u_{IN} = R_{1} \left ( i_{C_{2}} + i_{C_{1}} \right ) + u_{C_{1}} \tag{12}\]Replacing (6), (9) and (8) in (12) gives the **governing differential equation of the RRCC circuit**:

In order to simplify the equation (13), we are going to do the following assignment:

\[\begin{split}a &= R_{1} C_{1} R_{2} C_{2}\\

b &= R_{1} C_{1} + R_{1} C_{2} + R_{2} C_{2}

\end{split}\]

Which leads to the simplified differential equation of the RRCC circuit:

\[\bbox[#FFFF9D]{a \frac{d^2u_{C_{2}}}{dt^2} + b \frac{du_{C_{2}}}{dt}+u_{C_{2}}=u_{IN}} \tag{14}\]### Transfer function of the RRCC circuit

Applying Laplace’s transform to equation (14) gives:

\[ \begin{split}\mathcal {L} \left [ a \frac{d^{2}u_{C_{2}}}{dt^2} \right ] &= a s^2 U_{C_{2}}(s) – s U_{C_{2}}(t_{0}) – \frac{dU_{C_{2}}}{dt_{0}}\\

\mathcal {L} \left [ b \frac{du_{C_{2}}}{dt} \right ] &= b s U_{C_{2}}(s) – U_{C_{2}}(t_{0})\\

\mathcal {L} \left [ u_{C_{2}}(t) \right ] &= U_{C_{2}}(s)\\

\mathcal {L} \left [ u_{IN}(t) \right ] &= U_{IN}(s)\\

\end{split} \]

From the initial data we also know the initial conditions for the capacitor voltage and voltage derivative (gradient) at the initial time are:

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

\frac{dU_{C_{2}}}{dt_{0}}=0

\end{split}\]

Replacing the Laplace transforms and initial conditions in (14) gives:

\[a s^2 U_{C_{2}}(s) + b s U_{C_{2}}(s) +U_{C_{2}}(s) = U_{IN}(s) \tag{15}\]From (15) and defining *u _{C2}(t)* as the output variable, we can extract the

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

### State-space model of the RLC circuit

We can rewrite equation (14) as:

\[\frac{d^{2}u_{C_{2}}}{dt^2} = – \frac{b}{a} \frac{du_{C_{2}}}{dt} – \frac{1}{a} u_{C_{2}} + \frac{1}{a} \cdot u_{IN} \tag{17}\]Now, we define two state variables *x _{1}* and

*x*as:

_{2}Applying time derivation to (18) gives:

\[\left\{\begin{matrix} \dot{x_{1}} &=& \dot{u_{C_{2}}} &=& x_{2} \tag{19}\\\dot{x_{2}} &=& \ddot{u_{C_{2}}} &=& – \frac{b}{a} x_{2} – \frac{1}{a} x_{1} + \frac{1}{a} u_{IN} \end{matrix}\right.\]We can write (19) in matrix multiplication form:

\[\begin{Bmatrix} \dot{x_{1}} \tag{20}\\ \dot{x_{2}} \end{Bmatrix}=\begin{bmatrix} 0 & 1\\-\frac{1}{a} & -\frac{b}{a} \end{bmatrix} \cdot \begin{Bmatrix} x_{1}\\x_{2} \end{Bmatrix} + \begin{bmatrix} 0\\\frac{1}{a} \end{bmatrix} \cdot \begin{Bmatrix} u_{IN} \end{Bmatrix}\]From (20) we can extract the *A* and *B* matrices of the state-space:

We also define the state variable *u _{C2}(t)* as being the output of the system:

We can write (21) in matrix multiplication form:

\[\begin{Bmatrix} y \end{Bmatrix}= \begin{bmatrix} 1 & 0 \end{bmatrix} \cdot \begin{Bmatrix} x_{1} \tag{22}\\ x_{2} \end{Bmatrix} + \begin{bmatrix} 0 \end{bmatrix} \cdot \begin{Bmatrix} u_{IN} \end{Bmatrix}\]From (22) we can extract the *C* and *D* matrices of the state-space:

### RRCC circuit differential equation – Scilab simulation

One way of solving the differential equation of the RRCC 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 = 0.5; // [ohm] R2 = 1.2; // [ohm] C1 = 0.02; // [F] C2 = 0.05; // [F] // simplification parameters a = R1*R2*C1*C2; b = R1*C1+R1*C2+R2*C2; // Define t [s] t0=0; tinc=0.001; tf=0.5; t=t0:tinc:tf; // Define differential equation function dx=f(t, x) dx(1)=x(2) dx(2)=(1/a)*(uIN-x(1)-b*x(2)) endfunction // Define initial conditions uC20 = 0; // [V] upC20 = 0; // [V/s] // Solve differential equation uC2=ode([uC20;upC20],t0,t,f);; // Plot numeric solution subplot(2,1,1) plot(t,uC2(1,:),'b'), xgrid() title("x-engineer.org","Color",'b') ylabel('$\large{u_{C2}(t) \text{ [V]}}$','fontsize',2) subplot(2,1,2) plot(t,uC2(2,:),'r'), xgrid() ylabel('$\large{\frac{du_{C2}(t)}{dt} \text{ [V/s]}}$','fontsize',2) xlabel('$\large{t}$','fontsize',2)

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

The plot represents the voltage across the capacitor *u _{C2}(t)* (blue) and the gradient of the voltage across the capacitor

*du*(red).

_{C2}/dt### RRCC 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 (RRCC 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 s=poly(0,'s'); Hs=[1/(a*s^2+b*s+1)]; sys=syslin('c',Hs) // Run step response uC2 = uIN*csim('step',t,sys); figure(2) plot(t,uC2), xgrid() hf=gcf(); hf.background = -2; title("x-engineer.org","Color",'b') ylabel('$\large{u_{C2}(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).

### RRCC 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 (RRCC 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 = [0 1;-1/a -b/a]; B = [0; 1/a]; C = [1 0]; D = [0]; X0 = [uC20; upC20]; sys = syslin('c',A,B,C,D,X0); // Run step response uC2 = uIN*csim('step',t,sys); figure(3) plot(t,uC2,'m'), xgrid() hf=gcf(); hf.background = -2; title("x-engineer.org","Color",'b') ylabel('$\large{u_{C2}(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).

### RRCC circuit differential equation – Xcos simulation

In order to integrate the differential equation of the RRCC circuit into Xcos, we are going to write equation (14) as:

\[\frac{d^{2}u_{C_{2}}}{dt^{2}}= \frac{1}{a} \left ( u_{IN} – u_{C_{2}} – b \frac{du_{C_{2}}}{dt} \right ) \tag{23}\]Equation (23) is modelled into Xcos using block diagram as:

Parameters `uIN`

, `a`

and `b`

are loaded into the Scilab workspace from the previous examples.

The simulation is run for `1 s`

and outputs the following plot.

The plot represents the voltage across the capacitor *u _{C2}(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 RRCC circuit using the Electrical library blocks from Xcos.

Parameters `uIN`

, `a`

and `b`

are loaded into the Scilab workspace from the previous examples.

Running the simulation will output the same time variation for *u _{C2}(t)*, which proves that the differential equation, transfer function and state-space model of the RRCC circuit are correct.

### RRCC circuit transfer function – Xcos simulation

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

Parameters `uIN`

, `a`

and `b`

are loaded into the Scilab workspace from the previous examples.

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

### RRCC circuit state-space – Xcos simulation

In this approach we are going to use the state-space model of the RRCC 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 capacitor *u _{C2}(t)*, which is the same as the one in the previous examples.