Scientific Calculations with Julia/Electrical Circuits


Example 1

Given the following linear circuit, we want to determine the values ​​of the currents :

Applying Kirchoff's laws, we obtain a linear system of three equations in three unknowns:

If the data are as follows: :

You can solve the system with Julia:

A=[-1 1 1;13 0 20;0 2 -20]
3×3 Matrix{Int64}:
-1  1    1
13  0   20
 0  2  -20
b=[0, 40, -20]
3-element Vector{Int64}:
0
40
-20
A\b
3-element Vector{Float64}:
1.4723926380368098
0.4294478527607364
1.0429447852760736

So we get:

Example 2

Given the following second-order linear circuit, we want to determine The values ​​of the voltages and currents :

Applying Kirchoff's laws yields a linear system of three equations in three unknowns:

If the data are as follows:

we obtain the system of differential equations:

with initial conditions:

Solving this system with Julia gives:

function ode_fn(du,u,p,t)
    x, y = u
    du[1] = -4 - x/5- y/5
    du[2] = x/5 - y/5
end
using DifferentialEquations
using Plots


t_begin=0.0
t_end=30
tspan = (t_begin,t_end)
x_init=10.0
y_init=10.0

prob = ODEProblem(ode_fn, [x_init, y_init], tspan)
num_sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
x_num_sol = [u[1] for u in num_sol.u]
y_num_sol = [u[2] for u in num_sol.u]

plot(num_sol.t, x_num_sol,
    title="Tensione su C e corrente su L",
    xaxis="t",
    linewidth=2,
    label="Tensione su C")
plot!(num_sol.t, y_num_sol,
    linewidth=2,
    label="Corrente su L")