Contents
clear all;
close all;
c=1;
delx=.01;
a=-3;
b=5;
x=a:delx:b;
J=(b-a)/delx;
delt=.01;
r=c*delt/delx;
nsteps=100;
tfinal=nsteps*delt;
u=f(x);
Forward difference
figure
for n=1:nsteps
v=u;
for j=1:J
u(j)=(1+r)*v(j)-r*v(j+1);
end;
if(n==1)
plot(x,u,'g-+');
end;
hold on;
if(n==nsteps)
plot(x,u,'r-s')
hold on
plot(x,sol(c,x,delt*nsteps));
end;
end;
title('The solution using a forward difference approx.')
xlabel('x','FontSize',14);
ylabel('u_{fwd}(x,t)','FontSize',14);
legend('u(x,t_1)', 'u(x,tfinal)','u_{exact}(x,tfinal)')
hold off;
Upwind scheme
u=f(x);
figure
for n=1:nsteps
v=u;
for j=2:J+1
u(j)=(1-r)*v(j)+r*v(j-1);
end;
if(n==1)
plot(x,u,'g-+');
end;
hold on;
if(n==nsteps)
plot(x,u,'r-s')
hold on
plot(x,sol(c,x,delt*nsteps));
end;
end;
title('The solution using an upwind difference approx.')
xlabel('x','FontSize',14);
ylabel('u_{uwd}(x,t)','FontSize',14);
legend('u(x,t_1)', 'u(x,tfinal)','u_{exact}(x,tfinal)')
hold off;
The solution with the method of characteristics
figure
plot(x,f(x),'r');
hold on
plot(x,sol(c,x,1));
hold off;
title('Initial solution and Char.solution u(x(t),t)');
xlabel('x','FontSize',14);
ylabel('u(x,t)','FontSize',14);
legend('f','u_{char}(x,tfinal)')