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

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

- constant input voltage:
*u*_{IN}= 12 V - equivalent resistance:
*R*_{1}= 0.9 Ω - equivalent inductance
*L*_{1}= 0.115 H - equivalent capacitance
*C*_{1}= 0.02 F - simulation time
*t = 1 s* - initial time
*t*_{0}= 0 s - initial voltage across the capacitor
*u*_{C10}(t_{0}) = 0 V - initial voltage gradient across the capacitor
*du*_{C10}/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 RLC circuit

From Kirchhoff’s Current and Voltage Law (KCL and KVL) we can write the following equation:

\[u_{R_{1}} + u_{L_{1}} + u_{C_{1}} = u_{IN} \tag{1}\]The voltage across the resistor is:

\[u_{R_{1}} = R_{1} i_{R_{1}} \tag{2}\]The voltage across the inductor is:

\[u_{L_{1}} = L_{1} \frac{di_{L_{1}}}{dt} \tag{3}\]The current through the capacitor is:

\[i_{C_{1}} = C_{1} \frac{du_{C_{1}}}{dt} \tag{4}\]Also, the current is the same throughout the loop, which means:

\[i_{R_{1}} = i_{L_{1}} = i_{C_{1}} \tag{5}\]Replacing (5), (4), (3) and (2) in (1) gives the **governing differential equation of the RLC circuit**:

### Transfer function of the RLC circuit

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

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

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

\mathcal {L} \left [ u_{C_{1}}(t) \right ] &= U_{C_{1}}(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_{1}}(t_{0}) = 0\\

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

\end{split}\]

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

\[L_{1} C_{1} s^2 U_{C_{1}}(s) + R_{1} C_{1} s U_{C_{1}}(s) +U_{C_{1}}(s) = U_{IN}(s) \tag{7}\]From (7) and defining *U _{C1}(s)* as the output variable, we can extract the

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

### State-space model of the RLC circuit

We can rewrite equation (6) as:

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

*x*as:

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

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

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

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

We can write (13) in matrix multiplication form:

\[\begin{Bmatrix} y \end{Bmatrix}= \begin{bmatrix} 1 & 0 \end{bmatrix} \cdot \begin{Bmatrix} x_{1} \tag{14}\\ x_{2} \end{Bmatrix} + \begin{bmatrix} 0 \end{bmatrix} \cdot \begin{Bmatrix} u_{IN} \end{Bmatrix}\]From (14) 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}} \]### RLC circuit differential equation – Scilab simulation

One way of solving the differential equation of the RLC 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.9; // [ohm] C1 = 0.02; // [F] L1 = 0.115; // [H] // Define t [s] t0=0; tinc=0.001; tf=1; t=t0:tinc:tf; // Define differential equation function dx=f(t,x) dx(1)=x(2) dx(2)=(1/(L1*C1))*(uIN-R1*C1*x(2)-x(1)) endfunction // Define initial conditions uC10 = 0; // [V] upC10 = 0; // [V/s] // Solve differential equation uC1=ode([uC10;upC10],t0,t,f);; // Plot numeric solution subplot(2,1,1) plot(t,uC1(1,:),'b'), xgrid() title("x-engineer.org","Color",'b') ylabel('$\large{u_{C1}(t) \text{ [V]}}$','fontsize',2) subplot(2,1,2) plot(t,uC1(2,:),'r'), xgrid() ylabel('$\large{\frac{du_{C1}(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 _{C1}(t)* (blue) and the gradient of the voltage across the capacitor

*du*(red).

_{C1}/dt### RLC 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 (RLC 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/(L1*C1*s^2+R1*C1*s+1)]; sys=syslin('c',Hs) // 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)

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

### RLC 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 (RLC 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/(L1*C1) -R1/L1]; B = [0; 1/(L1*C1)]; C = [1 0]; D = [0]; X0 = [uC10; upC10]; sys = syslin('c',A,B,C,D,X0); // Run step response uC1 = uIN*csim('step',t,sys); figure(3) plot(t,uC1,'m'), 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)

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

### RLC circuit differential equation – Xcos simulation

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

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

Parameters `uIN`

, `R1`

, `L1`

and `C1`

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 _{C1}(t)*.

### RLC 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 RLC circuit using the Electrical library blocks from Xcos.

The parameters `uIN`

, `R1`

, `L1`

and `C1`

are defined in the Scilab workspace.

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

### RLC circuit transfer function – Xcos simulation

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

The parameters `uIN`

, `R1`

, `L1`

and `C1`

are defined in the Scilab workspace.

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

### RLC circuit state-space – Xcos simulation

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