【发布时间】:2019-04-09 19:50:30
【问题描述】:
您好,我想解决以下第一个 ODE:
dt/dr = +- cos(t)^2/cos(r)^2
我知道解决方案是:t(r) = t(r) = arctan(tan(r)+_C1),其中: pi/2
我想知道如何改进下面的代码,这样我的解决方案类似于图像中 t 轴上趋于 + 无穷大的曲线:
我的代码是:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
"""
Equations to be solved:
boundary conditions:
-pi/2 << t << pi/2
0 <= r <= pi/2
Equation:
dt/dr = +- cos^2(t)/cos^2(r)
Solution :
t(r) = arctan(tan(r) +_C1)
"""
def dt_dr(t,r):
return (cos(t)**2)/(cos(r)**2)
rs = np.linspace(0,pi/2,1000)
t0 = 0.0 #the initial condition
ts = odeint(dt_dr,t0,rs)
ts = np.array(rs).flatten()
plt.rcParams.update({'font.size': 14})
plt.xlabel("r")
plt.ylabel("t")
plt.plot(rs,ts);
我当前的图形输出是:
【问题讨论】:
-
我想你想把
ts = np.array(rs).flatten()改成rs = np.array(rs).flatten() -
如果您将轴限制更改为所需的图片限制,那么您的代码会生成相同的图表
-
您好,感谢 cmets,但更改为
rs = np.array(rs).flatten()并没有帮助,@DavidG 我如何更改上面代码中的限制。我以为我是用代码“rs = np.linspace(0,pi/2,1000)”和“t0 = 0.0”来建立它们的。
标签: python matplotlib scipy