用离散值求解常微分方程的最优方法

2024-10-03 11:21:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解一个常微分方程,比如y’(x)=f1(x)*y(x)+f2(x)*y(x)^3。问题是我不知道f1(x)和f2(x)的解析表达式是什么,我只知道常微分方程域上的离散值

每次我都要解这个常微分方程,因为它进入了偏微分方程组,所以每次t,f1(x)和f2(x)的值都不同

我编写了以下代码来解决这个问题(代码是理想化的,因为函数f1和f2不是解析的)。问题是执行完整循环需要很多时间

from scipy.integrate import odeint
from scipy.interpolate import interp1d
import numpy as np
import time

start_time = time.time()
xs = np.linspace(0,10,100+1);


def dy_dx(y, x):
    return (y)*f1i(x)+(y**3)*f2i(x)


for t in range(100):
    f1 = xs+(0.01*t)
    f2 = np.sin(xs)*(xs+0.01*t)**2
    f1i = interp1d(xs, f1, kind="cubic", fill_value="extrapolate")
    f2i = interp1d(xs, f2, kind="cubic", fill_value="extrapolate")
    ys = odeint(dy_dx, 1.0, xs)[:, 0]

print("--- %s seconds ---"% (time.time()-start_time)) 

我想知道是否还有其他更为理想的方法。我看过这篇文章。但我不知道该如何应用于我的问题,以及它是否有用

此外,我还收到一条错误消息:ODEintWarning:此调用完成了过多的工作(可能是错误的Dfun类型)。以full_output=1运行以获取定量信息可证明是相关的,因为我正在做一个循环,但看起来很奇怪

你有什么想法吗?谢谢


Tags: 代码fromimporttimenpscipystartf2