Nested for loop for Lax-Friedrich's scheme Please am writing a matlab code to solve a system of hyperbolic pde using Lax Friedrich's scheme
\begin{equation}
V_j^{n+1}=\frac{1}{2}(I+B)V_{j-1}^n+\frac{1}{2}(I-B)V_{j+1}^n
\end{equation}
where $V$ is a $3\times 1$ column matrix, $I$ is a $3\times 3$ identity matrix and $B$ is a $3\times 3$ matrix.
I have been able to define all the parameters and it does run but in the nested for loop am having difficulty writing the above equation in matlab, I always get the error subscripted assignment dimension mismatch and I don't know how to figure out this.
In my nested for loop, this is how I wrote the above equation:
for n = 1:Nt
    for j = 1:Nx
        V(j,n+n) = 1/2*(eye(3)+B)*V(j,n) + 1/2*(eye(3)-B)*V(j+2,n);
    end 
end

 A: To save memory, we don't save the data at each iteration in time, but only the data from the last iteration. Thus, we create a $3\times N_x$ matrix Vn, and a temporary $3\times N_x$ matrix Vtemp to avoid overwriting data during one time step. As written in the time-stepping formula of the Lax-Friedrichs scheme, the computation of $V_j^{n+1}$ requires $V_{j-1}^{n}$ and $V_{j+1}^{n}$. Therefore, one must be careful that the subscripts don't exceed the matrix dimensions when reaching the edges of the numerical domain. Here is a minimum working example in Matlab syntax:
I = eye(3);
B = % Enter 3x3 matrix B here. Make sure that the stability condition is satisfied.
Mp = I+B;
Mm = I-B;

Vn = zeros(3,Nx);
Vtemp = zeros(3,Nx);
for j = 1:Nx
    Vn(:,j) = sin(j)*ones(3,1); % Example of initial conditions
end

for n = 1:Nt % Loop
    for j = 2:Nx-1 % Time-stepping formula
        Vtemp(:,j) = 0.5*( Mp*Vn(:,j-1) + Mm*Vn(:,j+1) );
    end
    Vtemp(:,1) = Vtemp(:,2); % Example of boundary conditions (infinite)
    Vtemp(:,Nx) = Vtemp(:,Nx-1);
    Vn = Vtemp; % Copy
end

