Consider the advection equation
$$ v_t + v_x = 1 $$
with initial condition
$$ v(x,0) = \begin{cases} \sin^2 \pi (x-1), & x \in [1,2] \\ 0, & \text{otherwise} \end{cases}$$
Clearly, we know that for any $F$, the general solution is
$$ v(x,t) = F(x - s t) $$ and $v(x,0) = F(x) = \sin^2 \pi (x-1)$. Therefore, the solution we are looking for is
$$ v(x,t) = \sin^2 \pi (x-1-st) $$
where $s$ is constant.
My question is how do we implement the solution numerically in matlab? Numerically, we can discretize the PDE using the following scheme Lax
$$ \frac{ u_j^{n+1} - \frac{1}{2}( u_{j+1}^n + u_{j-1}^n) }{\Delta t} + \frac{ u_{j+1}^n - u_{j-1}^n }{2 \Delta x} =0 $$
say for $x \in [0,6]$ and $t \in [0,4]$
