我写了一个代码来计算Lorenz系统的李雅普诺夫指数使用qr分解.我收到了这个有线错误消息,我无法理解.下面的代码运行没有任何错误.
import numpy as np
from scipy.integrate import solve_ivp
from numpy.linalg import qr, norm
def Lorenz(t, x):
xdot = [s*(x[1]-x[0]), x[0]*(r-x[2])-x[1], x[0]*x[1]-b*x[2]]
return xdot
s=10; r=28; b = 8/3
y0 = np.random.randn(3)
T = 100
sol = solve_ivp(Lorenz, [0, T], y0, dense_output = True,
atol = 1e-9, rtol = 1e-6)
在最后solve_ivp
步之前,我需要对另一个numpy数组执行qr分解.所以我在最后一步之前插入了以下几行.
A = np.random.randn(3, 3)
q, r = qr(A)
上面的代码块没有直接引用到solve_ivp
步中使用的任何变量或函数,但我仍然收到错误消息.
runfile('E:/Codes/ChuaDiode/untitled0.py', wdir='E:/Codes/ChuaDiode')
Traceback (most recent call last):
File "E:\Codes\ChuaDiode\untitled0.py", line 23, in <module>
sol = solve_ivp(Lorenz, [0, T], y0, dense_output = True,
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\ivp.py", line 542, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\rk.py", line 94, in __init__
self.f = self.fun(self.t, self.y)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\base.py", line 138, in fun
return self.fun_single(t, y)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\base.py", line 20, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py", line 102, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.
我已经try 了同样的代码为一个2维Duffing系统运行顺利.
def Duffing(t, x):
xdot = [x[1], x[0] - x[0]**3-c*x[1] + F*np.sin(w*t)]
return xdot
F = 1.3; w = 0.6; c = 0.2
这是我安装Python的问题吗?请帮帮忙先谢谢你.