Add simple resistor example

This commit is contained in:
Enrico Lumetti 2021-08-13 01:15:53 +02:00
parent f7f66d5fa3
commit 9ce0814750
3 changed files with 1518 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,4 @@
[deps]
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"

View File

@ -0,0 +1,41 @@
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