%clear;

%Number of wavelengths in the Box;
M=3;
%Nondimensional advective speed C (or CFL #)
C=1.0;  
%Number of Wave Periods to Run
NT=5;
%Wave Amplitude
A=1;
%Gaussian Envelope? (1 yes, 0 no)
G=1;
%V Envelope Multiplier (i.e., -1 for backward, 1 for forward)
B=0;
%Phase shift of second wave
phishift=0;

%Spatial step size and temporal step duration
dx=1;
%Uncomment to set dt for CFL=1
%dt=dx./C;
%Uncomment to set dt for 1, so that CFL=C (Comment for CFL=1)
dt=1;
%Calculate and show CFL
CFL=C.*dt./dx

%Max domain size in space, and evolution duration time
Imin=0;
Imax=100;
Xmin=dx.*(Imin-1);
Xmax=dx.*(Imax+1);
Xdomain=(Imax-Imin+1).*dx;
Nmin=0;
Nmax=Xdomain.*NT./C;
Tmin=dt.*Nmin;
Tmax=dt.*Nmax;

%Domain X-axis Reference
x=[Xmin:dx:Xmax];

%Open Figure
figure(1);

%Calculate later times t...
for n=Nmin:1:Nmax   

    
    %Calculate numerical solution (or set to initial value)
    if n==Nmin  %Set initial value at initial timestep
        %Define analytical solution, also used for initial value
        Ua=A*sin(2*pi*(x./(Xdomain./M))-2*pi*((n*dt)./(Xdomain./(C*M))));
        Va=A*sin(2*pi*(x./(Xdomain./M))-2*pi*((n*dt)./(Xdomain./(C*M)))-phishift);
        if G==1 
            Ua=Ua.*exp(-.5*((x-Xmax/2).^2)./(Xdomain/10)^2);
            Va=B.*Va.*exp(-.5*((x-Xmax/2).^2)./(Xdomain/10)^2);
        end;
        
        UL=Ua;  %Lax Init.
        VL=Va;
        ULW=Ua; %Lax-Wendroff Init.
        VLW=Va;
        
    else        %Evolve solution at later timesteps
        
        %Initialize temporary U(n+1) from U(n)
        U1L=UL;
        U1LWa=ULW;
        V1L=VL;
        V1LWa=VLW;
        
        for im=Imin:1:Imax
            i=im+2;  %Convert to MATLAB Index
            %Obtain U(n+1) for Lax Method
            U1L(i)=(1./2).*(UL(i+1)+UL(i-1))-(C.*VL(i+1)-C.*VL(i-1)).*dt./(2.*dx);
            V1L(i)=(1./2).*(VL(i+1)+VL(i-1))-(C.*UL(i+1)-C.*UL(i-1)).*dt./(2.*dx);  
        end;
        for im=Imin-1:1:Imax
            i=im+2;  %Convert to MATLAB Index
            %Obtain U(n+1/2) for Lax-Wendroff 2-Step Method
            U1LWa(i)=(1./2).*(ULW(i+1)+ULW(i))-(C.*VLW(i+1)-C.*VLW(i)).*dt./(2.*dx);       
            V1LWa(i)=(1./2).*(VLW(i+1)+VLW(i))-(C.*ULW(i+1)-C.*ULW(i)).*dt./(2.*dx);     
        end;

        %Perform second Lax-Wendroff step:
        %Update results for n+1/2 (Lax-Wendroff)
        ULWa=U1LWa; 
        VLWa=V1LWa; 
        %Initialize Array
        U1LW=ULWa;
        V1LW=VLWa;
        for im=Imin:1:Imax
            i=im+2;  %Convert to MATLAB Index
            %Obtain U(n+1) for Lax-Wendroff 2-Step Method
            U1LW(i)=ULW(i)-(C.*(VLWa(i)-VLWa(i-1))).*(dt./dx);   
            V1LW(i)=VLW(i)-(C.*(ULWa(i)-ULWa(i-1))).*(dt./dx);            
        end;
        
        %Assign value at right boundary, such that domain is "periodic"
        %This means, physically, that whatever goes out on the right will 
        %come back in on the left.
        %Update boundaries for Lax 
        UL=U1L;
        UL(Imin+1)=U1L(Imax+2);
        UL(Imax+3)=U1L(2);
        VL=V1L;
        VL(Imin+1)=V1L(Imax+2);
        VL(Imax+3)=V1L(2);
        %Update boundaries for Lax-Wendroff
        ULW=U1LW;
        ULW(Imin+1)=U1LW(Imax+2);
        ULW(Imax+3)=U1LW(2);
        VLW=V1LW;
        VLW(Imin+1)=V1LW(Imax+2);
        VLW(Imax+3)=V1LW(2); 
    end;
    
    %Show Results    
    plot(x,Ua,'k:',x,ULW,'k--',x,UL,'k-',x,Va,'b:',x,VLW,'b--',x,VL,'b-');
    axis([0 100 -2.*A 2.*A]);
    xlabel('Horizontal Position');
    ylabel('Amplitude');
    title(['Lax (Solid), Lax-Wendroff(Dashed) and Initial Condition (Dotted) @ t=',num2str(n.*dt), ', n=',num2str(n),'']);
    pause(0.005); 
end;
