# Mathematical models and simulation of electrical systems

### 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: uIN = 12 V
• equivalent resistance:  R1 = 0.5 Ω
• equivalent resistance:  R2 = 1.2 Ω
• equivalent capacitance C1 = 0.02 F
• equivalent capacitance C2 = 0.05 F
• simulation time t = 0.5 s
• initial time t0 = 0 s
• initial voltage across the 2nd capacitor uC20(t0) = 0 V
• initial voltage gradient across the 2nd capacitor duC20/dt0 = 0 V/s

Image: RRCC circuit schematic

• 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 iL1(t) will be iL1 and uR1(t) will be uR1. Also, bear in mind that lower case parameters are variable in time (e.g. u(t), i(t)), while upper case parameters are constant in time (e.g. R1, L1, C1).

### 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 uIN-R1-C1

$u_{IN}=u_{R_{1}}+u_{C_{1}} \tag{1}$

Loop C1-R2-C2

$u_{C_{1}}=u_{C_{2}}+u_{R_{2}} \tag{2}$

The voltage across the resistor R1 is:

$u_{R_{1}}=R_{1} i_{R_{1}} \tag{3}$

The voltage across the resistor R2 is:

$u_{R_{2}}=R_{2} i_{R_{2}} \tag{4}$

The current through the capacitor C1 is:

$i_{C_{1}}= C_{1} \frac{du_{C_{1}}}{dt} \tag{5}$

The current through the capacitor C2 is:

$i_{C_{2}}= C_{2} \frac{du_{C_{2}}}{dt} \tag{6}$

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:

$\bbox[#FFFF9D]{R_{1} C_{1} R_{2} C_{2} \frac{d^2u_{C_{2}}}{dt^2} + \left (R_{1} C_{1} + R_{1} C_{2} + R_{2} C_{2} \right ) \frac{du_{C_{2}}}{dt}+u_{C_{2}}=u_{IN}} \tag{13}$

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 uC2(t) as the output variable, we can extract the transfer function of the RRCC circuit as:

$\bbox[#FFFF9D]{H(s) = \frac{U_{C_{2}}(s)}{U_{IN}(s)}=\frac{1}{a s^2 + b s + 1}} \tag{16}$

### 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 x1 and x2 as:

$\left\{\begin{matrix} x_1 = u_{C_{2}} \tag{18}\\ x_2 = \dot{u_{C_{2}}} \end{matrix}\right.$

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:

$\bbox[#FFFF9D]{A =\begin{bmatrix} 0 & 1\\ -\frac{1}{a} & -\frac{b}{a} \end{bmatrix}, \quad B =\begin{bmatrix} 0\\ \frac{1}{a} \end{bmatrix}}$

We also define the state variable uC2(t) as being the output of the system:

$y(t) = u_{C_{2}} \tag{21}$

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:

$\bbox[#FFFF9D]{C = \begin{bmatrix} 1 & 0 \end{bmatrix}, \quad D = \begin{bmatrix} 0 \end{bmatrix}}$

### 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.

Image: RRCC circuit – plot – Scilab

The plot represents the voltage across the capacitor uC2(t) (blue) and the gradient of the voltage across the capacitor duC2/dt (red).

### 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 uIN 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 uIN 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:

Image: RRCC circuit – differential equation – Xcos

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.

Image: RRCC circuit – plot – Xcos

The plot represents the voltage across the capacitor uC2(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.

Image: RRCC circuit – electrical components – 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 uC2(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.

Image: RRCC circuit – transfer function – 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 uC2(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.

Image: RRCC circuit – state-space – 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 uC2(t), which is the same as the one in the previous examples.