有没有一种方法可以轻松地在一组完整的点网格上积分一组微分方程?

时间:2019-05-18 11:22:58

标签: python numpy scipy differential-equations

问题是我希望能够一次对网格的每个点开始积分微分方程,而不必在每个坐标上遍历scipy积分器。 (我敢肯定有一个简单的方法)

作为代码的背景,我试图解决在每个特定周期交替改变速度方向的Couette通量的轨迹,这是一个众所周知的产生混乱的动力学系统。我认为剩下的代码对于与scipy集成以及我对meshgrid numpy函数的用法并不重要。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, writers
from scipy.integrate import solve_ivp

start_T = 100
L = 1
V = 1
total_run_time = 10*3
grid_points = 10

T_list = np.arange(start_T, 1, -1)
x = np.linspace(0, L, grid_points)
y = np.linspace(0, L, grid_points)
X, Y = np.meshgrid(x, y)
condition = True
totals = np.zeros((start_T, total_run_time, 2))
alphas = np.zeros(start_T)
i = 0

for T in T_list:
    alphas[i] = L / (V * T)
    solution = np.array([X, Y])
    for steps in range(int(total_run_time/T)):
        t = steps*T
        if condition:
            def eq(t, x):
                return V * np.sin(2 * np.pi * x[1] / L), 0.0
            condition = False
        else:
            def eq(t, x):
                return 0.0, V * np.sin(2 * np.pi * x[1] / L)
            condition = True
        time_steps = np.arange(t, t + T)
        xt = solve_ivp(eq, time_steps, solution)
        solution = np.array([xt.y[0], xt.y[1]])
        totals[i][t: t + T][0] = solution[0]
        totals[i][t: t + T][1] = solution[1]
    i += 1

np.save('alphas.npy', alphas)
np.save('totals.npy', totals)

给出的错误是:

ValueError: y0 must be 1-dimensional.

它来自scipy的'solve_ivp'函数,因为它不接受numpy函数meshgrid的格式。我知道我可以运行一些循环并克服它,但是我假设必须有一种使用numpyscipy的“好”方法。我也接受其余代码的建议。

0 个答案:

没有答案