function snowfall_smoothed_plot ( )

%*****************************************************************************80
%
%% snowfall_smoothed_plot plots smoothed snowfall data.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 April 2019
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'snowfall_smoothed_plot:\n' );
  fprintf ( 1, '  MATLAB version\n' );
  fprintf ( 1, '  Read snowfall data\n' );
  fprintf ( 1, '  Smooth data using a 10 year moving average.\n' );
  fprintf ( 1, '  Plot the smoothed data.\n' );

  y = load ( 'snowfall_data.txt' );
  year = y(:,1);
  inches = y(:,10);
%
%  Replace inches by a 10 year moving average.
%
  n = length ( inches );
  z = r8vec_smooth ( n, inches, 10 );

  plot ( year, z, 'Linewidth', 2 );
  grid on
  xlabel ( '<-- Year -->', 'Fontsize', 16 )
  ylabel ( '<-- Total Snowfall (inches) -->', 'Fontsize', 16 )
  title ( 'Yearly Snowfall in Michigan, 10-Year Moving Average', 'Fontsize', 16 )
%
%  Save the graphics in a file.
%
  filename = 'snowfall_smoothed_plot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'snowfall_smoothed_plot:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
function z = r8vec_smooth ( n, x, s )

%*****************************************************************************80
%
%% r8vec_smooth smooths an R8VEC.
%
%  Discussion:
%
%    An R8VEC is a vector of R8's.
%
%    Except for the beginning and ending entries, the vector values
%    are replaced by averages of 2*S+1 neighbors.
%
%  Example:
%
%    S = 2
%
%    Z(1)   =                     X(1)
%    Z(2)   = (          X(1)   + X(2)   + X(3) ) / 3
%    Z(3)   = ( X(1)   + X(2)   + X(3)   + X(4)   + X(5) ) / 5
%    Z(4)   = ( X(2)   + X(3)   + X(4)   + X(5)   + X(6) ) / 5
%    ...
%    Z(N-2) = ( X(N-4) + X(N-3) + X(N-2) + X(N-1) + X(N) ) / 5
%    Z(N-1) =          ( X(N-2) + X(N-1) + X(N) ) / 3
%    Z(N) =                       X(N)
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 April 2019
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the dimension of X.
%
%    Input, real X(N), the vector to be smoothed.
%
%    Output, real Z(N), the smoothed vector.
%
  z = zeros ( n, 1 );

  for j = 1 : s
    z(j) = sum ( x(1:2*j-1) ) / ( 2 * j - 1 );
  end

  for j = s + 1 : n - s
    z(j) = sum ( x(j-s:j+s) ) / ( 2 * s + 1 );
  end

  for j = s : -1 : 1
    z(n+1-j) = sum ( x(n+1-(2*j-1):n) ) / ( 2 * j - 1 );
  end

  return
end
