# Trapezoidal Method

2pages on
this wiki

The Trapezoidal Method, also known as the Trapezoidal Rule, is an approximation method of numerical integration, and is a member of the closed type group of the Newton-Cotes formulae.

## BackgroundEdit

The Trapezoidal Method is used to approximate the values of definite integrals, defined as the area under the graph of the function $f(x)$ with respect to $x$(meaning if $x$ is negative, then $f(x)$ has a negative area), over the compact interval $\left[a,b\right]$ where $a$ < $b$. Examples of definite integrals are $\textstyle\int\limits_{0}^{1}\sqrt{1-x^3}\,dx\quad$ and $\textstyle\int\limits_{0}^{1}e^{-x^2}\,dx\quad$.

The Trapezoidal Method belongs to a group of numerical integration formulae called the Newton-Cotes formulae, named after Isaac Newton and Roger Cotes. Other formulae belonging to the group (for the closed type, of which the Trapezoidal Method is one) include the Simpson's 1/3 and 3/8 Rules, and the Boole's Rule.

There are two types of applications of the Trapezoidal Method. One is for evalutaion at only two points, and the other is for evalutaion at multiple points -- which is called the Composite Trapezoidal Method, For this particular discussion, we will focus on the latter.

## ApplicationsEdit

There are arguably countless applications for integration throughout the many fields of engineering. Among the more common examples of which include finding of the velocity of a body from an acceleration function, and finding the displacement of a body from a velocity function. The use of the Trapezoidal Method and other approximation methods for integration could prove to be an effective tool in such applications. lol

## MethodEdit

Consider the definite integral

$\int\limits_{a}^{b} f(x)\,dx$.

We assume that $f(x)$ is continuous on $\left[a,b\right]$ into $n$ subintervals of equal length, defined as

$\Delta x = \frac{b - a}{n}$,

and using the $n + 1$ points

$x_0 = a, \quad x_1 = a + \Delta x, \quad x_2 = a + 2\Delta x, \quad \dots \quad x_n = a + n\Delta x = b$,

after which we can compute for f(x) at these points.

$y_0 = f(x_0), \quad y_1 = f(x_1), \quad y_2 = f(x_2), \quad \dots \quad y_n = f(x_n)$

We then form $n$ number of trapezoids by drawing straight line segments in between the points $(x_{i},y_{i})$ and $(x_{i+1},y_{i+1})$ for $0 \le i \le n$, as shown below.

Recalling the formula in solving for the area of a trapezoid,

$A = \frac{1}{2}\,(b_1 + b_2)\,h$.

To solve for the area between two subintervals under the curve, defined as $a_i$, we replace $b_1$ and $b_2$ with $y_i$ and $y_{i+1}$ for $0 \le i \le n - 1$, and replace $h$ with $\Delta x$, thus

$a_i = \frac{1}{2}\,(y_i + y_{i+1})\,\Delta x$.

To solve for the entire area under the curve, defined as $A$, we simply get the sum of all the areas between the subintervals.

$A = a_1 + a_2 + \dots + a_i$

Expanding the equation,

$A = \frac{\Delta x}{2} \Big(\big(y_0 + y_1\big) + \big(y_1 + y_2\big) + \dots + \big(y_{n-2} + y_{n-1}\big) + \big(y_{n-1} + y_n\big)\Big)$

and further simplifying it, we get

$A = \frac{\Delta x}{2} \Big(y_0 + 2\big(y_1 + y_2 + \dots + y_{n-2} + y_{n-1}\big) + y_n\Big) \approx \int\limits_{a}^{b} f(x)\,dx$.

## ExampleEdit

Use the trapezoidal rule with $n = 6$ to estimate

$\int\limits_{1}^{4}x^4\,dx$.

Compute also for percentage of error.

Solution

For $n = 6$, we have

$\Delta x = \frac{4 - 1}{6} = 0.5$

We compute for the values of $y_0,~y_1,~y_2,~\dots,~y_6$.

 $x$ $1$ $1.5$ $2$ $2.5$ $3$ $3.5$ $4$ $f(x) = x^4$ $1$ $5.0625$ $16$ $39.0625$ $81$ $150.0625$ $256$

Therefore,

$\int\limits_{1}^{4}x^4\,dx ~ \approx ~ \frac{0.5}{2} \Big(1 + 2\big(5.0625 + 16 + 39.0625 + 81 + 150.0625\big) + 256\Big) ~ \approx ~ 209.84375$

To solve for the percentage of error, we compute for the exact value of the integral.

$\int\limits_{1}^{4}x^4\,dx ~ = ~ \frac{4^5}{5} - \frac{1^5}{5} ~ = ~ 204.6$

Percentage of error can then be computed using the formula

$\% ~ error ~ = ~ \left| \frac{approx. - actual}{actual} \right| \times 100\%$.

Substituting the values, we get:

$\% ~ error ~ = ~ \left| \frac{209.84375 - 204.6}{204.6} \right| \times 100\% ~ = ~ 2.56\%$

## ImplementationsEdit

### C++Edit

double trapezoidal(double l, double u, int n)
{
double interval, timestwo, sum;
double x[n+1], y[n+1];
int ctr1, ctr2, ctr3;

//computes for the interval
interval = (u - l) / n;

//sets the values for x and f(x)
for(ctr1 = 0; ctr1 <= n; ctr1++)
{
x[ctr1] = l;
y[ctr1] = function(l);
l = l + interval;
}

//displays the x and f(x) values
cout << "\nx\tf(x)\n----------------\n";
for(ctr2 = 0; ctr2 <= n; ctr2++)
cout << x[ctr2] << "\t" << y[ctr2] << "\n";

//adds the values in between the lower and upper limits
for(ctr3 = 1; ctr3 < n; ctr3++)
{
sum = sum + y[ctr3];
}

//computes for the entire approximation
sum = ((sum * 2) + y[0] + y[n]) * (interval / 2);

return sum;
}


### JavaEdit

private static double Integrate(double sizeofPanels, double yTable[]){
double firstY = yTable[0];
double lastY = yTable[yTable.length-1];
System.out.println("");
double middle = 0.0;
double middleValues = 0.0;

for(int i=1; i<yTable.length-1; i++)
middleValues = middleValues + yTable[i];

middle = 2*middleValues;
double integratedF = (sizeofPanels/2)*(firstY + middle + lastY);
return integratedF;
}