I came with a the following code to evaluate a double integral using Gauss Legendre quadrature in MatLab


%generate weights and abscissas

%define function
psi=@(x,theta) hypergeom(-3./4,1./2,x.^2.*exp(1i.*theta));

%integrate with respect to x
for num=1:m


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

Profile summary

  • $\begingroup$ Your profiling reveals that 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
    Commented Aug 8, 2016 at 19:13
  • $\begingroup$ I just clicked the run and time button because I wanted to know how long the code ran :) $\endgroup$
    – anonymous
    Commented Aug 8, 2016 at 22:39
  • $\begingroup$ Are you using symbolic algebra inside your function leg? If so, that might be the reason for your algorithm to be much slower. $\endgroup$
    – nicoguaro
    Commented Aug 10, 2016 at 13:28
  • $\begingroup$ @nicoguaro no it doesnt $\endgroup$
    – anonymous
    Commented Aug 10, 2016 at 14:50
  • $\begingroup$ Your hypergeom is slow. See math.stackexchange.com/q/478052/24717 $\endgroup$
    – Memming
    Commented Sep 9, 2016 at 9:20

1 Answer 1


The reason might be that your method is not as sophisticated. I would bet that NIntegrate uses an adaptive method and splits the integrals in parts which are then refined according to error estimates. If you do not improve your algorithm, you probably won't beat it. As someone pointed out, mupadex is the costly part. This comes from the number of function evaluations (specifically hypergeom). You can reduce them by some divide-and-conquer algorithm. It is not a good idea to use such a high number of Gauss-nodes anyway. A better one is as mention to resort to a lower order integrator and refine the grid. If you want code, look at the matlab function integral2Calc.m for the actual algorithm (integral2t) or at its wrapper integral2.m

In case anyone wants to run the code above, here is a working leg function.

function [x, w] = leg(a,b,N)
% Generates the abscissa and weights for a Gauss-Legendre quadrature.
beta = (1:N-1)./sqrt(4*((1:N-1)).^2-1);
[w,x] = eig(diag(beta,-1)+diag(beta,1));
absc = diag(x);
wght = 2*w(1,:)'.^2;

% Linear map from[-1,1] to [a,b]


  • $\begingroup$ This method of generating the abscissae and weights of GL quadrature is totally new to me, I'm only aware of generating them from the roots of the Legendre function iteratively (e.g. Newton's method). What is the relationship here between the eigenvalues/vectors of this matrix of off-diagonals? $\endgroup$
    – cbcoutinho
    Commented Dec 7, 2016 at 10:19
  • $\begingroup$ @cbcoutinho: this appears to be a specific implementation (for the case of the Gauss-Legendre quadrature) of the more general algorithm proposed by Golub and Welsch (1969) for quadrature rules for orthogonal polynomials. See their paper: ams.org/journals/mcom/1969-23-106/S0025-5718-69-99647-1 $\endgroup$
    – okrzysik
    Commented Dec 7, 2016 at 10:41
  • $\begingroup$ @okrzysik: Thanks for the quick reply and the link to the article. I'm curious if there has been a comparison between these two (or more?) methods of determining the abscissae and weights. Should there be a new question for that? $\endgroup$
    – cbcoutinho
    Commented Dec 7, 2016 at 12:17
  • $\begingroup$ @cbcoutinho: each method has its pros and cons (some of which they share); there are plenty of more recent articles, introducing sophisticated approaches for computing quadrature nodes and weights, that discuss some of the pros and cons. I'm unsure if any such posts, on this site, discuss comparisons between the methods. Perhaps you might like to look at some of the links given in this question on MathOverflow: mathoverflow.net/questions/203863/… $\endgroup$
    – okrzysik
    Commented Dec 7, 2016 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.