MATLAB Trapezoidal Rule
Consider the function y=f(x) for the interval a≤x≤b, shown in figure:
To evaluate the definite integral, dx, we divide the interval a≤x≤b into subintervals each of length. Then, the number of points between x0=a,and xn=b is x1=a+∆x,x2=a+2∆x,…xn-1=a+(n-1)∆x. Therefore, the integral from a to b is the sum of the integrals from a to x1, from x1 to x2 and so on, and finally from xn-1 to b.
The total area is:
0 P1 x1that is equal to plus the area of the trapezoid x1 P1 P2 x2 that is equal to , and so on. Then, the trapezoidal approximation becomes
Example
Using the trapezoidal rule with n=4, estimate the cost of the definite integral
Compare with the exact value and evaluate the percent error.
Solution:
The exact value of this integral is
For the trapezoidal rule approximation, we have
and by substitution into an equation
From equation 3 and equation 4, we find that the percent error is