In the previous question, if the NX is infinitely larger by itself (or NX = 100), the resulting drawing is divergent.
This is because there is an important requirement in the finite difference, the CFL number, in particular, the dependent domain of the difference equation must contain the dependent domain of the corresponding differential equation, the simplest can be understood as the time to push the solution speed (DX/DT) must be greater than the speed of the physical disturbance Propagation (U), only in order to capture all the physical disturbances.
every time intervalDTThe distance from the "wave" spread isDX, whileDXwith theNXrelated, the equation to keep the numerical solution stable is:
Import numpy as npimport matplotlib.pyplot as pltdef linearconv (NX): dx = 2./(nx-1) nt = 20 #nt is the number of timesteps we want to calculate c = 1 sigma = .5 dt = sigma*dx #跟步骤2唯一的变化在这 u = np.ones (NX) u[.5/dx : 1/dx+1]=2 un = np.ones (NX) for n in range (NT): # Iterate through time un = u.copy () ## copy the existing values of u into un for i In range (1,NX): u[i] = un[i]-c*dt/dx* (Un[i]-un[i-1]) plt.plot ( Np.linspace (0,2,NX), u)
Linearconv (121), the graph and step 2 are similar
Python for CFD (summary of the first two steps)