using odeint & odeintw for complex equations when initial conditions are real

爱⌒轻易说出口 提交于 2019-12-11 12:16:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!