简体   繁体   English

如何使用 Python 求解具有非常数系数的微分方程?

[英]How to solve a differential equation with a non-constant coefficient using Python ?

Suppose there is an equation about the length of the bungee cord (denoted by x), which is dependent on the mass of the object eg a player (denoted by m).假设有一个关于弹力绳长度的方程(用 x 表示),它取决于 object 的质量,例如播放器(用 m 表示)。

Assume that the natural length of the bungee cord is 30 meters, in other words, the starting position is x(0)=-30.假设弹力绳的自然长度为30米,即起始position为x(0)=-30。

The equation of the length of the bungee cord is given by:弹力绳的长度方程由下式给出:

    x''(m) = g + b/m*x(m) -a1/m*x'(m) - a2*|x'(m)|*x'(m)

where g, a1, a2 are constants;其中g, a1, a2是常数; b is a step function: b = -k (another constant) when x<0 and b = 0 when x>=0 . b是一个步骤 function:当x<0b = -k (另一个常数),当x>=0 0 时b = 0 0 。

import numpy as np
from scipy.integrate import odeint

g = 9.8
a1, a2 = 0.6, 0.8
k = 20
b = [-k, 0]

def dx_dt(x, m):
    if x[0]>=0:
        return [x[1], g+b[0]/m*x[0]-a1/m*x[1]-a2/m*np.abs(x[1])*x[1]]
    else:
        return [x[1], g+b[1]/m*x[0]-a1/m*x[1]-a2/m*np.abs(x[1])*x[1]]

init = [[-30, 0], [-40, 0.0001]]

m = np.linspace(1, 100, 10000)

fig, ax = plt.subplots(1, 2, sharey=True, figsize=(6, 4))

for i in range(len(init)):
    xs = odeint(dx_dt, init[i], m)
    ax[i].plot(m, xs[:, 0], 'r-')
    ax[i].set_xlabel('mass (m)')
    ax[i].set_ylabel('length (x)')
    ax[i].set_xlim(xmin, xmax)
    ax[i].set_ylim(ymin, ymax)
    ax[i].set_title('init={}'.format(init[i]))

the right answer should be a sine-like curve正确答案应该是正弦曲线

but the result from above codes turns out to be但是上面代码的结果是

在此处输入图像描述

Is there something wrong with the codes?代码有问题吗?

Change the length coordinate x to point upwards, the cord without jumper being at rest at position 0 so that for x<0 the cord behaves like a spring.将长度坐标x更改为向上,没有跳线的电线在 position 0 处的 rest 处,因此对于x<0 ,电线的行为类似于 Z2A2D595E6ED9A0B24F027F2B63B134D6。 Then also gravity points downwards.然后重力也指向下方。 The modified ODE function for this is修改后的 ODE function 是

def dx_dt(x, t, m):
    acc = -g-a1/m*x[1]-a2/m*np.abs(x[1])*x[1]
    if x[0] < 0: acc -= k/m*x[0]
    return [x[1], acc]

Plotting for 3 different masses绘制 3 个不同的质量

for i,ini in enumerate(init):
    for m in masses: 
        xs = odeint(dx_dt, ini, t, args=(m,))
        ax[i].plot(t, xs[:, 0], label="m=%.2f"%m)

gives then the picture然后给出图片

在此处输入图像描述

If, as example, the ground level in the first situation is at -80m , giving a total height of 110m then the mass of the jumper has to be less than 90kg.例如,如果第一种情况下的地面高度为-80m ,总高度为110m ,则跳线的质量必须小于 90kg。 For more precise statements use inter- and extrapolation or a numerical solver to find the first time where x'(t)=0 and the critical mass where x(t)=ground at that time.对于更精确的陈述,使用内插和外推或数值求解器来找到x'(t)=0的第一次时间和当时x(t)=ground的临界质量。

It appears clear that in no case the jumper does re-enter the "free-fall" phase x>0 .显然,在任何情况下,跳线都不会重新进入“自由落体”阶段x>0

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

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