function coeffs = projLegendre( max_deg, f, num_samples)
%
% coeffs = projLegendre( max_deg, f, num_samples)
%
% Project the function f into Legendre polynomials up to degree max_deg. 

  defval( 'num_samples', 100000);

  % memory for basis function coefficients
  coeffs = zeros( max_deg+1, 1);

  % generate random numbers for Monte Carlo integration
  x = (rand( [num_samples, 1]) * 2.0) - 1.0;
  
  % evaluate f at locations
  f_vals = f(x);
  
  % do for all Legendre polynomials
  for( deg = 0 : max_deg)

    % evaluate Legendre polynomial (and extract the one relevant for us)
    legendre_vals = legendre( deg, x, 'norm');
    if( 0 == deg) 
      legendre_vals = legendre_vals(:,1);
    else 
      legendre_vals = legendre_vals(1,:);
    end
    
    coeffs(deg+1) = 2/num_samples .* dot( legendre_vals, f_vals);
    
  end


end