Array indices must be positive integers or logical values.

4 visualizaciones (últimos 30 días)
lulu
lulu el 25 de Nov. de 2022
Respondida: Walter Roberson el 26 de Nov. de 2022
Hi all I have the above error, could you please help me to know the problem. THANK YOU.
xl=0; xr=1; %domain[xl,xr]
J=100; % J: number of dividion for x
dx=(xr-xl)/J; % dx: mesh size
ds=dx;
tf=10; % final simulation time
Nt=100; %Nt=100000; %Nt:number of time steps
dt=tf/Nt;
%c=50;
%s=600; % paremeter for the solution
lambdau1=0.008; %turning rate of susp(right)
lambdau2=0.008; %turning rate of susp(left)
lambdav1=0.005; %turning rate of infect(right)
lambdav2=0.005; %turning rate of infect(left)
beta=0.04; % transimion rate
alpha=0.6; %recovery/death rate
b=6;
d=3;
bV=3;
dV=5;
au=0.01; % advection speed - susceptible
av=0.001; % advection speed - infected
A=0.1;
mu1=au*dt/dx;
mu2=av*dt/dx;
if mu1>1.0 %make sure dt satisfy stability condition
error('mu1 should<1.0!')
end
if mu2>1.0 %make sure dt satisfy stability condition
error('mu2 should<1.0!')
end
%Evaluate the initial conditions
x=xl:dx:xr; %generate the grid point
%fr=0*exp(-c*(x-0.5).^2); %dimension f(1:J+1)initial conditions of right moving suscp
%fl=0*exp(-c*(x-0.5).^2); %initial conditions of left moving suscep
%gr=0.5*exp(-s*(x-0.5).^2); %initial conditions of infected
%gl=0.5*exp(-s*(x-0.5).^2); %initial conditions of infected
kk=6*pi/(xr-xl);
%fr=0.0+0.01*(1+sin(k*x)); %dimension f(1:J+1)initial conditions of right moving suscp
%fl=0.001+0.01*(1+sin(k*x)); %initial conditions of left moving suscep
fr = 7.5 + 0.001*(1+sin(kk*x));
fl = 7.5 + 0.001*(1+sin(kk*x));
gr = 0.0 + 0.001*(1+sin(kk*x)); %initial conditions of infected
gl = 0.0 + 0.001*(1+sin(kk*x));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
k1=6*pi/(xr-xl);
X=k1*ds;
for ii=1:J+1
Sr = 7.5 + 0.001*(1+sin(X*ii));
Sl = 7.5 + 0.001*(1+sin(X*ii));
Ir = 0.0 + 0.001*(1+sin(X*ii)); %initial conditions of infected
Il = 0.0 + 0.001*(1+sin(X*ii));
end
%store the solution at all grid points for all time steps
ur=zeros(J+1,Nt);
ul=zeros(J+1,Nt);
vr=zeros(J+1,Nt);
vl=zeros(J+1,Nt);
v=zeros(J+1,Nt);
I=zeros(J+1,Nt);
W=zeros(J+1,Nt);
Z=zeros(J+1,Nt);
U=zeros(J+1,Nt);
I1=zeros(J+1,Nt);
W1=zeros(J+1,Nt);
U1=zeros(J+1,Nt);
Z1=zeros(J+1,Nt);
II=zeros(J+1,Nt);
WW=zeros(J+1,Nt);
ZZ=zeros(J+1,Nt);
UU=zeros(J+1,Nt);
II1=zeros(J+1,Nt);
WW1=zeros(J+1,Nt);
ZZ1=zeros(J+1,Nt);
UU1=zeros(J+1,Nt);
y1=zeros(J+1,Nt);
y2=zeros(J+1,Nt);
y3=zeros(J+1,Nt);
y4=zeros(J+1,Nt);
Q1=zeros(J+1,Nt);
Q2=zeros(J+1,Nt);
Q3=zeros(J+1,Nt);
Q4=zeros(J+1,Nt);
F1=zeros(J+1,Nt);
F2=zeros(J+1,Nt);
F3=zeros(J+1,Nt);
F4=zeros(J+1,Nt);
P1=zeros(J+1,Nt);
P2=zeros(J+1,Nt);
P3=zeros(J+1,Nt);
P4=zeros(J+1,Nt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tt=0:tf/(Nt-1):tf;
[T,X]=meshgrid(tt,x);
%s=0.05;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wb = waitbar(0, 'please wait (N)');
cleanMe = onCleanup(@() delete(wb));
%find the approximate solution at each time step
for n=1:Nt
waitbar(n/Nt, wb);
t=n*dt; % current time
if n==1 % first time step
KU0 = 1 ./ (0.05 .* sqrt(2*pi)) .* exp(-0.5*(0./0.05).^2 );
KU = @(m) 1 ./ (0.05 .* sqrt(2*pi)) .* exp(-0.5*(m./0.05).^2 );
bKU = -b .* (Ir+Il);
y1 = bKU + d.* Sr ;
y2 = bKU+ d.* Sl;
Q1= bKU + d.* Sr;
Q2 = bKU + d.* Sl;
KV0 = 1 ./ (0.01 .* sqrt(2*pi)) .* exp(-0.5*(0./0.01).^2 );
KV = @(m) 1 ./ (0.01 .* sqrt(2*pi)) .* exp(-0.5*(m./0.01).^2 );
bKV = -bV .* (Ir+Il);
F1 = bKV + d .* Sr ;
F2 = bKV + d .* Sl;
P1= bKV + d .* Sr;
P2= bKV + d .* Sl;
for i=1:J+1
sum=0;
sum1=0;
sum2=0;
sum3=0;
for k=1:J
if ((i+k-J)>=1)
sum=sum+2*KU0*y1((i+k)-(J+1));
sum1=sum1+2*KU0*y2((i+k)-(J+1));
sum2=sum2+2*KV0*F1((i+k)-(J+1));
sum3=sum3+2*KV0*F2((i+k)-(J+1));
end
end
I(i) = ds/2*(2*KU(ds*(J+1))*2*y1(i)+sum+2*KU(ds*(J+1)));
W(i) = ds/2*(2*KU(ds*(J+1))*2*y2(i)+sum1+2*KU(ds*(J+1)));
Z(i) = ds/2*(KV(ds*(J+1))*2*F1(i)+sum2+KV(ds*(J+1)));
U(i) = ds/2*(KV(ds*(J+1))*2*F2(i)+sum3+KV(ds*(J+1)));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:J+1
sum4=0;
sum5=0;
sum6=0;
sum7=0;
for k=1:J
if (i-k<=0)
sum4=sum4+2*KV0*Q1(J+1+(i-k));
sum5=sum5+2*KV0*Q2(J+1+(i-k));
sum6=sum6+2*KV0*P1(J+1+(i-k));
sum7=sum7+2*KV0*P2(J+1+(i-k));
else
if (i-k>0)
sum4=sum4+2*KV0*Q1(i-k);
sum5=sum5+2*KV0*Q2((i-k));
sum6=sum6+2*KV0*P1((i-k));
sum7=sum7+2*KV0*P2((i-k));
end
end
end
II(i)=ds/2*(KU(-ds*(J+1))*2*Q1(i)+sum4+KU(-ds*(J+1)));
WW(i)=ds/2*(KU(-ds*(J+1))*2*Q2(i)+sum5+KU(-ds*(J+1)));
ZZ(i)=ds/2*(KV(-ds*(J+1))*2*P1(i)+sum6+KV(-ds*(J+1)));
UU(i)=ds/2*(KV(-ds*(J+1))*2*P2(i)+sum7+KV(-ds*(J+1)));
end
for j=2:J % interior nodes
%%%%%%%%%%%%%%%%%%%upwind of right-moving of susceptible
ur(j,n)=fr(j)-0.5*mu1*(fr(j)-fr(j-1))+dt*lambdau2*(0.5+0.5*tanh(1+(I(j)+II(j))))*fl(j)-dt*lambdau1*(0.5+0.5*tanh(1+(W+WW)))*fr(j)-dt*beta*fr(j)*(gr(j)+gl(j))+dt*0.5*alpha*(gr(j)+gl(j));
%%%%%%%%%%%%%%%%%%%upwind of left-moving of susceptible
ul(j,n)=fl(j)+0.5*mu1*(fl(j+1)-fl(j))+dt*lambdau1*(0.5+0.5*tanh(1+(W(j)+WW(j))))*fr(j)-dt*lambdau2*(0.5+0.5*tanh(1+(I(j)+II(j))))*fl(j)-dt*beta*fl(j)*(gr(j)+gl(j))+dt*0.5*alpha*(gr(j)+gl(j));
%%%%%%%%%%%%%%%%%%%upwind of right-moving of infected
vr(j,n)=gr(j)-0.5*mu2*(gr(j)-gr(j-1))+dt*lambdav2*(0.5+0.5*tanh(1+(ZZ(j)+Z(j))))*gl(j)-dt*lambdav1*(0.5+0.5*tanh(1+(U(j)+UU(j))))*gr(j)+dt*beta*fr(j)*(gr(j)+gl(j))-dt*alpha*gr(j);
%%%%%%%%%%%%%%%%%%%lupwind of left-moving of infected
vl(j,n)=gl(j)+0.5*mu2*(gl(j+1)-gl(j))+dt*lambdav1*(0.5+0.5*tanh(1+(U(j)+UU(j))))*gr(j)-dt*lambdav2*(0.5*+0.5*tanh(1+(ZZ(j)+Z(j))))*gl(j)+dt*beta*fl(j)*(gr(j)+gl(j))-dt*alpha*gl(j);
end
%peridic BC for right moving of suscep(j=J+1,j+2=1)
ur(J+1,1)= fr(J+1)-0.5*mu1*(fr(J+1)-fr(J))+dt*lambdau2*(0.5+0.5*tanh(1+(I(J+1)+II(J+1))))*fl(J+1)-dt*lambdau1*(0.5+0.5*tanh(1+(W(J+1)+WW(J+1))))*fr(J+1)-dt*beta*fr(J+1)*(gr(J+1)+gl(J+1))+dt*0.5*alpha*(gr(J+1)+gl(J+1));
%peridic BC for right moving of suscep(j=1,0=J)
ur(1,1)=fr(1)-0.5*mu1*(fr(1)-fr(J))+dt*lambdau2*(0.5+0.5*tanh(1+(II(1)+I(1))))*fl(1)-dt*lambdau1*(0.5+0.5*tanh(1+(W(1)+WW(1))))*fr(1)-dt*beta*fr(1)*(gr(1)+gl(1))+dt*0.5*alpha*(gr(1)+gl(1)); %peridic BC for right moving
%peridic BC for left moving of suscep(j=1)
ul(1,1)=fl(1)+0.5*mu1*(fl(2)-fl(1))+dt*lambdau1*(0.5+0.5*tanh(1+(W(1)+WW(1))))*fr(1)-dt*lambdau2*(0.5*+0.5*tanh(1+(I(1)+II(1))))*fl(1)-dt*beta*fl(1)*(gr(1)+gl(1))+dt*0.5*alpha*(gr(1)+gl(1)); %peridic BC for left moving; %peridic BC for left moving
%peridic BC for left moving of suscep(j=J+1)
ul(J+1,1)=fl(J+1)+0.5*mu1*(fl(1)-fl(J+1))+dt*lambdau1*(0.5+0.5*tanh(1+(W(J+1)+WW(J+1))))*fr(J+1)-dt*lambdau2*(0.5*+0.5*tanh(1+(I(J+1)+II(J+1))))*fl(J+1)-dt*beta*fl(J+1)*(gr(J+1)+gl(J+1))+dt*0.5*alpha*(gr(J+1)+gl(J+1)); %peridic BC for right moving(j=J+1); %peridic BC for left moving
%peridic BC for right moving of infected(j=J+1)
vr(J+1,1)=gr(J+1)-0.5*mu2*(gr(J+1)-gr(J))+dt*lambdav2*(0.5+0.5*tanh(1+Z(J+1)+ZZ(J+1)))*gl(J+1)-dt*lambdav1*(0.5+0.5*tanh(1+(U(J+1)+UU(J+1))))*gr(J+1)+dt*beta*fr(J+1)*(gr(J+1)+gl(J+1))-dt*alpha*gr(J+1);
%peridic BC for right moving of infected(j=1)
vr(1,1)=gr(1)-0.5*mu2*(gr(1)-gr(J))+dt*lambdav2*(0.5+0.5*tanh(1+(Z(1)+ZZ(1))))*gl(1)-dt*lambdav1*(0.5+0.5*tanh(1+(U(1)+UU(1))))*gr(1)+dt*beta*gr(1)*(gr(1)+gl(1))-dt*alpha*gr(1);
%peridic BC for left moving of infected(j=1)
vl(1,1)=gl(1)+0.5*mu2*(gl(2)-gl(1))+dt*lambdav1*(0.5+0.5*tanh(1+(U(1)+UU(1))))*gr(1)-dt*lambdav2*(0.5*+0.5*tanh(1+(Z(1)+ZZ(1))))*gl(1)+dt*beta*fl(J+1)*(gr(1)+gl(1))-dt*alpha*gl(1);
%peridic BC for left moving of infected(j=J+1)
vl(J+1,1)=gl(J+1)+0.5*mu2*(gl(1)-gl(J+1))+dt*lambdav1*(0.5+0.5*tanh(1+(U(J+1)+UU(J+1))))*gr(J+1)-dt*lambdav2*(0.5*+0.5*tanh(1+(Z(J+1)+ZZ(J+1))))*gl(J+1)+dt*beta*fl(J+1)*(gr(J+1)+gl(J+1))-dt*alpha*gl(J+1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:J+1
Sr=ur(j,n);
Sl=ul(j,n);
Il=vl(j,n);
Ir=ur(j,n);
end
else
KU = @(m) 1 ./ (0.05 .* sqrt(2*pi)) .* exp(-0.5*(m./0.05).^2 );
bKU2 = -b .* (vr(j,n)+vl(j,n));
y3 = bKU2 + d .* ur(j,n);
y4 = bKU2 + d .* ul(j,n);
Q3 = bKU2 + d .* ur(j,n);
Q4 = bKU2 + d .* ul(j,n);
KV = @(m) 1 ./ (0.01 .* sqrt(2*pi)) .* exp(-0.5*(m./0.01).^2 );
bKV2 = -bV .* (vr(j,n)+vl(j,n));
F3 = bKV2 + d .* ur(j,n);
F4 = bKV2 + d .*ul(j,n);
P3 = bKV2 + d .* ur(j,n);
P4 = bKV2 + d .*ul(j,n);
for i=1:J+1
sum8=0;
sum9=0;
sum10=0;
sum11=0;
for k=1:J
if (i+k>=J+1)
sum8=sum8+2*KU(ds*k)*y3(J+1-(i+k));
sum9=sum9+2*KU(ds*k)*y4(J+1-(i+k));
sum10=sum10+2*KV(ds*k)*F3(J+1-(i+k));
sum11=sum11+2*KV(ds*k)*F4(J+1-(i+k));
end
end
I1(i,n-1)=ds/2*(KU(ds*(J+1))*2*y3(i)+sum8+KU(ds*(J+1)));
W1(i,n-1)=ds/2*(KU(ds*(J+1))*2*y4(i)+sum9+KU(ds*(J+1)));
Z1(i,n-1)=ds/2*(KV(ds*(J+1))*2*F3(i)+sum10+KV(ds*(J+1)));
U1(i,n-1)=ds/2*(KV(ds*(J+1))*2*F4(i)+sum11+KV(ds*(J+1)));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:J+1
sum8=0;
sum9=0;
sum10=0;
sum11=0;
for k=1:J
if (i-k<=0)
sum8=sum8+2*KU(-ds*k)*Q3(J+1+(i-k));
sum9=sum9+2*KU(-ds*k)*Q4(J+1+(i-k));
sum10=sum10+2*KV(-ds*k)*P3(J+1+(i-k));
sum11=sum11+2*KV(-ds*k)*P4(J+1+(i-k));
else
if (i-k>0)
sum8=sum8+2*KU(-ds*k)*Q3(i-k);
sum9=sum9+2*KU(-ds*k)*Q4((i-k));
sum10=sum10+2*KV(-ds*k)*P3((i-k));
sum11=sum11+2*KV(-ds*k)*P4((i-k));
end
end
end
II1(i)=ds/2*(KU(-ds*(J+1))*2*Q3(i)+sum8+KU(-ds*(J+1)));
WW1(i)=ds/2*(KU(-ds*(J+1))*2*Q4(i)+sum9+KU(-ds*(J+1)));
ZZ1(i)=ds/2*(KV(-ds*(J+1))*2*P3(i)+sum10+KV(-ds*(J+1)));
UU1(i)=ds/2*(KV(-ds*(J+1))*2*P4(i)+sum11+KV(-ds*(J+1)));
end
for j=2:J % interior nodes
%%%%%%%%%%%%%%%%%%%upwind of right-moving of susceptible
ur(j,n)=ur(j,n-1)-0.5*mu1*(ur(j,n-1)-ur(j-1,n-1))+dt*lambdau2*(0.5+0.5*tanh(1+(I1(j,n-1)+II1(j,n-1))))*ul(j,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+(W1(j,n-1)+WW1(j,n-1))))*ur(j,n-1)-dt*beta*ur(j,n-1)*(vr(j,n-1)+vl(j,n-1))+dt*0.5*alpha*(vr(j,n-1)+vl(j,n-1));
%%%%%%%%%%%%%%%%%%%upwind of right-moving of susceptible
ul(j,n)=ul(j,n-1)+0.5*mu1*(ul(j+1,n-1)-ul(j,n-1))+dt*lambdau1*(0.5+0.5*tanh(1+(W1(j,n-1)+WW1(j,n-1))))*ur(j,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+(I1(j,n-1)+II1(j,n-1))))*ul(j,n-1)-dt*beta*ul(j,n-1)*(vr(j,n-1)+vl(j,n-1))+dt*0.5*alpha*(vr(j,n-1)+vl(j,n-1));
%%%%%%%%%%%%%%%%%%%upwindof right-moving of infected
vr(j,n)=vr(j,n-1)-0.5*mu2*(vr(j,n-1)-vr(j-1,n-1))+dt*lambdav2*(0.5+0.5*tanh(1+(Z1(j,n-1)+ZZ1(j,n-1))))*vl(j,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+(U1(j,n-1)+UU1(j,n-1))))*vr(j,n-1)+dt*beta*ur(j,n-1)*(vr(j,n-1)+vl(j,n-1))-dt*alpha*vr(j,n-1);
%%%%%%%%%%%%%%%%%%%upwind of left-moving of infected
vl(j,n)=vl(j,n-1)+0.5*mu2*(vl(j+1,n-1)-vl(j,n-1))+dt*lambdav1*(0.5+0.5*tanh(1+(U1(j,n-1)+UU1(j,n-1))))*vr(j,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+(Z1(j,n-1)+ZZ1(j,n-1))))*vl(j,n-1)+dt*beta*ul(j,n-1)*(vr(j,n-1)+vl(j,n-1))-dt*alpha*vl(j,n-1);
end
%peridic BC for right moving of suscep(j=J+1,j+2=1)
ur(J+1,n)=ur(J+1,n-1)-0.5*mu1*(ur(J+1,n-1)-ur(J,n-1))+dt*lambdau2*(0.5+0.5*tanh(1+(I1(J+1,n-1)+II1(J+1,n-1))))*ul(J+1,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+(W1(J+1,n-1)+WW1(J+1,n-1))))*ur(J+1,n-1)-dt*beta*ur(J+1,n-1)*(vr(J+1,n-1)+vl(J+1,n-1))+dt*0.5*alpha*(vr(J+1,n-1)+vl(J+1,n-1));
%peridic BC for right moving of suscep(j=1,0=J)
ur(1,n)=ur(1,n-1)-0.5*mu1*(ur(1,n-1)-ur(J,n-1))+dt*lambdau2*(0.5+0.5*tanh(1+(I1(1,n-1)+II1(1,n-1))))*ul(1,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+(W1(1,n-1)+WW1(1,n-1))))*ur(1,n-1)-dt*beta*ur(1,n-1)*(vr(1,n-1)+vl(1,n-1))+dt*0.5*alpha*(vr(1,n-1)+vl(1,n-1));
%peridic BC for left moving of suscep (j=1)
ul(1,n)=ul(1,n-1)+0.5*mu1*(ul(2,n-1)-ul(1,n-1))+dt*lambdau1*(0.5+0.5*tanh(1+(W1(1,n-1)+WW1(1,n-1))))*ur(1,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+(I1(1,n-1)+II1(1,n-1))))*ul(1,n-1)-dt*beta*ul(1,n-1)*(vr(1,n-1)+vl(1,n-1))+dt*0.5*alpha*(vr(1,n-1)+vl(1,n-1));
%peridic BC for left moving of suscep(j=J+1)
ul(J+1,n)=ul(J+1,n-1)+0.5*mu1*(ul(1,n-1)-ul(J+1,n-1))+dt*lambdau1*(0.5+0.5*tanh(1+(W1(J+1,n-1)+WW1(J+1,n-1))))*ur(J+1,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+(I1(J+1,n-1)+I1(J+1,n-1))))*ul(J+1,n-1)-dt*beta*ul(J+1,n-1)*(vr(J+1,n-1)+vl(J+1,n-1))+dt*0.5*alpha*(vr(J+1,n-1)+vl(J+1,n-1));
%peridic BC for right moving of infected(j=J+1)
vr(J+1,n)=vr(J+1,n-1)-0.5*mu2*(vr(J+1,n-1)-vr(J,n-1))+dt*lambdav2*(0.5+0.5*tanh(1+(Z1(J+1,n-1)+ZZ1(J+1,n-1))))*vl(J+1,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+(U1(J+1,n-1)+UU1(J+1,n-1))))*vr(J+1,n-1)+dt*beta*ur(J+1,n-1)*(vr(J+1,n-1)+vl(J+1,n-1))-dt*alpha*vr(J+1,n-1);
%peridic BC for right moving of infected(j=1)
vr(1,n)=vr(1,n-1)-0.5*mu2*(vr(1,n-1)-vr(J,n-1))+dt*lambdav2*(0.5+0.5*tanh(1+(Z1(1,n-1)+ZZ1(1,n-1))))*vl(1,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+(U1(1,n-1)+UU1(1,n-1))))*vr(1,n-1)+dt*beta*ur(1,n-1)*(vr(1,n-1)+vl(1,n-1))-dt*alpha*vr(1,n-1);
%peridic BC for left moving of infected(j=1)
vl(1,n)=vl(1,n-1)+0.5*mu2*(vl(2,n-1)-vl(1,n-1))+dt*lambdav1*(0.5+0.5*tanh(1+(U1(1,n-1)+UU1(1,n-1))))*vr(1,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+(Z1(1,n-1)+ZZ1(1,n-1))))*vl(1,n-1)+dt*beta*ul(1,n-1)*(vr(1,n-1)+vl(1,n-1))-dt*alpha*vl(1,n-1);
%peridic BC for left moving of infected(j=J+1)
vl(J+1,n)=vl(J+1,n-1)+0.5*mu2*(vl(1,n-1)-vl(J+1,n-1))+dt*lambdav1*(0.5+0.5*tanh(1+(U1(J+1,n-1)+UU1(J+1,n-1))))*vr(J+1,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+(Z1(J+1,n-1)+ZZ1(J+1,n-1))))*vl(J+1,n-1)+dt*beta*ul(J+1,n-1)*(vr(J+1,n-1)+vl(J+1,n-1))-dt*alpha*vl(J+1,n-1);
end
for j=1:J+1
Sr(j)=ur(j,n);
Sl(j)=ul(j,n);
Il(j)=vl(j,n);
Ir(j)=ur(j,n);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%calculate the analytical solution
for j=1:J+1
xj=xl+(j-1)*dx;
u_ex(j,n)=exp(-c*(xj-au*t-0.5)^2);
end
end
figure
ss=surf(T,X,ur+ul);
set(ss,'edgecolor','none');
xlabel('t','FontSize',28);
ylabel('x','FontSize',28);
title('u^++u^-','FontSize',28);
set(gca,'Fontsize',28);
view(2)
%%%%%%%%%%%%

Respuestas (3)

Florian Rössing
Florian Rössing el 25 de Nov. de 2022
The code tells you that the error is at line 125, which is here:
if ((i+k-J)>=1)
sum=sum+2*KU0*y1((i+k)-(J+1)); %< the problem ist that you check (i+k-J)>=1 instead of >1
sum1=sum1+2*KU0*y2((i+k)-(J+1));
sum2=sum2+2*KV0*F1((i+k)-(J+1));
sum3=sum3+2*KV0*F2((i+k)-(J+1));
end
If i+k-J = 1 then (i+k)-(J+1) is 0, which is not a valid index.
Your code has more flaws after that. I suggest you use breakpoints to identify those and fix them one by one.
  3 comentarios
Florian Rössing
Florian Rössing el 25 de Nov. de 2022
Like I said, you have more errors. I suggest you learn to debug that by yourself, at last it is your problem and debugging is a useful skill.
Some practice tipps from me:
  • Read Error Messages
  • Use break points, at the breakpoints check the variables that cause the error, e.g. compare values and sizes with what you would expect
  • Try to reproduce errors by building minimal examples, this gives better understanding of how your code works
  • Test your code frequently when adding new lines, to ensure that it still works. Debugging only a few lines is easier than debugging a 100 line script
lulu
lulu el 26 de Nov. de 2022
Editada: lulu el 26 de Nov. de 2022
Thank you, I use it. But I could not solve the problem. If anyone has an idea, please help. Thank you

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 26 de Nov. de 2022

Walter Roberson
Walter Roberson el 26 de Nov. de 2022
for ii=1:J+1
Sr = 7.5 + 0.001*(1+sin(X*ii));
Sl = 7.5 + 0.001*(1+sin(X*ii));
Ir = 0.0 + 0.001*(1+sin(X*ii)); %initial conditions of infected
Il = 0.0 + 0.001*(1+sin(X*ii));
end
You are overwriting those variables within the loop, so the result is the same as if you had only done the last iteration. The variables end up as scalars. That causes y1 to become a scalar later.

Categorías

Más información sobre GPU Computing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by