# Mathematical models and simulation of electrical systems

### 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: uIN = 12 V
• equivalent resistance:  R1 = 1.5 Ω
• equivalent resistance:  R2 = 2.5 Ω
• equivalent capacitance C1 = 2 F
• equivalent inductance L1 = 3 H
• simulation time t = 10 s
• initial time t0 = 0 s
• initial voltage across the capacitor uC1(t0) = 0 V
• initial voltage across the capacitor iL1(t0) = 0 A

Image: RRLC 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 RRLC circuit

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

Loop uIN-C1-R1

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

Loop R1-L1-R2

$u_{R_{1}}=u_{L_{1}}+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 voltage across the inductor L1 is:

$u_{L_{1}}= L_{1} \frac{di_{L_{1}}}{dt} \tag{6}$

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:

$\bbox[#FFFF9D]{\left\{\begin{matrix} \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 UC1(s) as the output of the system, gives the transfer function of the RRLC circuit as:

$\bbox[#FFFF9D]{H_{1}(s) = \frac{U_{C_{1}}(s)}{U_{IN}(s)} = \frac{L_{1}s + R_{2} + R_{1}}{R_{1}C_{1}L_{1}s^{2} + \left ( R_{1}R_{2}C_{1} + L_{1} \right ) s + R_{2} + R_{1}}} \tag{16}$

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 IL1(s) as the output of the system, gives the transfer function of the RRLC circuit as:

$\bbox[#FFFF9D]{H_{2}(s) = \frac{I_{L_{1}}(s)}{U_{IN}(s)} = \frac{R_{1}C_{1}s}{L_{1}R_{1}C_{1}s^{2} + \left ( L_{1} + R_{1}R_{2}C_{1} \right )s + R_{1} +R_{2}} }\tag{18}$

### State-space model of the RRLC circuit

After defining uC1(t) and iL1(t) as the states of the system, we can write (12) in matrix multiplication form:

$\begin{Bmatrix} \dot{u_{C_{1}}} \tag{19}\\ \dot{i_{L_{1}}} \end{Bmatrix}=\begin{bmatrix} – \frac{1}{R_{1}C_{1}} & \frac{1}{C_{1}}\\-\frac{1}{L_{1}} & -\frac{R_{2}}{L_{1}} \end{bmatrix} \cdot \begin{Bmatrix} u_{C_{1}}\\i_{L_{1}} \end{Bmatrix} + \begin{bmatrix} \frac{1}{R_{1}C_{1}}\\ \frac{1}{L_{1}} \end{bmatrix} \cdot \begin{Bmatrix} u_{IN} \end{Bmatrix}$

From (19) we can extract the A and B matrices of the state-space:

$\bbox[#FFFF9D]{A =\begin{bmatrix} – \frac{1}{R_{1}C_{1}} & \frac{1}{C_{1}}\\ -\frac{1}{L_{1}} & -\frac{R_{2}}{L_{1}} \end{bmatrix}, \quad B =\begin{bmatrix} \frac{1}{R_{1}C_{1}}\\ \frac{1}{L_{1}} \end{bmatrix}}$

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:

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

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.

Image: RRLC circuit – plot – Scilab

The plot represents the current through the inductor iL1(t) (blue), the voltage across the capacitor uC1(t) (red) and the voltage across the resistor uR2(t) (magenta).

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

Image: RRLC circuit – differential equation – Xcos

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.

Image: RRLC circuit – plot – Xcos (1)

The plot represents the current through the inductor iL1(t) (blue) and the voltage across the capacitor uC1(t) (red).

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

Image: RRLC circuit – electrical components – 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 uC1(t) and iL1(t), which proves that the differential equation, transfer function and state-space model of the RRLC circuit are correct.

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

Image: RRLC circuit – transfer function – Xcos (1)

Image: RRLC circuit – transfer function – Xcos (2)

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 uC1(t) and iL1(t), which is the same as the one in the previous examples.

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

Image: RRLC 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 resistor uR2(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.