I would like to improve this function to reduce the run time and optimize it.
function [Pl,Plm] = funlegendre(gamma)
Plm = zeros(70,71);
Pl = zeros(70,1);
P0 = 1;
Pl(1) = gamma;
Plm(1,1) = sqrt(1-gamma^2);
for L=2:70
for M=0:L
if(M==0)
if (L==2)
Pl(L) = ((2*L-1)*gamma*Pl(L-1)-(L-1)*P0)/L;
else
Pl(l) = ((2*l-1)*gamma*Pl(l-1)-(l-1)*Pl(l-2))/l;
end
elseif(M<L)
if(L==2)
if(M==1)
Plm(L,M) = (2*L-1)*Plm(1,1)*Pl(L-1);
else
Plm(L,M) = (2*M-1)*Plm(1,1)*Plm(L-1,m-1);
end
else
if(M==1)
Plm(L,M) = Plm(L-2,m) + (2*L-1)*Plm(1,1)*Pl(L-1);
else
Plm(L,m) = Plm(L-2,m) + (2*L-1)*Plm(1,1)*Plm(L-1,M-1);
end
end
elseif(M==L)
Plm(L,M) = (2*L-1)*Plm(1,1)*Plm(L-1,L-1);
else
Plm(L,M) = 0;
end
end
end
Pl = sparse(Pl);
Plm = sparse(Plm);
end