# Mathematical models and simulation of electrical systems

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

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

• constant input voltage: uIN = 12 V
• equivalent resistance:  R1 = 3 Ω
• equivalent capacitance C1 = 0.05 F
• simulation time t = 1 s
• initial time t0 = 0 s
• initial voltage across the capacitor uC10(t0) = 0 V

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

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

$u_{R_{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 current through the capacitor is:

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

Also, the current is the same throughout the loop, which means:

$i_{R_{1}} = i_{C_{1}} \tag{3.1}$

Replacing (2), (3) and (3.1) in (1) gives the governing differential equation of the RC circuit:

$\bbox[#FFFF9D]{R_{1} C_{1} \frac{du_{C_{1}}}{dt}+u_{C_{1}} = u_{IN}} \tag{4}$

### Transfer function of the RC circuit

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

$\begin{split} \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 condition for the voltage at the initial time is:

$U_{C_{1}}(t_{0})=0 \tag{5}$

Replacing the Laplace transform and (5) in (4) gives:

$R_{1} C_{1} s U_{C_{1}}(s) + U_{C_{1}}(s) = U_{IN}(s) \tag{6}$

From (6) and defining UC1(s) as the output variable, we can extract the transfer function of the RC circuit as:

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

### State-space model of the RC circuit

We can rewrite equation (4) as:

$\frac{du_{C_{1}}}{dt} = – \frac{1}{R_{1} C_{1}} u_{C_{1}} + \frac{1}{R_{1} C_{1}} u_{IN} \tag{8}$

From (8), having the voltage uC1(t) as the state variable, we can extract the A and B matrices of the state-space:

$\bbox[#FFFF9D]{\begin{split} A &= \left [- \frac{1}{R_{1} C_{1}} \right ] \\ B &= \left [\frac{1}{R_{1} C_{1}} \right ] \end{split}}$

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

$y(t) = u_{C_{1}} \tag{9}$

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

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

### RC circuit differential equation – Scilab simulation

One way of solving the differential equation of the RC 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]
C1 = 0.05; // [F]
// Define t [s]
t0=0; tinc=0.001; tf=1; t=t0:tinc:tf;

// Define differential equation
function dx=f(t,x)
dx(1)=(uIN-x(1))/(R1*C1)
endfunction
// Define initial conditions
uC10 = 0; // [V]
// Solve differential equation
uC1=ode(uC10,t0,t,f);
// Plot numeric solution
plot(t,uC1,'r'), xgrid
title("x-engineer.org","Color",'b')
ylabel('$\large{u_{C1}(t) \text{ [V]}}$','fontsize',2)
xlabel('$\large{t} \text{ [s]}$','fontsize',2)


After running the Scilab instructions above, we get the following plot.

Image: RC circuit – plot – Scilab

The plot represents the voltage across the capacitor uC1(t).

### RC 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 (RC 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/(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).

### RC 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 (RC 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)];
B = [1/(R1*C1)];
C = [1];
D = [0];
X0 = [uC10];
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).

### RC circuit differential equation – Xcos simulation

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

$\frac{du_{C_{1}}}{dt}= \frac{1}{R_{1} C_{1}} \left ( u_{IN} – u_{C_{1}} \right ) \tag{10}$

Equation (10) is modelled into Xcos using block diagram as:

Image: RC circuit – differential equation – Xcos

Parameters uIN, R1 and C1 are loaded into the Scilab workspace from the previous examples.

The simulation is run for 1 s and outputs the following plot.

Image: RC circuit – plot – Xcos

The plot represents the voltage across the capacitor uC1(t).

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

Image: RC circuit – electrical components – Xcos

The parameters uIN, R1 and C1 are defined in the Scilab workspace.

Running the simulation will output the same time variation for uC1(t), which proves that the differential equation, transfer function and state-space model of the RC circuit are correct.

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

Image: RC circuit – transfer function – Xcos

The parameters uIN, R1 and C1 are defined in the Scilab workspace.

Running the simulation will output the variation of the voltage across the capacitor uC1(t), which is the same as the one in the previous examples.

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

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