我试图对内循环进行矢量化,但我无法做到。
for i = 1:NTS
U(1) = 0;
V(1) = 0;
U(end)=1;
V(end)=1;
for j = 2:NAS-1
U(j) = (b(j) * U(j-1) + a(j) * UOld(j) + c(j) * UOld(j+1)) / gamma(j);
V(NAS-j+1) = (b(NAS-j+1) * VOld(NAS-j+1-1) + a(NAS-j+1) * VOld(NAS-j+1) + c(NAS-j+1) * V(NAS-j+1+1)) / gamma(NAS-j+1);
end
end
我已经尝试了以下代码,但是我得到了错误的结果,有人可以帮助我吗?
U(2:NAS-1) = ((b(2:NAS-1) .* U(1:NAS-2) + a(2:NAS-1) .* UOld(2:NAS-1) + c(2:NAS-1) .* UOld(3:NAS)) ./ gamma(2:NAS-1));
V(NAS-1:-1:2) = ((b(NAS-1:-1:2) .* VOld(NAS-2:-1:1) + a(NAS-1:-1:2) .* VOld(NAS-1:-1:2) + c(NAS-1:-1:2) .* V(NAS:-1:3)) ./ gamma(NAS-1:-1:2));
这是最小可验证事物所要求的整个代码。谢谢
clc
clear
NTS=1;
NAS=10;
U=rand(1,10)*100;
V=U;
UU=zeros(1,10);
VV=zeros(1,10);
Gamma=ones(1,100)*(1-10.05);
a=ones(1,10)*(1-0.05);
b=ones(1,10)*(1-0.15);
c=ones(1,10)*(1-0.35);
UOld=U;
VOld=V;
UU=U;
VV=V;
for i=1:NTS
UU(2:NAS-1) = ((b(2:NAS-1) .* UU(1:NAS-2) + a(2:NAS-1) .* UOld(2:NAS-1) + c(2:NAS-1) .* UOld(3:NAS)) ./ gamma(2:NAS-1));
VY(NAS-1:-1:2) = ((b(NAS-1:-1:2) .* VOld(NAS-2:-1:1) + a(NAS-1:-1:2) .* VOld(NAS-1:-1:2) + c(NAS-1:-1:2) .* VV(NAS:-1:3)) ./ gamma(NAS-1:-1:2));
for j = 2:NAS-1
U(j) = (b(j) * U(j-1) + a(j) * UOld(j) + c(j) * UOld(j+1)) / gamma(j);
V(NAS-j+1) = (b(NAS-j+1) * VOld(NAS-j+1-1) + a(NAS-j+1) * VOld(NAS-j+1) + c(NAS-j+1) * V(NAS-j+1+1)) / gamma(NAS-j+1);
end
display(sum(U(2:NAS-1)'-UU(2:NAS-1)'));
display(sum(V(2:NAS-1)'-VV(2:NAS-1)'));
end
display([U' UU']);
display([V' VV']);