function schroedinger_nonlinear_ode45 ( nx )

%*****************************************************************************80
%
%% schroedinger_nonlinear_ode45() solves the nonlinear Schroedinger PDE using ode45.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 April 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Willem Hundsdorfer, Jan Verwer,
%    Numerical solution of time-dependent advection-diffusion-reaction equations,
%    Springer, 2003
%    ISBN: 978-3-662-09017-6
%
%  Input:
%
%    integer NX: the number of spatial nodes.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'schroedinger_nonlinear_ode45\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Solve the nonlinear Schroedinger PDE using ode45()\n' );
%
%  Get the parameter values.
%
  [ alpha, c1, c2, delta, gamma, t0, xmin, xmax ] = ...
    schroedinger_nonlinear_parameters ( );

  fprintf ( 1, '  Solve the nonlinear Schroedinger equation in 1D,\n' );
  fprintf ( 1, '    du/dt = i uxx + i gamma |u|^2 u\n' );
  fprintf ( 1, '  over the spatial interval:\n' );
  fprintf ( 1, '    %g <= x <= %g\n', xmin, xmax );
  fprintf ( 1, '  and time interval\n' );
  fprintf ( 1, '     %g <= t <= 44.0\n', t0 );
  fprintf ( 1, '  with Neumann boundary conditions:\n' );
  fprintf ( 1, '    u''(%g) = 0.0\n', xmin );
  fprintf ( 1, '    u''(%g) = 0.0\n', xmax );
  fprintf ( 1, '  and parameter:\n' );
  fprintf ( 1, '    gamma = %g\n', gamma );
  fprintf ( 1, '  and initial condition\n' );
  fprintf ( 1, '    u(0,x) = sqrt(2 alpha/gamma) * \n' );
  fprintf ( 1, '    ( exp(0.5 i c1 x       ) sech( sqrt(alpha) x )\n' );
  fprintf ( 1, '    + exp(0.5 i c2 (x-delta) sech( sqrt(alpha)(x-delta) ).\n' );
  fprintf ( 1, '  and NX equally spaced nodes in X,\n' );
  fprintf ( 1, '  using ode45().\n' );
%
%  Set the X values.
%
  x = linspace ( xmin, xmax, nx );
  dx = ( xmax - xmin ) / ( nx - 1 );
%
%  Set the T values (for display only).
%
  tmin = t0;
  tmax = 44.0;
  tspan = [ tmin, tmax ];
  
  fprintf ( 1, '  Use %d nodes, with dx=%g in space interval [%g,%g]\n', ...
    nx, dx, xmin, xmax );
  fprintf ( 1, '  Time interval is [%g,%g]\n', tmin, tmax );
%
%  Set the initial condition.
%
  u0 = schroedinger_nonlinear_initial ( x, t0 );
%
%  Call ODE45 to compute the solution.
%
  [ t, u ] = ode45 ( @(t,u)schroedinger_nonlinear_deriv(t,u,x), tspan, u0 );

  nt = length ( t );
  fprintf ( 1, '  Number of (variable size) time steps = %d\n', nt );
%
%  Figure 1: |u(t)|
%
  for j = 1 : nt

    figure ( 1 );
    clf ( );
    hold ( 'on' );
      plot ( x, abs ( u(j,:) ), 'b-', 'linewidth', 2 );
      plot ( [x(1),x(nx)], [0.0,0.0], 'k-', 'linewidth', 2 ); 
    hold ( 'off' );
    grid ( 'on' );
    label = sprintf ( '|u|, schroedinger nonlinear, ode45, t=%.3f', t(j) );
    title ( label );
    xlabel ( '<--- X --->' );
    ylabel ( '<--- |U(X)| --->' );
    axis ( [x(1), x(nx), 0.0, 2.0 ] )

    if ( j == 1 )
      filename = 'schroedinger_nonlinear_ode45_initial.png';
      print ( '-dpng', filename );
      fprintf ( 1, '  Graphics saved as "%s"\n', filename );
    elseif ( j == floor ( nt / 2 ) )
      filename = 'schroedinger_nonlinear_ode45_midway.png';
      print ( '-dpng', filename );
      fprintf ( 1, '  Graphics saved as "%s"\n', filename );
    elseif ( j == nt )
      filename = 'schroedinger_nonlinear_ode45_final.png';
      print ( '-dpng', filename );
      fprintf ( 1, '  Graphics saved as "%s"\n', filename );
    end

    pause ( 0.01 );

  end
%
%  Figure 2: Conservation.
%
  h = zeros ( nt, 1 );
  for j = 1 : nt
    h(j) = schroedinger_nonlinear_conserved ( x, u(j,:) );
  end

  figure ( 2 );
  clf ( );
  hold ( 'on' )
    plot ( t, h, 'ro', 'linewidth', 3 );
    plot ( tspan, [h(1), h(1)], 'g--', 'linewidth', 3 );
    plot ( tspan, [0.0,0.0], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- T -->' );
  ylabel ( '<-- H(T) -->' );
  title ( 'conservation, schroedinger nonlinear, ode45' );
  legend ( 'computed', 'initial' );
  filename = 'schroedinger_nonlinear_ode45_conservation.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'schroedinger_nonlinear_ode45\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

