15 votes

Why does Matlab's integral outperform integrate.quad in Scipy?

The question has two very different subquestions. I will address the first one only. Matlab's version runs on average 24 times faster than my python equivalent! The second one is subjective. I ...
user avatar
15 votes

Tanh-sinh quadrature numerical integration method converging to wrong value

There are a bunch of pitfalls when it comes to tanh-sinh quadrature, one being that the integrand needs to be evaluated very closely to the interval boundaries, at distances less than machine ...
user avatar
12 votes

Numerical evaluation of highly oscillatory integral

Use Plancherel's theorem to evaluate this integral. The basic idea is that for two functions $f,g$, $$ I=\int_{-\infty}^{\infty} f(x) g^*(x)dx = \int_{-\infty}^{\infty} F(k) G^*(k) dk $$ where $F,G$...
user avatar
  • 653
10 votes

Integrating Lagrange polynomials with many nodes, round-off

You can evaluate this using the Björck-Pereyra algorithm for solving Vandermonde systems, because you are evaluating $b^\top V^{-1}$ with $b=(2,0,\frac23,0,\frac25,0,\ldots)$, and the algorithm is ...
user avatar
  • 11.4k
9 votes

Approximate $h$ in $F(\theta)=\sin \theta \int_{-L}^{+L}h(z)e^{-ikz\cos \theta} \,dz$

This is a linear integral equation (because the right hand side is linear in $h$) and presumably the problem is ill-posed (for one, because of the multiplication by $\sin(\theta)$ but also because the ...
user avatar
  • 1,728
9 votes
Accepted

Numerical calculation of Integral of Si(x)/x

For large $x$, you can use the same approach as the one in the paper you cite. They define these auxiliary functions that link $\mathrm{Si},\mathrm{Ci}$ with $\sin,\cos$, and the point of why this ...
user avatar
  • 11.4k
8 votes

Is there a Gauss-Laguerre integration routine in Python?

There are standard methods for these types of quadrature in Python, in NumPy and SciPy: Gauss-Laguerre quadrature Gauss-Legendre quadrature Gauss-Hermite quadrature (as noted in your post) Gauss-...
user avatar
8 votes
Accepted

How do error estimates scale for multidimensional cubature?

Using product quadrature rules for multi-dimensional integrals suffers from the so-called curse of dimensionality. An $O(N^{-2})$ accurate rule using N evaluations in one-dimension is generally $O(N^{...
user avatar
  • 221
8 votes
Accepted

Numerical evaluation of an elliptic integral in python

The problem is almost definitely with how QUADPACK (which is the backend used by scipy.integrate.quad) handles numerically small integrands. Essentially the ...
user avatar
  • 11.4k
8 votes

Plot integral function with scipy and matplotlib

First of all, your function $x\sin(\frac{1}{x})$ is singular in $x=0$. You might want to add an if clause like this: ...
user avatar
  • 6,046
8 votes
Accepted

How to estimate the error of trapezoidal rule using discrete data?

You may use the ideas of error extrapolation as one uses it to construct high-order Runge Kutta methods. Depending on the function that you interpolate, the interpolation error $I - I_h$, where $I$ ...
user avatar
  • 3,398
7 votes
Accepted

Numerical integration of a hypergeometric function

First of all, from the first paragraph of your attempts at a solution, I assume that the $z_j$ are non-negative? In that case, the integrand has no real problematic points (it's monotonous, decreasing)...
user avatar
  • 6,046
7 votes
Accepted

How do I integrate a function defined over an arbitrary area?

Instead of directly integrating over the area, it is often more convenient to use the divergence theorem to replace the area integral with an integral over the boundary edges. The divergence theorem ...
user avatar
  • 5,734
7 votes

Numerical evaluation of highly oscillatory integral

The key to the evaluation of oscillatory integrals is to truncate integral at the right point. For this example you need to choose upper limit of the form $$ \pi\mathbb{N}+\frac{\pi}{2} $$ Before ...
user avatar
7 votes

Numerical integration giving incorrect sign

This might be an accuracy problem in computing the second term, because of those large exponentials when $x \gg 1$. I would first work on that term: gather $e^x$ out from numerator and denominator and ...
user avatar
6 votes
Accepted

How to implement Gauss-Laguerre Quadrature in Python?

The issue here is that the error term increases very quickly as $s$ increases. The error term is, according to this article, is bounded by $$ |E| < \frac{n!^2}{(2n)!}\max |f^{(2n)}(t)|. $$ Now, ...
user avatar
  • 11.4k
6 votes

Radial integration of expensive function with Bessel weights

For the Hankel transform, one can classify the methods into four major groups: Numerical quadrature-based. Fourier-based ones. Asymptotic expansion of Bessel into sines and cosines. Projection-slice ...
user avatar
  • 8,287
6 votes

In C++, how do you calculate the analytical value of $\int_a^b \left|\sin x \right|\,dx$?

You could just go for the direct final answer of the integral: $$\int_{a}^{b}\vert {\sin(x)}\vert dx = 2\left(\left\lfloor\frac{b}{\pi}\right\rfloor - \left\lfloor\frac{a}{\pi}\right\rfloor\right) + \...
user avatar
6 votes
Accepted

Numerical computation of two-sided (bilateral) Laplace transform

With the substitution $y=x+t$, the $t$-dependency can be factored out: $$I(t)=\int_{-\infty}^\infty e^{-(y-t)}F(y)\;dy = e^t \int_{-\infty}^\infty e^{-y}F(y)\;dy = C\cdot e^t$$ This still leaves ...
user avatar
  • 481
6 votes
Accepted

Gaussian Numerical Differentiation

Yes. As you may know, numerical differentiation and integration is closely related to (polynomial) interpolation: The idea to approximately differentiate or integrate a given function is to ...
user avatar
6 votes
Accepted

Evaluating an integral numerically at many points

The point of @WolfgangBangerth is exactly what I mentionend in my comment, so I'd always try this first. In the best case, with millions of partitions $[a_{i},a_{i+1}]_{i\in \{0,\ldots,N-1\}}$ (where $...
user avatar
  • 2,274
6 votes

Numerical evaluation of highly oscillatory integral

Ooura's method for Fourier sine integrals works here, see: Ooura, Takuya, and Masatake Mori, A robust double exponential formula for Fourier-type integrals. Journal of computational and applied ...
user avatar
  • 2,001
5 votes
Accepted

Solving the quadratic in the Fast Marching Method

Physically, this problem arises when the two input nodes are "impossibly far apart" according to the planar wavefront approximation. Specifically, if two nodes separated by distance $\Delta x \sqrt{2}...
user avatar
5 votes

Tanh-sinh quadrature numerical integration method converging to wrong value

Your approach to selecting $h$ based on $N$ isn't a very sophisticated one and probably isn't getting $h$ to be small enough. See these notes for a more reasonable way to adjust $h$ and estimate the ...
user avatar
5 votes

Integration over a complicated domain

You could triangulate the domain (with say a Delaunay method), and then integrate over the triangles using standard integration techniques. This would be a finite element method, but just for ...
user avatar
  • 10.8k
5 votes

Integration over a complicated domain

To give you a better picture, what I mean with my comment, consider Stokes Theorem $$\int_D db^{n-1}=\int_{\partial D}b^{n-1}$$ be $b^{n-1}$ an arbitrary 1-form $$b^{1}= a_1 dx + a_2 dy$$ leads to $...
user avatar
  • 1,285
5 votes

Plot integral function with scipy and matplotlib

Replace the last line by plot(X, [F(x)[0] for x in X]) That should do it. Edit: you can define your function F as ...
user avatar
5 votes
Accepted

Calculating integrals for a function approximated by Chebyshev polynomials

If you know the Chebyshev expansion for $f(z)$, why don't you formally integrate the polynomials using the recurrence relation for Chebyshev polynomials ? The Clenshaw-Curtis method is based on this ...
user avatar
  • 6,046
5 votes
Accepted

Numerical evaluation of gaussian-like integral expressible as a recurrence relation

Step 1: analyze the recurrence I implemented the recurrence in Python, using basic numpy and scipy.special for the erf function. Why? Because it is simple and ...
user avatar
  • 6,046
5 votes
Accepted

Integrating Lagrange polynomials with many nodes, round-off

The calculation of $$ \int_{-1}^{1} L_k(x)\,\text{d} x $$ for the Lagrange polynomials $L_k$ defined on an arbitrary grid $x_k, k=0,\ldots,n$ can be performed by the following two steps: Calculate ...
user avatar
  • 2,274

Only top scored, non community-wiki answers of a minimum length are eligible