title: Matlab code optimization for Legendre polynomials
body: <p>I know Matlab has built-in functions for determining the associated Legendre functions. I want to compute the Legendre polynomials which are a particular case of those ones. I have written my own code for this task and I have compared with the Matlab built-in functions. Here is the comparison code:</p>
  
  <pre><code>function op
  t1 = zeros(1,100);
  t2 = zeros(1,100);
  P1 = zeros(1,10);
  for m = 1:100
  tic;
  % It is neccessary a for loop for the first ten terms (m =1,...,10) of 
  % Legendre polynomial with legendre matlab built-in function
  for i = 1:10
      A = legendre(i,0);% legendre function determines the associated 
      % Legendre functions
      P1(i) = A(1,1);% Legendre polynomials are the first row of A
  end
  t1(m) = toc;
  tic;
  % My own function determines the first ten terms at a time
  P2 = legendrep2(0,10);
  t2(m) = toc;
  end
  % Mean time using the Matlab built-in legendre functions
  t1_mean = mean(t1),
  % Mean time using my own custom legendre polynomial function
  t2_mean = mean(t2),
  
  function [Pl] = legendrep2(gamma,fin_suma)
  Pl = zeros(1,fin_suma);
  Pl(1) = gamma;
  Pl(2) = 0.5*(3*gamma*Pl(1)-1);
      for j =3:fin_suma;
          Pl(j) = ((2*j-1)*gamma*Pl(j-1)-(j-1)*Pl(j-2))/j;
      end
  end
  end
  </code></pre>
  
  <p>These are my results:</p>
  
  <pre><code>t1_mean =
       0.001621042906210
  t2_mean =
        7.536710452587590e-006
  </code></pre>
  
  <p>So, I would like to know if there is any possibility to improve my code (legendrep2 function) still more.</p>