function lorenz_ode_sensitivity_test ( )

%*****************************************************************************80
%
%% lorenz_ode_sensitivity_test() shows sensivitity of Lorenz ODE solutions.
%
%  Discussion:
%
%    This program demonstrates that small changes in the initial 
%    condition for the Lorenz equations can result in enormous changes
%    in the subsequent solution trajectories.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    26 January 2020
%
%  Author:
%
%    Original Python version by John Cook.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    John Cook,
%    A different view of the Lorenz system,
%    https://www.johndcook.com/blog/
%    26 January 2020.
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'lorenz_ode_sensitivity_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Demonstrate sensitive dependence on initial data for the Lorenz system.\n' );
  fprintf ( 1, '  This program is based on the work of John D Cook.\n' );

  a = 0.0;
  b = 40.0;

  f = @(t,y) lorenz_deriv(t,y);
  tspan = [ a, b ];
  ic1 = [1;1;1];
  ic2 = [1;1;1.00001];
  sol1 = ode45 ( f, tspan, ic1 );
  sol2 = ode45 ( f, tspan, ic2 );
%
%  Evaluate both solutions at common times t.
%
  t = linspace ( a, b, 4001 );
  xyz1 = deval ( sol1, t );
  xyz2 = deval ( sol2, t );
%
%  Plot phase portrait (x1,z1).
%
  plot ( xyz1(1,:), xyz1(3,:) );
  xlabel ( "<-- x1(t) -->" );
  ylabel ( "<-- z1(t) -->" );
  filename = "lorenz_ode_sensitivity_x1z1.png";
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
  print ( '-dpng', filename );
%
%  Plot evolution (t,x1) and error evolution (t,x1-x2).
%
  subplot ( 211 );
  plot ( t, xyz1(1,:) );
  grid ( 'on' );
  xlabel ( "<-- t -->" );
  ylabel ( "x_1(t)" );

  subplot ( 212 );
  plot ( t, xyz1(1,:) - xyz2(1,:) );
  grid ( 'on' );
  xlabel ( "<-- t -->" );
  ylabel ( "x_1(t) - x_2(t)" );

  filename = "lorenz_ode_sensitivity_xdif.png";
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
  print ( '-dpng', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' )
  fprintf ( 1, 'lorenz_ode_sensitivity_test():\n' )
  fprintf ( 1, '  Normal end of execution.\n' )
  timestamp ( );

  return
end
function dydt = lorenz_deriv ( t, y )

%*****************************************************************************80
%
%% lorenz_deriv() evaluates the right hand side of the Lorenz ODE.
%
%  Discussion:
%
%    The values of parameters sigma, rho and beta correspond to those used by
%    Lorenz in his original report.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 October 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T: the value of the independent variable.
%
%    real Y(M): the values of the dependent variables at time T.
%
%  Output:
%
%    real DYDT(M): the right hand side of the ODE.
%
  beta = 8.0 / 3.0;
  rho = 28.0;
  sigma = 10.0;

  dy1dt = sigma * ( y(2) - y(1) );
  dy2dt = y(1) * ( rho - y(3) ) - y(2);
  dy3dt = y(1) * y(2) - beta * y(3);

  dydt = [ dy1dt; dy2dt; dy3dt ];

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end

