I have error when I applied trapezoidal rule

1 Ansicht (letzte 30 Tage)
lulu
lulu am 18 Sep. 2022
Kommentiert: Walter Roberson am 19 Sep. 2022
This is my code, I could not figure out the error.
clear all;
xl=0; xr=1; %domain[xl,xr]
J=100; % J: number of dividion for x
dx=(xr-xl)/J; % dx: mesh size
tf=1000; % final simulation time
Nt=100000; %Nt:number of time steps
dt=tf/Nt;
c=50;
s=600; % paremeter for the solution
lambdau1=0.0004; %turning rate of susp(right)
lambdau2=0.0002; %turning rate of susp(left)
lambdav1=0.0003; %turning rate of infect(right)
lambdav2=0.0001; %turning rate of infect(left)
beta=0.1; % transimion rate
alpha=0.0; %recovery/death rate
b=0.1;
d=0.5;
bV=0.3;
dV=0.1;
au=0.0003; % advection speed - susceptible
av=0.0001; % advection speed - infected
A=0.01;
mu1=au*dt/dx;
mu2=av*dt/dx;
w = linspace(xl,xr,J);
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
k=6*pi/(xr-xl);
fr=0+0.01*(1+sin(k*x)); %dimension f(1:J+1)initial conditions of right moving suscp
fl=0+0.01*(1+sin(k*x)); %initial conditions of left moving suscep
gr=0.25+0.01*(1+sin(k*x)); %initial conditions of infected
gl=0.75+0.01*(1+sin(k*x));
g=gr+gl;
%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);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tt=0:tf/(Nt-1):tf;
[T,X]=meshgrid(tt,x);
%%%%%%%%%%%%%%%%%%%%%%%%
KU = @(x) 1/(0.05*sqrt(2*pi))*exp(-0.5*(x/0.05).^2 );
y1 = @(x) -b*KU(x)*(gr+gl)+d*KU(x)*fr;
y2 = @(x) -b*KU(x)*(gr+gl)+d*KU(x)*fl;
KV = @(x) 1/(0.01*sqrt(2*pi))*exp(-0.5*(x/0.01).^2 );
F1 = @(x) -bV*KV(x)*(gr+gl)+dV*KV(x)*fr;
F2 = @(x) -bV*KV(x)*(gr+gl)+dV*KV(x)*fl;
for i=1:J+1
x=xr+(i-1)*dx;
end
ynum1 = y1(x);
ynum2 = y2(x);
I = trapz(x,ynum1);
W = trapz(x,ynum2);
Fnum1 = F1(x);
Fnum2 = F2(x);
Z = trapz(x,Fnum1);
U = trapz(x,Fnum2);
%find the approximate solution at each time step
for n=1:Nt
t=n*dt; % current time
if n==1 % first time step
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)))*fl(j)-dt*lambdau1*(0.5+0.5*tanh(1+W(j)))*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)))*fr(j)-dt*lambdau2*(0.5+0.5*tanh(1+I(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+Z(j)))*gl(j)-dt*lambdav1*(0.5+0.5*tanh(1+U(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)))*gr(j)-dt*lambdav2*(0.5*+0.5*tanh(1+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)))*fl(J+1)-dt*lambdau1*(0.5+0.5*tanh(1+W(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+I(1)))*fl(1)-dt*lambdau1*(0.5+0.5*tanh(1+W(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)))*fr(1)-dt*lambdau2*(0.5*+0.5*tanh(1+I(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)))*fr(J+1)-dt*lambdau2*(0.5*+0.5*tanh(1+I(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,n)=gr(J+1)-0.5*mu2*(gr(J+1)-gr(J))+dt*lambdav2*(0.5+0.5*tanh(1+Z(J+1)))*gl(J+1)-dt*lambdav1*(0.5+0.5*tanh(1+U(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,n)=gr(1)-0.5*mu2*(gr(1)-gr(J))+dt*lambdav2*(0.5+0.5*tanh(1+Z(1)))*gl(1)-dt*lambdav1*(0.5+0.5*tanh(1+U(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,n)=gl(1)+0.5*mu2*(gl(2)-gl(1))+dt*lambdav1*(0.5+0.5*tanh(1+U(1)))*gr(1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z(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,n)=gl(J+1)+0.5*mu2*(gl(1)-gl(J+1))+dt*lambdav1*(0.5+0.5*tanh(1+U(J+1)))*gr(J+1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z(J+1)))*gl(J+1)+dt*beta*fl(J+1)*(gr(J+1)+gl(J+1))-dt*alpha*gl(J+1);
else
KU = @(x) 1/(0.05*sqrt(2*pi))*exp(-0.5*(x/0.05).^2 );
y3 = @(x) -b*KU(x)*(vr+vl)+d*KU(x)*ur;
y4 = @(x) -b*KU(x)*(vr+vl)+d*KU(x)*ul;
KV = @(x) 1/(0.01*sqrt(2*pi))*exp(-0.5*(x/0.01).^2 );
F3 = @(x) -bV*KV(x)*(vr+vl)+dV*KV(x)*ur;
F4 = @(x) -bV*KV(x)*(vr+vl)+dV*KV(x)*ul;
for i=1:J+1
x=xr+(i-1)*dx;
end
ynum3 = y3(x);
ynum4 = y4(x);
I1 = trapz(x,ynum3);
W1 = trapz(x,ynum4);
Fnum3 = F3(x);
Fnum4 = F4(x);
Z1 = trapz(x,Fnum3);
U1 = trapz(x,Fnum4);
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)))*ul(j,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+W1(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)))*ur(j,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+I1(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)))*vl(j,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+U1(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)))*vr(j,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z1(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)))*ul(J+1,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+W1(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)))*ul(1,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+W1(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)))*ur(1,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+I1(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)))*ur(J+1,n-1)-dt*lambdau2*(0.5*+0.5*tanh(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)))*vl(J+1,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+U1(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)))*vl(1,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+U1(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)))*vr(1,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z1(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)))*vr(J+1,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z1(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
%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
Index exceeds the number of array elements. Index must not exceed 1.
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)
%%%%%%%%%%%%

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Sep. 2022
I = trapz(x,ynum1);
That is going to return a scalar.
ur(j,n)=fr(j)-0.5*mu1*(fr(j)-fr(j-1))+dt*lambdau2*(0.5+0.5*tanh(1+I(j)))*fl(j)-dt*lambdau1*(0.5+0.5*tanh(1+W(j)))*fr(j)-dt*beta*fr(j)*(gr(j)+gl(j))+dt*0.5*alpha*(gr(j)+gl(j));
That requires I(j) which is indexing the scalar I.
Perhaps you wanted cumtrapz() instead of trapz() ?
  4 Kommentare
Walter Roberson
Walter Roberson am 19 Sep. 2022
You are not computing f at each of those locations.
Walter Roberson
Walter Roberson am 19 Sep. 2022
The below code computes the first of the two plots. It was not obvious to me what the difference was between the two parts. You can take this code and adjust it to suit whatever your second plot does differently than your first plot.
Note that I guessed that you should use cumtrapz() instead of trapz() -- it is the only thing that made sense considering the indexing you were doing.
Note that Nt (number of time steps) was cut down by a factor of 10. This was done so that you could see some progress: with the original Nt the computation time is long enough that you would have definite grounds to wonder whether anything is happening with the code.
xl=0; xr=1; %domain[xl,xr]
J=100; % J: number of dividion for x
dx=(xr-xl)/J; % dx: mesh size
tf=1000; % final simulation time
Nt=10000; %Nt=100000; %Nt:number of time steps
dt=tf/Nt;
c=50;
s=600; % paremeter for the solution
lambdau1=0.0004; %turning rate of susp(right)
lambdau2=0.0002; %turning rate of susp(left)
lambdav1=0.0003; %turning rate of infect(right)
lambdav2=0.0001; %turning rate of infect(left)
beta=0.1; % transimion rate
alpha=0.0; %recovery/death rate
b=0.1;
d=0.5;
bV=0.3;
dV=0.1;
au=0.0003; % advection speed - susceptible
av=0.0001; % advection speed - infected
A=0.01;
mu1=au*dt/dx;
mu2=av*dt/dx;
w = linspace(xl,xr,J);
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
k=6*pi/(xr-xl);
fr=0+0.01*(1+sin(k*x)); %dimension f(1:J+1)initial conditions of right moving suscp
fl=0+0.01*(1+sin(k*x)); %initial conditions of left moving suscep
gr=0.25+0.01*(1+sin(k*x)); %initial conditions of infected
gl=0.75+0.01*(1+sin(k*x));
g=gr+gl;
%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);
u_ex = zeros(J+1,Nt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tt=0:tf/(Nt-1):tf;
[T,X]=meshgrid(tt,x);
%%%%%%%%%%%%%%%%%%%%%%%%
KU = @(x) 1 ./ (0.05 .* sqrt(2*pi)) .* exp(-0.5*(x./0.05).^2 );
bKU = @(x) -b .* KU(x) .* g;
dKU = @(x) d .* KU(x);
y1 = @(x) bKU(x) + dKU(x) .* fr;
y2 = @(x) bKU(x) + dKU(x) .* fl;
KV = @(x) 1 ./ (0.01 .* sqrt(2*pi)) .* exp(-0.5*(x./0.01).^2 );
bKV = @(x) -bV .* KV(x) .* g;
dKV = @(x) dV .* KV(x);
F1 = @(x) bKV(x) + dKV(x) .* fr;
F2 = @(x) bKV(x) + dKV(x) .* fl;
ynum1 = y1(x);
ynum2 = y2(x);
I = cumtrapz(x,ynum1);
W = cumtrapz(x,ynum2);
Fnum1 = F1(x);
Fnum2 = F2(x);
Z = cumtrapz(x,Fnum1);
U = cumtrapz(x,Fnum2);
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
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)))*fl(j)-dt*lambdau1*(0.5+0.5*tanh(1+W(j)))*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)))*fr(j)-dt*lambdau2*(0.5+0.5*tanh(1+I(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+Z(j)))*gl(j)-dt*lambdav1*(0.5+0.5*tanh(1+U(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)))*gr(j)-dt*lambdav2*(0.5*+0.5*tanh(1+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)))*fl(J+1)-dt*lambdau1*(0.5+0.5*tanh(1+W(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+I(1)))*fl(1)-dt*lambdau1*(0.5+0.5*tanh(1+W(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)))*fr(1)-dt*lambdau2*(0.5*+0.5*tanh(1+I(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)))*fr(J+1)-dt*lambdau2*(0.5*+0.5*tanh(1+I(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,n)=gr(J+1)-0.5*mu2*(gr(J+1)-gr(J))+dt*lambdav2*(0.5+0.5*tanh(1+Z(J+1)))*gl(J+1)-dt*lambdav1*(0.5+0.5*tanh(1+U(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,n)=gr(1)-0.5*mu2*(gr(1)-gr(J))+dt*lambdav2*(0.5+0.5*tanh(1+Z(1)))*gl(1)-dt*lambdav1*(0.5+0.5*tanh(1+U(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,n)=gl(1)+0.5*mu2*(gl(2)-gl(1))+dt*lambdav1*(0.5+0.5*tanh(1+U(1)))*gr(1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z(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,n)=gl(J+1)+0.5*mu2*(gl(1)-gl(J+1))+dt*lambdav1*(0.5+0.5*tanh(1+U(J+1)))*gr(J+1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z(J+1)))*gl(J+1)+dt*beta*fl(J+1)*(gr(J+1)+gl(J+1))-dt*alpha*gl(J+1);
else
KU = @(x) 1 ./ (0.05 .* sqrt(2*pi)) .* exp(-0.5*(x./0.05).^2 );
bKU2 = @(x) -b .* KU(x).' .* (vr+vl);
dKU = @(x) d .* KU(x);
y3 = @(x) bKU2(x) + dKU(x).' .* ur;
y4 = @(x) bKU2(x) + dKU(x).' .* ul;
KV = @(x) 1 ./ (0.01 .* sqrt(2*pi)) .* exp(-0.5*(x./0.01).^2 );
bKV2 = @(x) -bV .* KV(x).' .* (vr+vl);
dKV = @(x) dV .* KV(x);
F3 = @(x) bKV2(x) + dKV(x).' .* ur;
F4 = @(x) bKV2(x) + dKV(x).' .* ul;
ynum3 = y3(x);
ynum4 = y4(x);
I1 = cumtrapz(x,ynum3);
W1 = cumtrapz(x,ynum4);
Fnum3 = F3(x);
Fnum4 = F4(x);
Z1 = cumtrapz(x,Fnum3);
U1 = cumtrapz(x,Fnum4);
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)))*ul(j,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+W1(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)))*ur(j,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+I1(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)))*vl(j,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+U1(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)))*vr(j,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z1(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)))*ul(J+1,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+W1(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)))*ul(1,n-1)-dt*lambdau1*(0.5+0.5*tanh(1+W1(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)))*ur(1,n-1)-dt*lambdau2*(0.5*+0.5*tanh(1+I1(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)))*ur(J+1,n-1)-dt*lambdau2*(0.5*+0.5*tanh(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)))*vl(J+1,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+U1(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)))*vl(1,n-1)-dt*lambdav1*(0.5+0.5*tanh(1+U1(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)))*vr(1,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z1(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)))*vr(J+1,n-1)-dt*lambdav2*(0.5*+0.5*tanh(1+Z1(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
%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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu General Physics finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by