How to get diff from legendreP faster?
이전 댓글 표시
I am trying to get differential from LegendreP, but it took me long time, for some data more that 12 hours !!!
syms mn
for n=1:1025
expr(n)=legendreP(n-1,cos(mn));
end
for n=1:1025
mmm(n)=diff(expr(n),mn,1);
end
teta=0:0.01:pi;
L=zeros(1025,315);
for i=1:length(teta)
L(:,i)=subs(mmm,teta(i));
end
LegendreP(n,cos(teta)) and first diff were considered. How can I have this one faster???? Thanks
댓글 수: 1
Walter Roberson
2015년 11월 30일
I do not know at the moment why it might be slow. For whatever it is worth, mmm(n) should be
-n*(legendreP(n-1, cos(mn))*cos(mn) - legendreP(n, cos(mn)))/sin(mn)
This already takes into account that expr(n) is defined in terms of legendreP(n-1,cos(mn))
답변 (1개)
Walter Roberson
2015년 11월 30일
I suggest trying
syms mn N
LPD = diff( legendreP(N, cos(mn)), mn );
teta = 0:0.01:pi;
L = zeros(1025,length(teta));
for n = 1:1025
mmm = matlabFunction( subs(LPD, N, n-1), mn );
L(n, :) = mmm(teta);
end
댓글 수: 4
Walter Roberson
2015년 12월 1일
You could try
syms mn N
LPD = diff( legendreP(N, cos(mn)), mn );
teta = 0:0.01:pi;
L = zeros(1025,length(teta));
for n = 1:1025
mmm = subs(LPD, N, n-1);
L(n, :) = double(subs(mmm, mn, teta));
end
Elvis Alexander Agüero Vera
2023년 1월 16일
In Matlab2020a, your last try throws
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 698)
Xstr = mupadmex('symobj::double', S.s, 0);
Elvis Alexander Agüero Vera
2023년 1월 16일
And the main comment throws
Error using symengine
Unable to generate code for partial derivative 'D([2], orthpoly::legendre)(12,
cos(mn))'.
Error in sym/matlabFunction>mup2mat (line 404)
res = mupadmex('symobj::generateMATLAB',r.s,ano,spa,0);
Error in sym/matlabFunction>mup2matcell (line 382)
c = cellfun(@mup2mat,c,anonymousArgs,sparseArgs,'UniformOutput',false);
Error in sym/matlabFunction (line 188)
body = mup2matcell(funs, opts.Sparse);
Walter Roberson
2023년 1월 16일
Interesting, I am now seeing the same results that you post.
At the moment I do not recall whether I tested back in 2015; I will check if I have time.
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!