# Writing a program using the trapezoidal rule

Write a program to evaluate $I=\int_a^bf(x)dx$ using the trapezoidal rule with $n$ subdivisions, calling the result $I_n$. Use the program to calculate the following integrals with $n=2,4,8,16,32,64,128,256$. Analyze empirically the rate of convergences of $I_n$ to $I$ by calculating the ratios $R_n=\frac{I_{2n}-I_n}{I_{4n}-I_{2n}}$.

a.$\int_0^{2\pi}\frac{dx}{2+cos(x)}$

b.$\int_{-4}^4\frac{dx}{1+x^2}$

c.$\int_0^1x^{5/2}dx$

My attempt at a pseudo-code:

Initialize $1-$dimensional arrays $I(8)$ and $R(6)$.

input function $\int_a^b f(x)$

for $m=1$ to $8$

$n = 2^m$

$h = \frac{(b - a)}{n}$

$t = f(a) + f(b)$

for $x = a + h$ to $b - h/2$ step $h$

$t = t + 2*f(x)$

next $x$

$t = t*(h/2)$

$I(m)=t$

next $m$

for $r = 1$ to $6$

$R(r)= (I(r+1) - I(r))/(I(r+2) - I(r+1))$

next $r$

Display values in arrays $I$ and $R$.

-
If it's a question about coding, there's a website for that. – Gerry Myerson Feb 24 '13 at 3:38
@DasKrümelmonster the question is marked in gray. – user60514 Feb 24 '13 at 3:38
Another forum may be more appropriate for this question but anyways. There are several issues with your code. The approximation of the integral and of the error should be separate functions. To evaluate the trapeze approximation, think of it as a dot product between two vectors. If you use, e.g., Matlab, that will not only simplify your code but make it run much faster. – Dominique Feb 24 '13 at 4:24
Also, you should NOT compute $x$ by $x = x+h$, because you might accumulate roundoff error for small $h$. Instead, use $x = x_0+(n-1)h$ where $x_0$ is the starting $x$ and $n$ is the number of steps so far. – marty cohen Feb 24 '13 at 7:50

Lets work an example of the Trapezoidal Rule algorithm.

Once you have working code, adding larger sample sizes (increasing $n$) is very easy, but lets first work out an example.

Using the Trapezoidal rule, with $n = 4$, we have:

$\displaystyle a = 0, b = 2 \pi ~~\text{and}~~ f(x) = \frac{1}{2 + \cos x}$, so for $\displaystyle n = 4 \Rightarrow h = \frac{b-a}{n} = \frac{2 \pi}{4} = \frac{\pi}{2} ~~\text{and}~~ x_i = a + ih$

$$\begin{array}{c|c|c} \text{i} & \text{0} & \text{1} & \text{2} & \text{3} & \text{4}\\ \hline \\x_i & 0 & \frac{\pi}{2} & \frac{\pi}{1} & \frac{3 \pi}{2} & \frac{2 \pi}{1} \\f(x_i) & 0.333333 & 0.5 & 1 & 0.5 & 0.333333 \end{array}$$

So,

$\displaystyle \int_0^{2\pi}\frac{dx}{2+cos(x)} \approx \frac{h}{2}[f(x_0) + 2f(x_1) + 2f(x_2) + 2f(x_3) + f(x_4)] = \frac{\pi}{2 \times 2}[0.333333 + 1 + 2 + 1 + 0.333333] = 3.665191.$

Using WA, we get:

$$\int_0^{2\pi}\frac{dx}{2+cos(x)} = 3.6275987$$

If you take more samples, the error should settle and you'll get better approximations.

There are some working code snippets on Wiki as this is quite an easy program to implement.

Once you have working code, you can compare to this online calculator for the various $n$ values.

Does that all make sense?

Update

Here is a nice write-up on error calculations. For your question on $R_n$, all you are doing is filling out a row of calculations at a time and keeping an error estimate at each step and you actually documented this in your algorithm. It is Just comparing your calculated value to the actual value ($I_n$ to $I$), using the formula you gave.

Have fun!

-
Thanks again Amzoti! I am going to go over what you wrote and implement it in my studies. Thanks! – user60514 Feb 24 '13 at 5:03
Will do, going to go over it now. Thanks! – user60514 Feb 24 '13 at 5:10
Amzoti, I understand everything except two things that I hope you can answer? First is how can I analyze empirically the rate of convergences of $I_n$ to $I$ by calculating the ratios $$R_n=\frac{I_{2n}-I_n}{I_{4n}-I_{2n}}?$$ And also, is there a website like the one you gave me (online calculator) for the "corrected trapezoidal rule"? – user60514 Feb 24 '13 at 7:22
@user60514: It is just a running total of the error so you can show that the error is decreasing as a function of n. I do not know of any online version like that, but you can find such things or slightly modify your code to do the "corrected, which I think means composite" approach. See update in answer. – Amzoti Feb 24 '13 at 17:04
Good work, Amzoti! +1 – amWhy May 1 '13 at 0:20