N=100;
Tmax=1;
alpha=1;
h=0.01;
delt=0.005;
maxt=Tmax/delt;
c=alpha*delt/h;
u=zeros(N+1,maxt+1);
x=zeros(N+1,1);
for i=1:N+1
x(i)=(i-1)*h;
u(i,1)=sin(2*pi*x(i));
end
for k=1:maxt
u0 = u(N,k);
u(1,k+1)=u(1,k)-c/2*(u(2,k)-u0)+c^2/2*(u(2,k)-2*u(1,k)+u0);
for i=2:N
u(i,k+1) =u(i,k)-c/2*(u(i+1,k)-u(i-1,k))+(c^2)/2*(u(i+1,k)-2*u(i,k)+u(i-1,k));
end
uNp2 = u(2,k);
u(N+1,k+1) =u(N+1,k)-c/2*(uNp2-u(N,k))+c^2/2*(uNp2-2*u(N+1,k)+u(N,k));
end
plot(x,u(:,10),x,u(:,20),x,u(:,30),x,u(:,40),x,u(:,50),x,u(:,60),x,u(:,70))
Best Answer