solve_ivp 包含平方函数的微分方程

问题描述 投票:0回答:1

我试图用 python 来求解 (x’(t))^2+ax^2(t)-b=0 。我知道我可以通过定义如下函数来使用 scipy 的solve_ivp 来求解更简单的方程:

def f(t, y):
   return y ** 2

代表方程 f’(t)=t^2。我怎样才能写出这样的第一个方程?我知道这个方程可能可以通过替换(也许 y=x^2 或其他)来简化,但我只是有兴趣在这种情况下直接(并以数值方式)求解它。

我尝试天真地求解 x'(t) 的第一个方程,这意味着 python 中 f 的定义包含 math.sqrt()。这给了我一个域错误(在某些阶段输入可能是负数)。这个错误发生在 an 和 b 的许多不同值上,所以我不知道如何继续。

python scipy differential-equations
1个回答
0
投票

请根据建议更新问题,但在我看来,您可以遵循求平方根的方法,但要确保值一开始就很复杂,以避免域错误。

import numpy as np
from scipy.integrate import solve_ivp

a = 2 + 0j
b = 1 + 0j

def f(t, x):
    return np.sqrt(b - a*x**2)
    # return -np.sqrt(b - a*x**2)

t0, tf = 0, 10
y0 = [1 + 0j]
res = solve_ivp(f, (t0, tf), y0)
# solves successfully
© www.soinside.com 2019 - 2024. All rights reserved.