根据我的语言知识,我的代码写得正确。但它没有给我正确的解决方案(情节)。当我在mathematica中解决了相同的ODE系统时,我有正确的解决方案,两种解决方案完全不同。我正在编写一个研究项目,所以我需要在python中使用正确的代码。你能不能让我知道我的代码的错误。 python code solution Mathematica solution
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as si
##Three system
def func(state, T):
H = state[0]
P = state[1]
R = state[2]
Hd = -(16./3.)*np.pi*P
Pd = -4.*H*P
Rd = H*R
return Hd,Pd,Rd
T = np.linspace(0.1,0.9,50)
state0 = [1,0.0001, 0.1]
s = si.odeint(func, state0, T)
h = np.transpose(s)
plt.plot(T,h[0])
plt.show()
Mathematica代码
Clear[H,\[Rho],a]
Eq1=(H'[t] == -16 \[Pi] \[Rho][t]/3)
Eq2= (\[Rho]'[t] == -4 H[t] \[Rho][t])
Eq3 = (a'[t] == H[t] a[t])
sol=NDSolve[{Eq1,Eq2, Eq3,
H[0.1]==0.1, \[Rho][0.1]==0.1, a[0.1]==0.1},
{H[t],\[Rho][t],a[t]}, {t,0.1, 0.9}]
Plot[Evaluate[{H[t]}/.sol],{t,0.1,0.9}]
答案 0 :(得分:0)
这两个代码都是正确的,我只是关闭了我的笔记本电脑并再次打开它,它给了我正确的结果(如mathematica)