function [ u, ut, ux, uxx ] = porous_medium_exact ( x, t )

%*****************************************************************************80
%
%% porous_medium_exact() evaluates an exact solution of the porous medium equation.
%
%  Discussion:
%
%    du/dt = Del^2 ( u^m )
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    19 May 2024
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Grigory Barenblatt,
%    On some unsteady fluid and gas motions in a porous medium,
%    Prikladnaya Matematika i Mekhanika (Applied Mathematics and Mechanics, 
%    Volume 16, Number 1, pages 67-78, 1952.
%
%    Rouben Rostamian,
%    Programming Projects in C 
%    for Students of Engineering, Science, and Mathematics,
%    SIAM, 2014,
%    ISBN: 978-1-611973-49-5
%
%  Input:
%
%    real x, t: the position and time.
%
%  Output:
%
%    real u, ut, ux, uxx: the values of the exact solution, its time
%    derivative, and its first and second spatial derivatives at (x,t).
%
  [ c, delta, m, t0, tstop ] = porous_medium_parameters ( );

  alpha =         1.0 / ( m - 1.0 );
  beta =          1.0 / ( m + 1.0 );
  gamma = ( m - 1.0 ) / ( 2.0 * m * ( m + 1.0 ) );

  bot = ( t + delta )^beta;
  factor = c - gamma * ( x / bot )^2;

  if ( 0.0 < factor )

    u = 1.0 / ( t + delta)^beta * factor^alpha;

    ut = 2.0 * alpha * beta * gamma * ( t + delta )^(-1.0-3.0*beta) ...
      * x^2 * factor^(alpha - 1.0) ...
      - beta * ( t + delta )^(-1.0-beta) * factor^alpha;

    ux = - 2.0 * alpha * gamma ...
      * ( t + delta )^(-3.0 * beta ) * x * factor^(alpha-1.0);

    uxx = 4.0 * ( alpha - 1.0 ) * alpha * gamma^2 ...
      * ( t + delta )^(-5.0*beta) * x^2 * factor^(alpha-2.0) ...
      - 2.0 * alpha * gamma * ( t + delta )^(-3.0 * beta ) * factor^(alpha-1.0);

  else

    u = 0.0;
    ut = 0.0;
    ux = 0.0;
    uxx = 0.0;

  return
end
