前言,
最近无聊,好多书想看,但又过于理论,怕没有实际用途,因此想做个小点的程序。目前在看偏微分的波动方程,里面的波的解挺精确的,应该比很多游戏里的要美一些。因为,我打算用python写个数值解,用图像展示出这些波动解的图像动画。
技术路线
数学理论方面:就是解那个波动方程,得到数值解。计算机方面,一张一张地把那个解画出来,然后用matplotlib中的animation或者其他的动画,连着做出来。
第一次,一个水滴落到水面时的模拟
因为里面的是一维解,所以直接用就可以了。得到的动画是这个样子:
代码:
import matplotlib.pyplot as plt import numpy as np import scipy as sp def f(x): return x z,err = sp.integrate.quad(f,0,1) print(z) def fi(x): return np.exp(-(x**2)*20) #return np.log(1+x**2) def posi(x): return 0 def waveSolution(x,t): c =1 start = x+c*t end = x -c*t front = 0.5*(fi(start) + fi(end)) behind,err = sp.integrate.quad(posi,start, end) return front +behind*(1/(2*c)) #上面的是一维的情况,也就是线 from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() xdata, ydata = [], [] ln, = plt.plot([], [], animated=True) def init(): ax.set_xlim(-10, 10) ax.set_ylim(-4, 4) return ln, def update(frame): xdata = np.linspace(-10,10,100) # print(xdata) ydata = [waveSolution(xdata[i],frame) for i in range(100)] # ydata.append(np.sin(frame)) # print(ydata) ln.set_data(xdata, ydata) return ln, ani = FuncAnimation(fig, update, frames=np.linspace(0,100,300), init_func=init, blit=True) plt.show()