Use numpy to solve transport equation with wave-like initial condition Use numpy to solve transport equation with wave-like initial condition numpy numpy

Use numpy to solve transport equation with wave-like initial condition


Your implementation is correct. The distortion comes from relatively large spatial step dx. At its current value of 0.2 it is comparable to the size of the wave, which makes the wave visibly polygonal on the graph. These these discretization errors accumulate over 500 steps. This is what I get from plt.plot(X, U[-1]) with your code:

distorted

And this is what I get after using n = 100 (halving both time and space stepsize), running the solution for t in range(1000) to compensate for smaller time step, and again plotting plt.plot(X, U[-1]):

much better

The symmetric-difference approximation for du/dx has error of order dx**3 proportional to the third derivative. The manner in which these accumulate is complicated because the solution is moving around, but in any case smaller dx improves the matters if dt scales with it.