我无法在模拟中获得 spring

I am not able to get spring in the simulation

我正在模拟 spring-质量系统。这是代码。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
k = 1
m = 1
def f(x, u, t):
    return -k/m*x

x_graph = []
t_graph = []
u_graph = []
y_graph = []

def func(x_0, u_0, t_0, h):
    for i in range(1, 1000):
        m1 = h*u_0
        k1 = h*f(x_0, u_0, t_0)
        m2 = h*(u_0 + 0.5*k1)
        k2 = h*f(x_0+0.5*m1, u_0+0.5*k1, t_0+0.5*h)
        m3 = h*(u_0 + 0.5*k2)
        k3 = h*f(x_0+0.5*m2, u_0+0.5*k2, t_0+0.5*h)
        m4 = h*(u_0 + k3)
        k4 = h*f(x_0+m3, u_0+k3, t_0+h)
        x_0 += (m1 + 2*m2 + 2*m3 + m4)/6
        u_0 += (k1 + 2*k2 + 2*k3 + k4)/6
        t_0 += h
        x_graph.append(x_0)
        t_graph.append(t_0)
        u_graph.append(u_0)
        y_graph.append(0)
    return x_0



print(func(0, 5, np.pi, 0.01))

fig, ax = plt.subplots()

ax.set(xlim=(-5.1, 5.1), ylim=(-0.05,1))
# ax.grid()
def animate(i):
    l1.set_data(x_graph[:i],y_graph[:i])
    return l1,

l1, = ax.plot([],[], 'o-',markevery=[-1])

ani = animation.FuncAnimation(fig, animate,frames=len(t_graph),interval =5, blit=True)
# plt.show()
ani.save('pandemic.gif', writer='ffmpeg')

我想在 (-5,0) 点有一个 spring,它会随着方块的移动而改变它的位置。我当前的输出是这个 Edit1:- 我添加了代码,其中包含 x_graph、y_graph 和 t_graph.

的数据

我认为您正在尝试模拟围绕一个点密切接触的质量。

这个小小的修改应该有效


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
k = 1
m = 1
def f(x, u, t):
    return -k/m*x


def func(x_0, u_0, t_0, h):
    x_graph = []
    t_graph = []
    u_graph = []
    y_graph = []
    for i in range(1, 1000):
        m1 = h*u_0
        k1 = h*f(x_0, u_0, t_0)
        m2 = h*(u_0 + 0.5*k1)
        k2 = h*f(x_0+0.5*m1, u_0+0.5*k1, t_0+0.5*h)
        m3 = h*(u_0 + 0.5*k2)
        k3 = h*f(x_0+0.5*m2, u_0+0.5*k2, t_0+0.5*h)
        m4 = h*(u_0 + k3)
        k4 = h*f(x_0+m3, u_0+k3, t_0+h)
        x_0 += (m1 + 2*m2 + 2*m3 + m4)/6
        u_0 += (k1 + 2*k2 + 2*k3 + k4)/6
        t_0 += h
        x_graph.append(x_0)
        t_graph.append(t_0)
        u_graph.append(u_0)
        y_graph.append(0)
    return  t_graph, x_graph, y_graph, u_graph 

t, x, y, u = func(0, 5, np.pi, 0.01)


fig, ax = plt.subplots()

ax.set(xlim=(-5.1, 5.1), ylim=(-0.05,1))
# ax.grid()
def animate(i):
    i, j = [0, x[i]], [0, y[i]]
    l1.set_data(i, j)
    return l1,

l1, = ax.plot([],[], 'o-',markevery=[-1])
l2, = ax.plot([],[], 'o-',markevery=[-1])
l3, = ax.plot([],[], 'o-',markevery=[-1])

ani = animation.FuncAnimation(fig, animate,frames=len(t),interval =5, blit=True)
fig.show()
ani.save('pandemic.mp4', writer='ffmpeg')

已提供正确答案。我正在添加一个不同的解决方案。如果 spring 在左侧固定在刚性墙上并且球在移动,则此解决方案适用。 spring 根据墙和球之间的距离进行拉伸和压缩。如下所示。

为此,我稍微更改了您的代码。请注意,某些值是硬编码的,因此必须更改它们以满足要求。根据 x_lim 值很容易更改它们。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

k = 1
m = 1
def f(x, u, t):
    return -k/m*x

x_graph = []
t_graph = []
u_graph = []
y_graph = []

def func(x_0, u_0, t_0, h):
    for i in range(1, 1000):
        m1 = h*u_0
        k1 = h*f(x_0, u_0, t_0)
        m2 = h*(u_0 + 0.5*k1)
        k2 = h*f(x_0+0.5*m1, u_0+0.5*k1, t_0+0.5*h)
        m3 = h*(u_0 + 0.5*k2)
        k3 = h*f(x_0+0.5*m2, u_0+0.5*k2, t_0+0.5*h)
        m4 = h*(u_0 + k3)
        k4 = h*f(x_0+m3, u_0+k3, t_0+h)
        x_0 += (m1 + 2*m2 + 2*m3 + m4)/6
        u_0 += (k1 + 2*k2 + 2*k3 + k4)/6
        t_0 += h
        x_graph.append(x_0)
        t_graph.append(t_0)
        u_graph.append(u_0)
        y_graph.append(0)
    return x_0



print(func(0, 5, np.pi, 0.01))

fig, ax = plt.subplots()

ax.set(xlim=(-5.1, 5.1), ylim=(-1,1))
# ax.grid()
def animate(i):
    l1.set_data(x_graph[:i],y_graph[:i])
    sin_x = []
    sin_y = []
    for j in np.linspace(-5,x_graph[i],250):
        sin_x.append(j)
        sin_y.append(0.15*np.sin((j+5)*(2*np.pi)*10/(x_graph[i]+5)))
    l2.set_data(sin_x, sin_y)
    return l1,l2,

l1, = ax.plot([],[], 'o',markevery=[-1])
l2, = ax.plot([],[], '-')


ani = animation.FuncAnimation(fig, animate, frames=len(t_graph),interval =0.5, blit=True)
# plt.show()
ani.save('pandemic.gif', writer='imagemagic')