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

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
Walter Roberson 2015년 11월 30일

0 개 추천

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

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
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);
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
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.

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2015년 11월 30일

댓글:

2023년 1월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by