其他分享
首页 > 其他分享> > 用阶跃函数求解微分方程

用阶跃函数求解微分方程

作者:互联网

我正在尝试解决这个微分方程,这是我的作业的一部分.我不明白如何在代码中输入u条件.在下面显示的代码中,我任意提供了

u = 5. 


2dx(t)dt=−x(t)+u(t)

5dy(t)dt=−y(t)+x(t)

u=2S(t−5)

x(0)=0

y(0)=0

其中S(t-5)是一个阶跃函数,在t = 5时从零变为一.当它乘以2时,它同时从零变为2,t = 5.

def model(x,t,u):
    dxdt = (-x+u)/2
    return dxdt

def model2(y,x,t):
    dydt = -(y+x)/5
    return dydt

x0 = 0
y0 = 0
u = 5
t = np.linspace(0,40)


x = odeint(model,x0,t,args=(u,))
y = odeint(model2,y0,t,args=(u,))
plt.plot(t,x,'r-')
plt.plot(t,y,'b*')
plt.show()

解决方法:

我不太了解SciPy库,但是对于example in the documentation,我会尝试这样的操作:

def model(x, t, K, PT)
    """
    The model consists of the state x in R^2, the time in R and the two
    parameters K and PT regarding the input u as step function, where K
    is the infimum of u and PT is the delay of the step.
    """
    x1, x2 = x   # Split the state into two variables

    u = K if t>=PT else 0    # This is the system input

    # Here comes the differential equation in vectorized form
    dx = [(-x1 + u)/2,
          (-x2 + x1)/5]
    return dx

x0 = [0, 0]
K  = 2
PT = 5
t = np.linspace(0,40)

x = odeint(model, x0, t, args=(K, PT))
plt.plot(t, x[:, 0], 'r-')
plt.plot(t, x[:, 1], 'b*')
plt.show()

标签:differential-equations,scipy,ode,python
来源: https://codeday.me/bug/20191025/1926463.html