Code's optimization and speeding up (Legendre's polynomials and functions)
Show older comments
Hi. 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
1 Comment
Walter Roberson
on 31 May 2011
Do you have the symbolic toolbox?
I am not sure what your code is doing, but it appears to be using the recursion relationship to form the legendre matrix. As such I get the impression that it might perhaps be what is implemented by http://www.mathworks.com/help/techdoc/ref/legendre.html ?
Accepted Answer
More Answers (0)
Categories
Find more on Polynomials in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!