3
$\begingroup$

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.

$\endgroup$
2
  • 4
    $\begingroup$ You are solving the linear advection equation $u_t + a u_x = 0$ using an upwind scheme (backward difference) in space and explicit Euler (forward difference) in time. This particular combination of problem, space and time discretization is quite special for reasons that do not generalize to other problems: The error is zero (up to rounding errors) if $a \Delta t/\Delta x = 1$ and tends to grow as this quantity departs from unity. See my answer to scicomp.stackexchange.com/questions/31375/… for an explanation. $\endgroup$
    – ekkilop
    Commented Nov 8, 2022 at 15:22
  • $\begingroup$ Yes that can be the point. Thanks a lot $\endgroup$
    – Cristie
    Commented Nov 16, 2022 at 15:33

1 Answer 1

1
$\begingroup$

Perhaps not exactly what you were looking for, but maybe it can give you more insights into the behavior of the solution depending on the solver method - MATLAB has ODE (ordinary differential equation) solvers collection (see here), and if you rewrite your PDE as $$u_t=-au_x$$ and then discretize the right-hand side as you did in your original code (with the boundary condition of choice), MATLAB ODE solver should dynamically tune the time-step along the way. You can even track the error and the entire convergence procedure to see how the algorithm deduces the optimal time step.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.