I am struggling with this assignment. I have to write an upwind scheme for the following PDE:
$$u_t+a Du=0 \quad\mathrm{on}\;(-1,3)$$
$a$ is said to be positive, the initial condition is $\sin(2\pi x)$ and periodic condition on the inflow border.
It seemed easy to me so I wrote this on Matlab but I am struggling with computing the order. It seems as the error tends to grow if I take a smaller step and it doesn't make any sense, so I guess there must be something wrong but I can't figure out where is the problem.
Here is my code in Matlab:
function [u,u_es,norm_inf, norm_1]=upwind1(a,u0,xmin,xmax,T,dx,dt,scelta,choice_a)
lambda=dt/dx;
N_T=ceil(T/dt);
M=ceil((xmax-xmin)/dx);
u_es=zeros(M+1,N_T+1);
u(:,1)=u0;
u_es(:,1)=u0;
x=xmin:dx:xmax;
t=0:dt:T;
switch scelta
case 1
%positive a
for k=2:N_T+1
u(2:M+1,k)=u(2:M+1,k-1)-lambda*a*(u(2:M+1,k-1)-u(1:M,k-1));
u(1,k)=u(M+1,k);
for i=1:M+1
u_es(i,k)=sol_esatta(x(i),t(k),a,scelta,xmax);
end
end
end
err=abs(u(:,end)-u_es(:,end));
norm_inf=max(err);
norm_1=sum(err*dx);
end
The function gets as input a vector u0
which is the initial value at each x
of the discretization.
function g=sol_esatta(x,t,a,scelta,xmax)
switch scelta
case {1,2}
g=f((x-a*t),scelta);
end
end
I have different tests so I don't mind about scelta
which is the flag for the problem.