clear;
clc;

xl=0;
xr=10; 
tfinal=10;
dx=0.2;
dt=0.01;

nx=(xr-xl)/dx+1;
nt=tfinal/dt+1;

a=0.5;
d1=1;
fsize=15;

s=d1*dt/(dx*dx);

xmesh=linspace(xl,xr,nx);
tmesh=linspace(0,tfinal,nt);

U=zeros(nx,nt);

U(:,1)=0.5+0.1*sin(xmesh');

A=zeros(nx-2,nx-2);
A(1,1)=1+2*s;
A(1,2)=-s;
A(nx-2,nx-3)=-s;
A(nx-2,nx-2)=1+2*s;
for i=2:nx-3
    A(i,i)=1+2*s;
    A(i,i-1)=-s;
    A(i,i+1)=-s;
end

AA=inv(A);

for i=1:nt-1   %%for time t
    for j=2:nx-1 %%for space x
        F(j-1)=U(j,i)+dt*a*U(j,i)-dt*U(j,i)^2;
    end
    Utemp=AA*(F');
    U(1,i+1)=0;
    U(nx,i+1)=0;
    for j=2:nx-1
       U(j,i+1)=Utemp(j-1);
    end
end

surf(tmesh,xmesh,U,'EdgeColor','none');
colormap Jet
colorbar
ylabel('Space x','fontsize',fsize)
xlabel('Time t','fontsize',fsize)
zlabel('u concentration','fontsize',fsize)
view([-1 -1 1])

figure;
surf(tmesh,xmesh,U,'EdgeColor','none');
colormap Jet
colorbar
ylabel('Space x','fontsize',fsize)
xlabel('Time t','fontsize',fsize)
view([0 0 1])

figure;
plot(xmesh,U(:,nt));
colormap Jet
colorbar
