function [ x, t, u ] = kdv_euler ( nx, nt )
%
%  function [ x, t, u ] = kdv_euler ( nx, nt )
%
%  KDV_EXP solves the Korteweg-deVries "soliton" wave equation.
%  It uses a simple Euler method in time, after discretizing the
%  space derivatives using a poor method.
%
%  The equation has the form:
%
%    Ut = 6 * U * Ux - Uxxx
%
%  Boundary conditions at the first and last few points are simply
%  that the value of U never changes from its initial value.
%
%  Because the space discretization is poor, and the time integration
%  method is of low accuracy, the results do not look very nice.
%
%  Try NX = 64, NT = 5, 10, then 15, 20 or so.

%
%  Initialize.
%
%  The spatial domain is fixed at [-6,6], so NX defines the value of DX.
%
xmax = 6.0;
xmin = - 6.0;
dx = ( xmax - xmin ) / ( nx - 1 );
x = linspace ( xmin, xmax, nx );
%
%  The time step is fixed at 0.002, so NT defines how far we advance in time.
%
dt = 0.002;
for it = 1 : nt
  t(it) = ( it - 1 ) * dt;
end
%
%  Define U to be an array of the right size, and all zero.
%
u = zeros ( nt, nx );
%
%  Set the initial condition at time zero.
%
it = 1;
for ix = 1 : nx
  u(it,ix) = 6.0 * ( sech ( x(ix) ) )^2;
end
%
%  Take a time step.
%
%  The boundary conditions of the equation are (crudely) handled by simply
%  forcing the first and last two entries of U to remain fixed.
%
for it = 2 : nt

  u(it,1) = u(it-1,1);

  for ix = 2 : nx-2
    u(it,ix) = u(it-1,ix) + dt * (...
      - 6.0 * u(it-1,ix) * ( u(it-1,ix+1) - u(it-1,ix) ) / dx ...
      - ( u(it-1,ix+2) - 3 * u(it-1,ix+1) + 3 * u(it-1,ix) - u(it-1,ix-1) ) / dx^3 );
  end

  u(it,nx-1) = u(it-1,nx-1);
  u(it,nx) = u(it-1,nx);

end
