如何使用函数及其导数(' s)在Python中求解另一个微分方程?

发布于 2025-01-24 06:19:18 字数 888 浏览 0 评论 0原文

我需要在Python中同时解决这两个方程式: “ B”是恒定的;但是我不知道如何将函数“ P”及其导数放在第二个方程式中, 有人可以帮助我吗?

版: 我已经写了此代码来解决这个问题,但我不知道它是正确的。此代码是否正确给出了这些方程式的答案?

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import scipy.integrate as sy
import sys
import numpy as np
np.set_printoptions(threshold=sys.maxsize)
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
b=2
def eq(t,u):
    #u[0] is p and u[1] is p', u[2]=y, u[3]=y'.
    u[1]=u[0]*np.sqrt(0.25*(u[0]**-4)*(u[0]+(1/7)+0.7)
    return [u[0],u[1],u[3],-3*(u[1]/(b*u[0]))*u[3] - u[2]]
u0 = [0.00001,0.00001,1.22*(10**28),0]
t = np.linspace(0.0000000001,0.6924*10**33,10000)
sol=solve_ivp(eq,t,u0,None)

I need to solve this two equations simultaneously in python :
enter image description here
"b" is constant; but I don't know how to put the function "P" and its derivative at same time in the second equations code,
is there anyone could help me?

edition:
I have wrote this code to solve the question, but I don't know it is correct or not. is this code correctly gives the answer of those equations?

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import scipy.integrate as sy
import sys
import numpy as np
np.set_printoptions(threshold=sys.maxsize)
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
b=2
def eq(t,u):
    #u[0] is p and u[1] is p', u[2]=y, u[3]=y'.
    u[1]=u[0]*np.sqrt(0.25*(u[0]**-4)*(u[0]+(1/7)+0.7)
    return [u[0],u[1],u[3],-3*(u[1]/(b*u[0]))*u[3] - u[2]]
u0 = [0.00001,0.00001,1.22*(10**28),0]
t = np.linspace(0.0000000001,0.6924*10**33,10000)
sol=solve_ivp(eq,t,u0,None)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文