簡體   English   中英

用python在函數中求解具有周期性變化常數的耦合微分方程

[英]solving coupled differential equations with a periodically changing constant in the function with python

我正在求解一個耦合微分方程組,微分方程中的一個“常數”實際上是一個周期性變化的值:周期的前半部分的值為 1,周期的其余部分的值為 0,周期為 2pi。

我將該常數的值設置為平方函數(使用平方函數代碼和 if else 代碼),並用 odeint 求解微分方程。

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
from scipy import signal
u=0.3
def l(f):
    if int(2*t)%2np.pi == 0:
        return 1
    else:
        return -1


def model(theta,t):   

    j = theta[0]
    x = theta[1]
    p = theta[2]
    
    dJ_dt = 2*l(f)*(math.sqrt(2))*x*j
    dX_dt = p-(k/2)*x
    dP_dt = -x-(k/2)*p-2*l(f)*(math.sqrt(2))*j
    
    dtheta_dt = [dJ_dt, dX_dt, dP_dt]
    return dtheta_dt

theta_0 = [0.5*math.sqrt(1-u**2), 0, -0.5*u, 0, 0]

t = np.linspace(0,5000,1000)

theta = odeint(model,theta_0,t)

plt.figure(figsize=(25,8))
plt.plot(t, theta[:,0],label='j')
plt.legend(fontsize=15)
plt.xlabel('Time', fontsize= 30)
plt.xticks(fontsize= 20)
plt.ylabel('jx', fontsize= 30)
plt.yticks(fontsize= 20)
plt.show()

但似乎由於常數不是“標量”,代碼不可解。

我還參考了這篇文章: 在不斷變化的值上繪制求解的常微分方程,但方法和結果不是我想要的,我沒有解決方案,也不知道如何包含這個周期性變化的值並求解系統。

還是在這種情況下 odeint 不可用?

提前感謝任何答案。

從純 python 的角度來看,這個函數有幾處錯誤

def l(f):
    if int(2*t)%2np.pi == 0:
        return 1
    else:
        return -1
  • 函數中沒有使用參數f ,而是出現了一個未聲明的t
  • 2np.pi應該立即給出語法錯誤
  • int(2*t)%2*np.pi(int(2*t)%2)*np.pi ,這可能不是所需的解釋。 使用int((2*f)%(2*np.pi)) 您也可以取消 2
  • 較低的值是-1 ,而在描述中你有0

  • model是一個包含 3 個狀態組件的系統,您的初始狀態有 5 個組件。 那是不兼容的。

  • 重復表達式應分配給局部變量以避免冗余計算。

請報告詳細的錯誤描述,而不僅僅是您對觀察到的錯誤的總結。

暫無
暫無

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

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