42 lines
747 B
Julia
42 lines
747 B
Julia
using ModelingToolkit, OrdinaryDiffEq
|
|
using GLMakie
|
|
|
|
@parameters R
|
|
@variables t v(t) i(t)
|
|
D = Differential(t)
|
|
|
|
eqs = [
|
|
i ~ v/R
|
|
D(v) ~ 0
|
|
]
|
|
|
|
@named circuit = ODESystem(eqs, t)
|
|
|
|
function solve_circuit(circuit)
|
|
u0 = [
|
|
v => 5.0,
|
|
]
|
|
|
|
p = [
|
|
R => 10,
|
|
]
|
|
|
|
tspan = (0.0, 0.01)
|
|
# convert the DAE problem to an ODE problem
|
|
circuit = structural_simplify(circuit)
|
|
prob = ODEProblem(circuit, u0, tspan, p)
|
|
sol = solve(prob, Tsit5())
|
|
|
|
# Interpolation can give the solution inside tspan and also further
|
|
#sol(0.009)
|
|
return sol
|
|
end
|
|
|
|
|
|
function show_sol(circuit)
|
|
sol = solve_circuit(circuit)
|
|
lines(sol.t, sol[i], color = :red)
|
|
lines!(sol.t, sol[v], color = :blue)
|
|
current_figure()
|
|
end
|