问题描述来自:
http://my.oschina.net/cfdvalidation/blog/308374:
差分法解决代码:#!/usr/bin/pythonimport numpy as npimport matplotlib.pylab as pltimport time,sysnx = 41dx = 2.0/(nx-1)nt=25dt=0.025c = 1.def fun(x1,x2): x = x1 - c * (dt/dx) * (x1 - x2) return xu = np.ones(nx)u[.5/dx : 1/dx+1] = 2plt.plot(np.linspace(0,2,nx),u,label="initial")for t in range(nt): un = u.copy() for i in range(1,nx): u[i] = fun(un[i],un[i-1]) plt.plot(np.linspace(0,2,nx),u,label="converged")plt.legend(loc=' right')plt.show()