MATLAB: Difference between integral and quad functions

integrationnumerical integration

Can someone briefly explain to me the difference between the integral and quad functions? I know that they will give different answers because of different algorithms but usually the results are close.
In my case I have a function of 2 variables f(x,y). Say I integrate over y from a to b using integral so I have
g(x) = integral(@(y) f(x,y),a,b).
Suppose now that I solve for the roots of g and I find a root at x_0. If I test this solution is correct by
integral(@(y) f(x_0,y),a,b) I get something of the power e^-17.
If instead I use quad(@(y) f(x_0,y),a,b) I get 0.0365.
Moreover whether I compute g(x) using integral or quad makes a huge difference as the solution x_0 I am getting from g(x)=0 is very different in the 2 cases.

Best Answer

Well, the short answer is don't use QUAD for anything anymore. Here's why.
  1. INTEGRAL supports mixed relative and absolute error control: specify the relative accuracy (essentially how many digits) you want with RelTol. You need AbsTol because relative error isn't defined when the true answer is zero. So RelTol is usually going to control the accuracy, but if the answer is small in magnitude, AbsTol will take over.
  2. INTEGRAL can handle mild singularities at the end points.
  3. INTEGRAL can integrate over non-finite intervals.
  4. INTEGRAL can perform piecewise-linear path integrals in the complex plane using the Waypoints options. The Waypoints option can also be used to improve efficiency for piecewise-defined functions on the real axis.
  5. INTEGRAL can handle vector-valued functions with the ArrayValued option (you can use QUADV for that).
  6. INTEGRAL uses a higher order method even than QUADL, so it is usually more accurate and efficient on smooth problems than either QUAD or QUADL.
  7. INTEGRAL starts with a much finer initial mesh than QUAD and is more conservative in how it controls the error. As a result it tends to be more reliable.