How to solve integrate(exp(x^2)) from 0 to 1 using by Numerical integration/Gauss-Legendre Quadrature?

4k Views Asked by At

I tried to implement Numerical integration/Gauss-Legendre Quadrature using by Python.

And follwing this example

enter image description here

I could get the correct answer but anyway if the function changes to exp(-x^2)

How should i do?

Should i put ^2 over x or over f() or ...? from this formula. and how about the minus?

enter image description here

1

There are 1 best solutions below

2
On BEST ANSWER

The following are true for both problems.

The points (nodes) are:

  • $x_1 = -0.9061798459386641$
  • $x_2 = -0.538469310105683$
  • $x_3 = 0.$
  • $x_4 = 0.5384693101056831$
  • $x_5 = 0.9061798459386641$

The weights are:

  • $w_1 = 0.23692688505618847$
  • $w_2 = 0.4786286704993665$
  • $w_3 = 0.568888888888889$
  • $w_4 = 0.47862867049936614$
  • $w_5 = 0.23692688505618847$

My program calculates the above, but they have a nice closed-form table of the points /nodes for $n=1 ~\ldots~ 5$ on the Wiki.

We want to use Gauss-Legendre Quadrature to find $\int_a^b f(x)~dx$, where:

$$\int_a^b ~f(x)~dx = \int_{-3}^3 ~e^{-x^2}~dx \approx \dfrac{b-a}{2} \sum_{n=1}^5 f\left(\dfrac{b-a}{2}x_i + \dfrac{a+b}{2}\right) = 3 \sum_{i=1}^5 f\left(3x_i\right)$$

This gives us:

$$3 \sum_{i=1}^5 e^{-(3x_i)^2} = 3 (0.000146211 + 0.0352118 + 0.568889 + 0.0352118 + 0.000146211) = 1.91881$$

We can compare this to the exact results of:

$$\int_{-3}^3 ~e^{-x^2}~dx = \sqrt{\pi } \text{erf}(3) \approx 1.77241$$

If we would have taken $10$ nodes, we would have gotten:

$$\int_{-3}^3 ~e^{-x^2}~dx = 1.77224$$

Update

Recall, that when we change the limits of integration, we must rescale the problem to $[-1,1]$ (that is what those $a,b$ terms do in the function definition), so we have:

$$\int_a^b ~f(x)~dx = \int_{0}^1 ~e^{-x^2}~dx \approx \dfrac{b-a}{2} \sum_{n=1}^5 f\left(\dfrac{b-a}{2}x_i + \dfrac{a+b}{2}\right) = \dfrac{1}{2} \sum_{i=1}^5 f\left(\dfrac{1}{2}x_i + \dfrac{1}{2}\right)$$

This gives us:

$$\int_{0}^1 ~e^{-x^2}~dx \approx = \dfrac{1}{2} \sum_{i=1}^5 f\left(\dfrac{1}{2}x_i + \dfrac{1}{2}\right) = \dfrac{1}{2} \sum_{i=1}^5 e^{-\left(\dfrac{1}{2}x_i + \dfrac{1}{2}\right)^2} = 0.746824$$

The exact result is:

$$\frac{1}{2} \sqrt{\pi } \text{erf}(1) \approx 0.746824$$