gpt4 book ai didi

勒让德多项式的 Matlab 代码优化

转载 作者:行者123 更新时间:2023-12-03 17:36:13 24 4
gpt4 key购买 nike

我知道 Matlab 具有用于确定相关 Legendre 函数的内置函数。我想计算勒让德多项式,这是这些多项式的特例。我为此任务编写了自己的代码,并与 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 函数)。

最佳答案

尝试使用 Matlab 分析器找出瓶颈在哪里:

Menu -> Desktop -> Profiler



这将帮助您集中注意力。

关于勒让德多项式的 Matlab 代码优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8777791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com