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 个)

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);
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 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by