【发布时间】:2016-12-13 08:54:36
【问题描述】:
我想使用 Gauss-Legendre 求积来评估双积分。我想出了以下代码
m=5000;
%generate weights and abscissas
[wx,xx]=leg(-1,1,m);
[wy,xy]=leg(-1,1,m);
%define function
psi=@(x,y) hypergeom(-1./4,3./2,x.^2.*y.^6);
%integrate with respect to x
intx=zeros(1,m);
for num=1:m
intx(num)=sum(wx.*psi(xx,yx(num)));
end
sum(wy.*intx)
我使用符号 leg(x1, x2, m) 来生成权重和横坐标。
还有其他方法可以让这段代码运行得更快吗?
【问题讨论】:
-
为什么不在for循环之外定义psi?但这仍然不应该是您的瓶颈。您是否尝试过分析您的代码?您可以通过“运行和时间”按钮访问它
-
当我将 psi 放在 for lopp 之外时,会发生错误。它说可变 y 是未定义的
-
尝试了你的建议,但是代码仍然运行得很慢
-
在scicomp.stackexchange.com提出此类问题可能会获得更多帮助
-
我真的很感激。谢谢!
标签: matlab numerical-methods numerical-integration