簡體   English   中英

使用scipy解決python中的微分方程

[英]solve differential equation in python using scipy

您好,我想解決以下第一個ODE:

dt / dr = +-cos(t)^ 2 / cos(r)^ 2

我知道解決方案是:t(r)= t(r)= arctan(tan(r)+ _ C1),其中:pi / 2 <t <pi / 2和0 <r <pi / 2。

我想知道如何改善下面的代碼,使我的解決方案類似於圖像中t軸趨於+無窮大的曲線:

一個C值的理想解決方案

我的代碼是:

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);

我當前的圖形輸出是:

current_solution

不知道我是否理解您的問題,但是似乎兩個圖中的解決方案是相同的,但是繪制的方式卻不同。 在“對於一個C值的理想解”圖中,y軸與x軸相比被拉伸。 在您的“ current_solution”中,它們是相等的。

問題中的兩個圖是相同的,但是它們都有不同的限制。 要更改限制,您需要執行plt.xlim()plt.ylim() 將它們設置為與期望的結果相同將給您相同的結果。

y軸與x軸在0處而不是x軸的左邊緣(默認值)相交,從而為期望的結果提供了一個補充。 您可以通過移動左脊椎來更改此設置:

rs = np.linspace(0, pi/2, 1000)
t0 = 0.0 #the initial condition
ts = odeint(dt_dr, t0, rs)

plt.rcParams.update({'font.size': 11})
plt.xlabel("t")
plt.ylabel("r")
plt.plot(ts, rs)

# Change axis limits
plt.ylim(0,0.6)
plt.xlim(-1.5,1.5)

# Move left spine to x=0
ax = plt.gca()
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')

plt.show()

這使:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM