# Mathematical models and simulation of electrical systems

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

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

• constant input voltage: uIN = 12 V
• equivalent resistance:  R1 = 3 Ω
• equivalent inductance L1 = 0.115 H
• simulation time t = 1 s
• initial time t0 = 0 s
• initial current through the inductor iL10(t0) = 0 A

• 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 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:

$\bbox[#FFFF9D]{R_{1} i(t)+L_{1} \frac{di}{dt}=u_{IN}(t)} \tag{5}$

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

$\bbox[#FFFF9D]{H(s) = \frac{I(s)}{U_{IN}(s)}=\frac{1}{L_{1} s + R_{1}}} \tag{8}$

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

$\bbox[#FFFF9D]{\begin{split} 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:

$y(t) = i(t) \tag{10}$

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

$\bbox[#FFFF9D]{\begin{split} C &= \\ D &=  \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 iL1(t), which is equal with the current through the resistor iR1(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 uIN 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 uIN and time t.

// Define state-space model
A = [-R1/L1];
B = [1/L1];
C = ; // replace with [R1] for uR output
D = ;
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 uL1(t), the lower plot (red) represents the current through the inductor iL1(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 uL1(t) and iL1(t), which proves that the differential equation, transfer function and state-space model of the RL circuit are correct.

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