# Trapezoidal and Simpson's rule for a function

Approximate $$\int _0^1\: \sqrt{2-x^2}dx$$ using the trapezoidal and simpson's rule for 4 intervals.

Now I can determine the simpson rule is $$\frac{h}{3} \big(f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + f(x_4) \big)$$ and the trapezoidal rule is

$$\frac{h}{2} \big(f(x_0) + 2f(x_1) + 2f(x_2) + 2f(x_3) + f(x_4) \big)$$

and $$h= \frac{b-a}{n}$$ which I assume is $$\frac{1-0}{4}$$

But what I don't understand is in knowing this how you add it all together?

## 2 Answers

Take the Simpson rule as an example.

Pick the equally-spaced points at 0, 0.25, 0.5, 0.75 and 1. Let $$f(x) = \sqrt{2-x^2}$$. Then, the Simpson expression becomes,

$$\frac{1}{12}(\sqrt 2 + 4 \sqrt{ 2-0.25^2} +2\sqrt{2-0.5^2}+4\sqrt{2-0.75^2}+1)$$ $$= \frac {1}{12}(\sqrt 2 + \sqrt{31} + \sqrt 7 + \sqrt{23} +1) = 1.2853$$

Compared with the exact integral result

$$\int_0^1\sqrt{2-x^2}= \frac{2+\pi}{4}=1.2854$$

the 4-point Simpson numerical integration is very accurate.

The similar procedure can be carried out for the Trapezoidal rule.

• I see, but where do the values for the equally-spaced points come from? Sep 27, 2019 at 10:57
• @DuncanK3 - since you decide to use 4 points within the integration range (0,1), you divide the range by 4 to get $(1-0)/4=0.25$. The values of those equally spaced points are just multiples of 0.25 Sep 27, 2019 at 11:18
• Ah ok that was the piece of the puzzle I was missing! Sep 28, 2019 at 11:34

You need to actually find the values of $$x_0$$, $$x_1$$, $$\ldots$$, $$x_4$$ and then plug them into $$f$$ so that you get the values of $$f(x_0)$$, $$f(x_1)$$, $$\ldots$$, $$f(x_4)$$. Then you just plug them in and evaluate.

For example, $$x_0=0$$ and $$x_1=0+\frac{1}{4}$$, so $$f(x_0)=\sqrt{2-0^2}=\sqrt{2}$$ and $$f(x_1)=\sqrt{2-\left(\frac{1}{4}\right)^2}=\frac{\sqrt{31}}{4}$$. Then plug those in. For example, with Simpson, you get $$\frac{h}{3}\left(0+4\cdot \frac{\sqrt{31}}{4} +2f(x_2)+4f(x_3)+f(x_4)\right)$$ Now do it for $$x_2$$, $$x_3$$, and $$x_4$$, and you'll have done the Simpson's Rule part. The Trapezoid part is similar.

• I did try and plug in using the formula but how did you get (x1) = 1/4? Sep 27, 2019 at 10:55
• @DuncanK3 Start with $x_0$ as the lower number in the interval ($0$ in this case) and then add $h$ each time to get $x_1,x_2,x_3$ etc. So we have $x_1=x_0+h=0+\frac{1}{4}=\frac{1}{4}$, then $x_2=x_1+h=\frac{1}{4}+\frac{1}{4}=\frac{1}{2}$, etc. Sep 28, 2019 at 2:55
• Thanks, I was able to use the formula correctly this time! Sep 28, 2019 at 11:36