#
RLC Circuit Using Equations
#
Basic System Diagram
Let's start modeling a simple physical system. We'll start with an equation oriented approach and in the next section switch to a component oriented approach.
This equation oriented approach is the same one used here and is based on the following system topology:
The equations for this system are:
\begin{align}
V\left( t \right) =& R i_{R}\left( t \right) \\
C \frac{\mathrm{d} V\left( t \right)}{\mathrm{d}t} =& i_{C}\left( t \right) \\
L \frac{\mathrm{d} i_{L}\left( t \right)}{\mathrm{d}t} =& Vb - V\left( t \right) \\
i_{L}\left( t \right) =& i_{C}\left( t \right) + i_{R}\left( t \right)
\end{align}
#
Modelica Version
Out goal is to build an MTK version of the following Modelica model (presented here for comparison):
model RLC1 "A resistor-inductor-capacitor circuit model"
type Voltage=Real(unit="V");
type Current=Real(unit="A");
type Resistance=Real(unit="Ohm");
type Capacitance=Real(unit="F");
type Inductance=Real(unit="H");
parameter Voltage Vb=24 "Battery voltage";
parameter Inductance L = 1;
parameter Resistance R = 100;
parameter Capacitance C = 1e-3;
Voltage V;
Current i_L;
Current i_R;
Current i_C;
equation
V = i_R*R;
C*der(V) = i_C;
L*der(i_L) = (Vb-V);
i_L=i_R+i_C;
end RLC1;
#
ModelingToolkit Version
As usual, we start by defining the variables we are interested in:
using ModelingToolkit
using DifferentialEquations
using Plots
@variables t V(t) i_L(t) i_R(t) i_C(t)
@parameters Vb=24 L=1 R=100 C=1e-3
D = Differential(t)
eqs = [
V ~ i_R*R
C*D(V) ~ i_C
L*D(i_L) ~ Vb-V
i_L ~ i_R+i_C
]
@named sys = ODESystem(eqs, t)
prob = ODEProblem(structural_simplify(sys), [V => 0.0, i_L => 0.0], (0, 1), [], jac = true)
sol = solve(prob)
display(plot(sol, idxs=[V, i_L]))
#
Results
Running the model in the previous section gives us the following results:
This corresponds to the voltage source having a discontinuity at the start of the simulation (jumping from 0 voltes to 24 volts).
#
Rendering Equations
This section demonstrates how to work with the intermediate representations of our systems. The fact that we are rendering equations, while useful, isn't the important part. The important part is that we can operate on our system description in a variety of ways (this is just one).
If you want to see the Latex rendered versions of equations, you can use the
latexify function from the Latexify package. In order to use the Latexify
package, you'll need to add it as a dependency.
If you run the code above in a Julia REPL, you can simply run this after the code shown:
using Latexify
latexify(eqs)
...and you'll get:
L"\begin{align}
V\left( t \right) =& R i_{R}\left( t \right) \\
C \frac{\mathrm{d} V\left( t \right)}{\mathrm{d}t} =& i_{C}\left( t \right) \\
L \frac{\mathrm{d} i_{L}\left( t \right)}{\mathrm{d}t} =& Vb - V\left( t \right) \\
i_{L}\left( t \right) =& i_{C}\left( t \right) + i_{R}\left( t \right)
\end{align}
"
If you don't want to see the quoted version, you can "pipe" the output to the
print function, i.e.,
latexify(eqs) |> print
...which gives:
\begin{align}
V\left( t \right) =& R i_{R}\left( t \right) \\
C \frac{\mathrm{d} V\left( t \right)}{\mathrm{d}t} =& i_{C}\left( t \right) \\
L \frac{\mathrm{d} i_{L}\left( t \right)}{\mathrm{d}t} =& Vb - V\left( t \right) \\
i_{L}\left( t \right) =& i_{C}\left( t \right) + i_{R}\left( t \right)
\end{align}