所以,目前我正试图找到berger方程的数值解,$ u_t u * u_x = 0 $ . 这个等式的数值解是:

$ u ^ {n 1} j = u_n ^ j- \ frac {Δx} {Δt} u ^ n_j(u ^ n_j-u ^ n )$我在Matlab上编写了计算代码的代码 . 如您所见,代码中只有一个for循环 . 但是,我的u矩阵非常大,算法变慢 . 有没有办法让我根本不使用循环 . 我在考虑使用命令cumsum,但我不确定如何将它集成到我的程序中 . 请看我的代码 . 谢谢 .

dx = 0.9;
dt = 0.9;
tf = 10;
l = 10;
xstep = [-1:dx:l];
tstep = [0:dt:tf];
uinit = zeros(length(tstep),length(xstep));


%%Setting Initial and Boundary Conditions
bc.left = @(t) 2;

bc.right = @(t) -1;

ic = @(x) ...
    2*(x<=0) ...
    -1*(x>0);

uinit(1, :) = ic(xstep);
uinit(:, 1) = bc.left(tstep);
uinit(:, end) = bc.right(tstep);


%% Numerical method part

for c=1:(length(tstep)-1) 
   uinit(2:end -1, c+1) = uinit(2:end -1, c) - dx/dt*(uinit(2:end -1, c).*(uinit(2:end-1, c) - uinit(1:end-2, c)));

end

surf(xstep,tstep,uinit)