2
我知道Matlab内置函数用于确定关联勒让德函数。我想计算勒让德多项式,它们是那些特定的情况。我已经为这个任务编写了自己的代码,并且我已经与Matlab内置函数进行了比较。下面是比较代码:勒让德多项式的Matlab代码优化
function op
t1 = zeros(1,100);
t2 = zeros(1,100);
P1 = zeros(1,10);
for m = 1:100
tic;
% It is neccessary a for loop for the first ten terms (m =1,...,10) of
% Legendre polynomial with legendre matlab built-in function
for i = 1:10
A = legendre(i,0);% legendre function determines the associated
% Legendre functions
P1(i) = A(1,1);% Legendre polynomials are the first row of A
end
t1(m) = toc;
tic;
% My own function determines the first ten terms at a time
P2 = legendrep2(0,10);
t2(m) = toc;
end
% Mean time using the Matlab built-in legendre functions
t1_mean = mean(t1),
% Mean time using my own custom legendre polynomial function
t2_mean = mean(t2),
function [Pl] = legendrep2(gamma,fin_suma)
Pl = zeros(1,fin_suma);
Pl(1) = gamma;
Pl(2) = 0.5*(3*gamma*Pl(1)-1);
for j =3:fin_suma;
Pl(j) = ((2*j-1)*gamma*Pl(j-1)-(j-1)*Pl(j-2))/j;
end
end
end
这是我的结果:
t1_mean =
0.001621042906210
t2_mean =
7.536710452587590e-006
所以,我想知道是否有改善我的代码(legendrep2功能)更加任何可能性。
运行时是否是一个问题,因为您需要非常高的多项式顺序或因为您需要多次评估该函数? – 2012-01-08 18:07:16
是的,我需要多次评估此功能。感谢您的评论。 – julian 2012-01-08 18:51:21