2012-01-08 24 views
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功能)更加任何可能性。

+0

運行時是否是一個問題,因爲您需要非常高的多項式順序或因爲您需要多次評估該函數? – 2012-01-08 18:07:16

+0

是的,我需要多次評估此功能。感謝您的評論。 – julian 2012-01-08 18:51:21

回答

2

嘗試Matlab的探查,找出瓶頸是:

菜單 - >桌面 - >探查

這將幫助你集中注意力。