簡體   English   中英

使用 odeint 和 sympy 求解微分方程

[英]Problem solving differential equations using odeint and sympy

我正在嘗試求解並顯示以下等式的圖形:

f'=a f²-b f

因此我嘗試使用scipy.integrate.odeint庫函數來解決它,但沒有成功。

這是我到目前為止所做的:

from IPython.display import display
import sympy as sy
from sympy.solvers.ode import dsolve
import matplotlib.pyplot as plt
import numpy as np
from sympy import functions
from sympy import Function, Symbol

sy.init_printing()

t = sy.symbols("t", real=True)
f = sy.symbols("f", cls=functions)

eq1 = sy.Eq(f(t).diff(t), (0.1*f(t)**2 - 2*f(t)))
sls = dsolve(eq1)

print("For ode")
display(eq1)
print("the solutions are:")
for s in sls:
    display(s)

# plot solutions:
x = np.linspace(0, 3, 100)
fg, axx = plt.subplots(5000, 3)

然后我想為不同的f₀t=0的條件)顯示它,就像你對一個看起來像這樣的正規方程所做的那樣:

圖形

我使用帶有isympy shell 的 Sympy 解決了您的問題(簡化了一些定義名稱等)——您必須更加小心地執行所有必要的import

  1. 我求解了微分方程,取了結果方程的rhs (右手邊)並將所述rhs表達式分配給變量sol
  2. 我為c1求解方程f(0)-x0=0
  3. 我為所涉及的不同符號分配(任意)值。
  4. 在將除t之外的所有變量的實際值替換為繪圖的自由變量之后,我繪制了函數。 在此處輸入圖像描述
18:43 boffi@debian:~ $ isympy 
IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: gmpy)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at https://docs.sympy.org/1.4/


In [1]: a, b = symbols('a b')                                                             

In [2]: sol = dsolve(Derivative(f(t), t) + a*f(t)**2 + b*f(t), f(t)).rhs                  

In [3]: c1, x0 = symbols('C1 x0')                                                         

In [4]: c1_from_x0, = solve(sol.subs(t, 0) - x0, c1)                                      

In [5]: sol, c1_from_x0, sol.subs(c1, c1_from_x0)                                         
Out[5]: 
⎛                       ⎛  a⋅x₀  ⎞                                ⎞
⎜        C₁⋅b        log⎜────────⎟                                ⎟
⎜     b⋅ℯ               ⎝a⋅x₀ + b⎠               b⋅x₀             ⎟
⎜──────────────────, ─────────────,  ──────────────────────────────⎟
⎜  ⎛   C₁⋅b    b⋅t⎞        b                   ⎛    a⋅x₀      b⋅t⎞⎟
⎜a⋅⎝- ℯ     + ℯ   ⎠                 (a⋅x₀ + b)⋅⎜- ──────── + ℯ   ⎟⎟
⎝                                              ⎝  a⋅x₀ + b       ⎠⎠

In [6]: values = {x0:10, a:3, b:4, c1:c1_from_x0}                                         

In [7]: plot(sol.subs(values), (t, 0, 0.5));

In [8]: sol.subs(values).simplify()                                                      
Out[8]: 
     20     
────────────
    4⋅t     
17⋅ℯ    - 15

In [9]:

附錄

為了完整起見,使用scipy.integrate.odeint 1的數值解

from numpy import exp, linspace 
from scipy.integrate import odeint 
import matplotlib.pyplot as plt 
 
t = linspace(0, 0.5, 201) ; f0 = 10; a = 3 ; b = 4                                
f = odeint(lambda f, t: (-a*f -b)*f, f0, t)                                       

fig0, ax0 = plt.subplots(figsize=(8,3))                                           
ax0.plot(t, f) ; ax0.set_title('Numerical Solution')
plt.show()

exact = 20 / (17*exp(4*t)-15)                                                     
fig1, ax1 = plt.subplots(figsize=(8,3))                                           
ax1.plot(t, (f.flat-exact)*1E6) ; ax1.set_title('(Numerical-Analytical)*10**6')   
plt.show()

數值解

誤差圖

1. 對於新代碼,使用scipy.integrate.solve_ivp求解微分方程。

如果你能得到它,了解封閉形式的解決方案總是有幫助的。 我請 Wolfram Alpha 解決您的 ODE。 是他們給我的答案。

我建議您繪制它,以便您在看到它時知道正確的答案。

暫無
暫無

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

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