我写了一个代码来计算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的问题吗?请帮帮忙先谢谢你.

推荐答案

当你写:

def Lorenz(t, x):
    xdot = [s*(x[1]-x[0]), x[0]*(r-x[2])-x[1], x[0]*x[1]-b*x[2]]
    A = np.random.randn(3, 3)
    q, r = qr(A)  # Override the r global variable
    return xdot

您覆盖了系统用来计算其数量的r全局变量. 换个名字:

def Lorenz(t, x):
    xdot = [s*(x[1]-x[0]), x[0]*(r-x[2])-x[1], x[0]*x[1]-b*x[2]]
    A = np.random.randn(3, 3)
    Q, R = qr(A)  # Don't conflict anymore
    return xdot

更好的是,完全不要使用全局变量:

def Lorenz(t, x, s, r, b):
    xdot = [s*(x[1]-x[0]), x[0]*(r-x[2])-x[1], x[0]*x[1]-b*x[2]]
    A = np.random.randn(3, 3)
    Q, R = qr(A)
    return xdot

并将其传递给求解器:

sol = solve_ivp(
    Lorenz, [0, T], y0, dense_output=True, args=(s, r, b), 
    atol=1e-9, rtol=1e-6
)

这将解决你的问题.

Python-3.x相关问答推荐

像计数不显示在html和想知道如果我的模型设置正确

如何在matplotlib中显示次要刻度标签

根据另一列中的条件填写该列中的值

为什么我无法在django中按月筛选事件?

如何在 python 中将带有时区信息的时间戳转换为 utc 时间

ValueError at /register/ 视图authenticate.views.register_user 未返回HttpResponse 对象.它返回 None 相反

Python,Web 从交互式图表中抓取数据

Python 3 - 给定未知数量的类别动态地将字典嵌套到列表中

如何通过从特定列创建分组多标题来reshape 数据框?

避免重复连续字符但不包括一个特定字符的正则表达式

通过附加/包含多个列表来创建 nDimensional 列表

使用 pandas 进行多类分类的总体准确度

使用 distutils 分发预编译的 python 扩展模块

理解 Keras 的 ImageDataGenerator 类中的 `width_shift_range` 和 `height_shift_range` 参数

使用自定义比较删除重复项

Python中的依赖倒置

pip install dryscrape 失败并显示错误:[Errno 2] 没有这样的文件或目录:'src/webkit_server'?

如何在元素列表中找到最大的数字,可能是非唯一的?

aiohttp+sqlalchemy:在回滚无效事务之前无法重新连接

带有 Emacs 的 Python 3