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
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 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.
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:
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.
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.
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.
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.
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.
GUEYE
Bonjour,
Dans le cadre de mon stage chez ASSYSTEM, je travaille avec votre logiciel Scilab/Xcos. Cependant, je rencontre quelques difficultés avec le compilateur C Mingw et le module SIMM qui ne fonctionnent pas du tout. Cela m’empêche de progresser dans mes travaux.
Je sollicite votre aide pour résoudre ce problème.
Merci d’avance.
Bien cordialement,
Hoang
Bonjour,
Je ne sais pas si votre travail à present a été fini et si vos difficultés ont été bien résolues. J’ai vu par hasard votre message sur cette page web et je vous ai écrit quand même. Au début, j’ai rencontré aussi des problèmes concernant le compilateur C et le module MinGW (Scilab n’a pas détecté le compilateur C et le module MinGW malgré qu’ils ont bien été installé sur mon ordinateur). Ma simulation dans Xcos n’a pas marché.
J’ai trouvé il y a quelques jour la page web https://atoms.scilab.org/toolboxes/mingw/10.3.0 et J’ai suivi ses instructions. Maintenant, ca marche très bien, aucun problème pour moi dans l’exécution des simulations avec Xcos.
J’espère qu’elle puisse aussi vous aider à surmonter vos difficultés.
Bonne chance,
Priyadarshan
Hi Team,
I was going through the “RRCC circuit modelling and simulation in Scilab/Xcos” and I came across an equation (eq. 8).
Can you explain me the substitution made in that equation as the line says “Replacing (6) in (4) and the result in (2)”, but such a substitution was not made and that the equation (8) differs from the mentioned equation relations as eq. (6) and (4) were considered.
Can you explain me on that part if my understanding was wrong.
Thanks and Regards
Priyadarshan
Priyadarshan
Hi team,
I was going through the RRCC circuit modelling and simulation in Scilab/Xcos post.
Can you clarify me on the equation (8) as I was not able to understand the substitution that was made using equation (6) and (4).
The line says “Replacing (6) in (4) and the result in (2)”, but such a replacement did not seem to be a possible one as far as I have understood.
Looking forward to hear from you to clarify my doubt on this matter.
Thanks and Regards
Priyadarshan