Griggs Hutaff - 3 months ago 26

Python Question

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)

def RLC(A,t):

Vc,m=A

V = 2.0 #voltageSource

res=[m,(V-Vc-(m*R*C))/(L*C)]

return res

time = np.linspace(0.0,tl,numplot)

vc,m = odeint(RLC,[0.0,0.0],time).T

Here are my questions:

- What role are the parameter values A and t playing in the function RLC? Why is A assigned to both Vc (capacitor voltage) and m (dummy variable for v')? Is t even used?
- When I call RLC function inside the odeint module why aren't there any parameters?
- What does the .T at the end of the odeint module mean? I can't find anything about it in documentation.

Thanks for any insight on this. I hope this isn't a lazy question but I think fully understanding this will help me a great deal.

Answer

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 `y'(t)=f(y(t),t)`

) 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.