简体   繁体   English

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

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

I am solving a system of coupled differential equations, with one of the "constant" in the differential equations is actually a periodically changing value: first half of the period has the value of 1 and the rest of the period have the value of 0, and the period is 2pi.我正在求解一个耦合微分方程组,微分方程中的一个“常数”实际上是一个周期性变化的值:周期的前半部分的值为 1,周期的其余部分的值为 0,周期为 2pi。

I was setting the value of that constant as a square function(used square function code and if else codes), and solving the differential equation with odeint.我将该常数的值设置为平方函数(使用平方函数代码和 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()

But seems that since the constant is not a "scalar", the code is not solvable.但似乎由于常数不是“标量”,代码不可解。

I have also consult this post: Plotting solved ordinary differential equations over changing constant values but the method and the result is not what I want I out of solution and have no further idea how to include this periodic changing value and solve the system.我还参考了这篇文章: 在不断变化的值上绘制求解的常微分方程,但方法和结果不是我想要的,我没有解决方案,也不知道如何包含这个周期性变化的值并求解系统。

Or is that the odeint is not usable in this case?还是在这种情况下 odeint 不可用?

Thanks in advance of any answer.提前感谢任何答案。

There are several things wrong from a pure python perspective in this function从纯 python 的角度来看,这个函数有几处错误

def l(f):
    if int(2*t)%2np.pi == 0:
        return 1
    else:
        return -1
  • The parameter f is not used in the function, instead an undeclared t appears.函数中没有使用参数f ,而是出现了一个未声明的t
  • 2np.pi should give an immediate syntactic error 2np.pi应该立即给出语法错误
  • int(2*t)%2*np.pi is (int(2*t)%2)*np.pi , which is likely not the desired interpretation. int(2*t)%2*np.pi(int(2*t)%2)*np.pi ,这可能不是所需的解释。 Use int((2*f)%(2*np.pi)) .使用int((2*f)%(2*np.pi)) You can also cancel the 2您也可以取消 2
  • The lower value is -1 , while in the description you have 0 .较低的值是-1 ,而在描述中你有0

  • model is a system for 3 state components, your initial state has 5 components. model是一个包含 3 个状态组件的系统,您的初始状态有 5 个组件。 That is not compatible.那是不兼容的。

  • Repeating expressions should be assigned to a local variable to avoid redundant computation.重复表达式应分配给局部变量以避免冗余计算。

Please report a detailed error description, not just your summary of the observed error.请报告详细的错误描述,而不仅仅是您对观察到的错误的总结。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM