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 ...
kostyfisik's 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 ...
Nico Schlömer's 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$...
smh's user avatar
  • 663
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 ...
Kirill's 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: ...
GertVdE's user avatar
  • 6,179
8 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 ...
Kirill's user avatar
  • 11.4k
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)...
GertVdE's user avatar
  • 6,179
7 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$ ...
Jan's user avatar
  • 3,418
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 ...
Bill Greene's user avatar
  • 5,984
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 ...
David Saykin's 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 ...
Federico Poloni's user avatar
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 ...
Anton Menshov's user avatar
  • 8,652
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) + \...
SassyQuatch's 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 ...
cdalitz's 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 ...
Christian Clason's 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 $...
davidhigh's user avatar
  • 3,127
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 ...
user14717's user avatar
  • 2,155
6 votes
Accepted

Gauss-Lobatto quadrature and nodal points for FEM

Ok, here comes the answer promised in the comment section. Let's start the other way round, going from a general grid to Gaussian grids and further constructions such as spectral elements. In grid ...
davidhigh's user avatar
  • 3,127
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 ...
GertVdE's user avatar
  • 6,179
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 ...
Christoph Wehmeyer's user avatar
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 ...
GertVdE's user avatar
  • 6,179
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 ...
davidhigh's user avatar
  • 3,127
5 votes

Building Gaussian-type quadrature schemes with Zernike polynomials

Unfortunately the 1D reasoning will not directly carry into 2D and beyond. Depending on the domain for your integral, you will have considerable latitude in the node choice - but of course the ...
Reid.Atcheson's user avatar
5 votes

Building Gaussian-type quadrature schemes with Zernike polynomials

Yes, one can extend the Gaussian quadrature (in one dimension) to multiple dimensions. Often this is called cubature. Much work on this has been done by Ronald Cools and collaborators (see his ...
GertVdE's user avatar
  • 6,179
5 votes

What is the best numerical method for a six dimensional spherical integral?

The Genz-Malik algorithm [1], as implemented in the cubature library, works well for computing 6-dimensional integrals. [1] A. C. Genz and A. A. Malik, “Remarks on algorithm 006: An adaptive ...
Juan M. Bello-Rivas's user avatar
5 votes
Accepted

Quadrature of rational functions

Choose four collocation points in the interval $[a,b]$, e.g., $ x_0=a\\ x_1=a + (1/3)(b-a)\\ x_2=a + (2/3)(b-a)\\ x_3=b $ and form a matrix $M$ \begin{bmatrix} 1 & x_0 & x_0^2 & x_0^3 \\ 1 ...
Maxim Umansky's user avatar
5 votes

Which way is the right way to compute the integrals in finite element methods?

Quadrature and replacing the integrand by a polynomial are identical. That is how quadrature rules are derived. To see why this is true remember that quadrature evaluates the integrand at only a ...
Wolfgang Bangerth's user avatar
4 votes

Numeric Quadrature with Derivatives

Although this thread is quite old, I thought it might be useful to have a reference to a peer-reviewed paper for generalizations of some common quadrature rules. Nenad Ujevic, "A generalization of ...
Lysistrata's user avatar
4 votes

Plot integral function with scipy and matplotlib

...
YakovK's user avatar
  • 141
4 votes

Choice of Newton-Cotes formulae for regularly gridded multi-dimensional data

First, it can be found that Newton-Cotes can suffer from Runge's Phenomena when you use higher order version of it (because of the underlying Lagrange interpolants used in the formulation). So I ...
spektr's user avatar
  • 4,228

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