# Find $\int_0^1 f(x) \,dx \simeq A_0 f(x_0) + A_1 f(x_1)$ that is exact for $f(x) = a e^x + b \cos(\pi x /2)$?

I am reading through some exercises of the chapter I am reading about numerical integration. I stumbled upon the following question:

Find the formula $$\int_0^1 f(x) \,dx \simeq A_0 f(x_0) + A_1 f(x_1)$$ that is exact for functions of the form $$f(x) = a e^x + b \cos(\pi x/2)$$

I know that for any polynomial of degree $$\leq n$$ its integral can be exactly estimated (that is, is equal to) by the formula $$\int_a^b f(x) \,dx = \sum_{i=0}^{n} A_i f(x_i)$$ where $$A_i = \int_a^b \ell (x)\, dx$$ (Lagrange fundamental polynomial) for $$n+1$$ distinct nodes $$x_i \quad 0 \leq i \leq n$$.

However, the $$f$$ in the exercise is clearly not a polynomial. So no idea how to find such a formula that is exact as with numerical integration for polynomials. Can you help?

• Did you try to integrate analytically and guess A0, x0 A1 and x1? – Thomas Jan 13 at 19:20
• No I do not care about guesses but rather the derivation and explanation for the numerical integration formula that is exact for functions of the given form :)) – Dip Jan 13 at 19:26
• If you integrate analytically and equate you will have an equation which will has to be valid for all a and b. It will be linear in a and b therefore you can just put to zero their prefactors. Any combination of coefficients solving that system of two equations would do the job. Did not make the math but it could work like that – Thomas Jan 13 at 19:30

You can just set for instante $$x_0=0$$ and $$x_1=1$$ and compute $$A_0,A_1$$ such that $$\int_0^1 e^x dx = A_0 e^0 + A_1 e^1, \quad \int_0^1 \cos \frac{\pi x}{2}\, dx = A_0 \cos \frac{\pi \cdot 0}{2} + A_1 \cos \frac{\pi \cdot 1}{2}.$$

This way you obtain the quadrature $$Q(f)=\frac{2}{\pi} f(0) + \frac{e \pi -2-\pi }{e \pi } f(1).$$

If you require the formula to be exact to an extended set of functions, you'll be able to remove the initial arbitrarity in the choice of $$x_0,x_1$$. For instance, if you take $$x_0 \approx 0.221611$$, $$x_1 \approx 0.79518$$, $$A_0 \approx 0.513634$$, $$A_1 \approx 0.486366$$, the formula will be exact for functions of the form $$f(x)= a e^x + b \cos \frac{\pi x}{2} + c \sin \frac{\pi x}{2} + d.$$

Integrating explicitly and equating to 0 the coefficients multiplying $$a$$ and $$b$$ individually one gets the linear system:

$$e-1=A_0e^{x_0}+A_1e^{x_1}$$ 

$$\frac{2}{\pi}=A_0cos(\pi x_0/2)+A_1cos(\pi x_1/2)$$ 

If we have $$x_0$$, $$x_1$$ and $$A_0,A_1$$ that solve  and  we are done.

Let's take $$x_0$$ and $$x_1$$ arbitrary. Since this system is linear in $$A_0,A_1$$ one can simply invert the matrix of coefficients and solve for these:

$$\begin{pmatrix} A_0 \\ A_1 \end{pmatrix}$$ =$$\begin{pmatrix} e^{x_0} & e^{x_1}\\ cos(\pi x_0/2) & cos(\pi x_1/2) \end{pmatrix}^{-1}$$ $$\begin{pmatrix} e-1 \\ 2/\pi \end{pmatrix}$$

It is simple to find $$x_0$$ and $$x_1$$ such that the system is invertible checking that the determinant is different from zero.

• Wooooowww thank you! But one thing, can you elaborate on how you got the two equations? Like why $e-1$ and $\frac{2}{\pi}$? I am not sure how you get those two equations :/ I really, really appreciate your help! – Dip Jan 13 at 21:02
• It is derived integrating analytically the exponential and the cosine, as discussed in my comment. – Thomas Jan 13 at 21:50
• See also PierreCarre answer. I hope you understand the logic behind... it is nothing fancy actually... – Thomas Jan 14 at 10:14