问题
scipy.integrate.odeint
does not handle complex values. Warren Weckesser has created a wrapper odeintw which converts a complex system into real systems.
In line 176 of _odeintw.py, it checks to see whether there really are complex numbers, by checking if the initial condition is complex. But there are other ways that the equations might end up being complex.
This has caused some bugs for me, and so I have changed line 176 to simply be if False:
so that it uses the complex version.
So - is this a bug or a feature? In other words, am I potentially breaking something by doing this?
Here is some sample code:
from odeintw import odeintw
import numpy
import matplotlib.pyplot as plt
#Solve \dot{y} = cy$ with $y(0)=1$ and c=\sqrt(-1)
def dy_dt(Y, t, c):
y_val = Y[0]
return [y_val*c]
y0 = [1]
c=1j
times = numpy.linspace(0,10,21)
y = odeintw(dy_dt, y0, times, args = (c,))
plt.plot(numpy.real(y), numpy.imag(y))
plt.savefig('odeintw_error.pdf')
This fails for odeintw
, but works with the change I have suggested.
来源:https://stackoverflow.com/questions/47295511/using-odeint-odeintw-for-complex-equations-when-initial-conditions-are-real