I came with a the following code to evaluate a double integral using Gauss Legendre quadrature in MatLab
m=100;
%generate weights and abscissas
[wx,xx]=leg(-1,1,m);
[wtheta,xtheta]=leg(0,2.*pi,m);
%define function
psi=@(x,theta) hypergeom(-3./4,1./2,x.^2.*exp(1i.*theta));
%integrate with respect to x
intx=zeros(1,m);
for num=1:m
intx(num)=sum(wx.*psi(xx,xtheta(num)));
end
sum(wtheta.*intx)
I defined the function leg(x1,x2,m) in a different script to generate the weights and abscissas and I just call it in my code. My MatLab code runs significanlty slowly compared to NIntegrate of Mathematica.
I'd like to make my code faster because my idea of using MatLab is that it is faster than Mathematica. Is there any way I can make my code run faster?
Attached is a profile summary when i ran the code
mupadmex
consumes most of the time. I suggest to go deeper in your profiling. This routine is a MEX files, it means that it is compiled from a low-level language like Fortran or C which is faster so the optimization is already done. That also means that you can't improve this routine because it is already compiled. But I don't understand where it is called in your code, that's where you should look into. Why do you need this routine and what is its purpose ? $\endgroup$ – Coriolis Aug 8 '16 at 19:13leg
? If so, that might be the reason for your algorithm to be much slower. $\endgroup$ – nicoguaro♦ Aug 10 '16 at 13:28hypergeom
is slow. See math.stackexchange.com/q/478052/24717 $\endgroup$ – Memming Sep 9 '16 at 9:20