Code's optimization and speeding up (Legendre's polynomials and functions)
显示 更早的评论
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 个评论
Walter Roberson
2011-5-31
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 ?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Polynomials 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!