I am working on a project for electrical engineering where we plot the voltage over a capacitor in a RLC circuit as a function of time. I based my code on the following tutorial: Python-ODE for RLC Circuit. My code is fully functional but I just want a better understanding of how the user defined function and the SciPy integrating function works. I have read the documentation but I still have some specific questions. Here is my code:
from scipy.integrate import odeint # for integrating ode
import matplotlib.pyplot as plt # for plotting commands
import numpy as np #For loadtxt and linspace modules
R,L,C,tl,numplot = np.loadtxt('UserInput_RLC_Values.txt',unpack=True)
V = 2.0 #voltageSource
time = np.linspace(0.0,tl,numplot)
vc,m = odeint(RLC,[0.0,0.0],time).T
The ODE function passed to
odeint needs to have the format
f(state, time) where
time is the independent variable and
state the state vector. The corresponding (vector-valued) differential equation is
) For the standard ODE solvers you need to convert higher-order equations into systems of first order. Here using
m as momentum (?) variable,
m = dVc/dt. Then
dm/dt = d²Vc/dt² is the second derivative.
If the input is
state = [ Vc, m ] the output needs to be
[ dVc/dt, dm/dt ] = [ m, Eqn(t,Vc,m) ] where
d²Vc/dt² = Eqn(t,Vc,dVc/dt) is a form of the original equation.
t just needs to be present in the argument list, even if the ODE is time-invariant.
) You are passing the function as reference/address, not a function value
) It means "transpose". The result of
odeint is a list of state vectors, which are pairs of numbers. Transposing it transforms it into a pair of lists that can be assigned to a pair of variables.