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

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

- constant input voltage:
*u*_{IN}= 12 V - equivalent resistance:
*R*_{1}= 3 Ω - equivalent inductance
*L*_{1}= 0.115 H - simulation time
*t = 1 s* - initial time
*t*_{0}= 0 s - initial current through the inductor
*i*_{L10}(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 RL circuit

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

\[u_{R_{1}} + u_{L_{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}\]Also, we know that the current through the resistor is equal with the current through the inductor, since they are part from the same loop:

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

### Transfer function of the RL circuit

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

\[ \begin{split}\mathcal {L} \left [ L_{1} \frac{di}{dt} \right ] &= L_{1} s I(s) – I(t_{0})\\

\mathcal {L} \left [ R_{1} i(t) \right ] &= R_{1} \cdot I(s)\\

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

\end{split} \]

From the initial data we also know the initial condition for the current at the initial time is:

\[I(t_{0})=0 \tag{6}\]Replacing the Laplace transforms and initial conditions in (5) gives:

\[L_{1} s I(s) + R_{1} I(s) = U_{IN}(s) \tag{7}\]From (10) and defining *I(s)* as the output variable, we can extract the **transfer function of the RL circuit** as:

### State-space model of the RL circuit

We can rewrite equation (5) as:

\[\frac{di}{dt} = – \frac{R_{1}}{L_{1}} i(t) + \frac{1}{L_{1}} u_{IN} \tag{9}\]From (9), having the current *i(t)* as the state variable, we can extract the *A* and *B* matrices of the state-space:

A &= \left [ – \frac{R_{1}}{L_{1}} \right ] \\

B &= \left [ \frac{1}{L_{1}} \right ]

\end{split}} \]

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

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

C &= [1]\\

D &= [0]

\end{split}} \]

### RL circuit differential equation – Scilab simulation

One way of solving the differential equation of the RL 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 = 3; // [ohm] 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)=(1/L1)*(uIN-R1*x(1)); endfunction // Define initial conditions iL10 = 0; // [A] uR10 = 0; // [V] (use for uR as output) // Solve differential equation iL1=ode(iL10,t0,t,f); // Plot numeric solution plot(t,iL1,'b'), xgrid title("x-engineer.org","Color",'b') ylabel('$\large{i_{L1}(t) \text{ [A]}}$','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)*, which is equal with the current through the resistor

*i*.

_{R1}(t)### RL 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 (RL 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*s+R1)]; sys=syslin('c',Hs) // Run step response iL1 = uIN*csim('step',t,sys); figure(2) plot(t,iL1,'r'), 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).

### RL 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 (RL 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 = [-R1/L1]; B = [1/L1]; C = [1]; // replace with [R1] for uR output D = [0]; X0 = [iL10]; // replace with [uR10] for uR output sys = syslin('c',A,B,C,D,X0); // Run step response iL = uIN*csim('step',t,sys); figure(3) plot(t,iL1,'m'), 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).

### RL circuit differential equation – Xcos simulation

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

\[\frac{di}{dt}= \frac{1}{L_{1}} \left ( u_{IN}(t) – R_{1} i(t) \right ) \tag{11}\]Equation (11) is modelled into Xcos using block diagram as:

Parameters `uIN`

, `R1`

and `L1`

are loaded into the Scilab workspace from the previous examples.

The simulation is run for `1 s`

and outputs the following plot.

The upper plot (blue) represents the voltage across the inductor *u _{L1}(t)*, the lower plot (red) represents the current through the inductor

*i*.

_{L1}(t)### RL 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 RL circuit using the Electrical library blocks from Xcos.

The parameters `uIN`

, `R1`

and `L1`

are defined in the Scilab workspace.

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

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

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

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

The parameters `uIN`

, `R1`

and `L1`

are defined in the Scilab workspace.

Running the simulation will output the variation of the current through the inductor *i _{L1}(t)*, which is the same as the one in the previous examples.

### RL circuit state-space – Xcos simulation

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