In this exercise you will approximate the value of the definite integral of a po
ID: 3856030 • Letter: I
Question
In this exercise you will approximate the value of the definite integral of a polynomial using both Riemann sums and a MATLAB built-in function int.
The code accepts as inputs: a polynomial P, a vector n, and two scalars a, b. The program will use Riemann sums to approximate the definite integral on the interval [a,b] of a given polynomial P. Riemann sum calculations should be performed using partitions of [a,b] by subintervals of equal length h defined as
h = (b – a)/n(j),
where n(j) is a jth entry on n, j=1:N, and N= length(n). Each entry of vector n, n(j), is equal to the number of subintervals of the corresponding partition of
Your function has to return a table T whose first column is the vector n. Columns 2 – 4 are the column vectors c’, d’, f’ of the Riemann’s sums approximations of the integral of P on the interval [a, b], with the numbers of subintervals of partition defined by the entries of n, and the value of the function to be chosen at the left endpoints (gives vector c), at the middle points (gives vector d), and at the right endpoints (gives vector f) of each subinterval of the partition.
function [T, I] = reimsum(P,a,b,n)
that calculates vectors c, d, f as described above and forms a matrix A = [n’, c’, d’, f ’].
The following command converts the 4-by-N array A into a 4-by-N table T with the names of the variables as indicated below:
T=array2table(A,...
'VariableNames',{'n','Left','Middle','Right'})
You should use format long in you code.
You will output the table and, besides that, the value of the integral I, calculated by means of the MATLAB function int, specifically, I= double(int(P, a, b)). (double(s) converts the symbolic value s to double precision.)
The program could use nested for loops, the command sym2poly, and a MATLAB function polyval. Use closetozeroroundoff function (that you created in Exercise 3) on the variable d in your code, that is, to output d, write d= closetozeroroundoff (d);
Explanation / Answer
In our section on the numerical evaluation of Riemann sums, we saw that a Riemann sum with n points is an approximation of the definite integral of a function. For example, if we take the partition P = [x0, x1, . . . , xn], and we evaluate our function at right endpoints, then Z b a f(x)dx Xn k=1 f(xk)xk, where the larger n is the better this approximation is. If we are going to employ this approximation in applications, we need to have a more quantitative idea of how close it is to the exact value of the integral. That is, we need an estimate on the error. We define the error for this approximation by ER := | Z b a f(x)dx Xn k=1 f(xk)xk|. In order to derive a bound on this error, we will assume (for reasons that will become apparent during the calculation) that the derivative of f is bounded. That is, we assume there is some constant M so that |f (x)| M for all x [a, b]. We now begin computing ER := | Z b a f(x)dx Xn k=1 f(xk)xk| = | Xn k=1 Z xk xk1 f(x)dx Xn k=1 f(xk)xk|, where we have used the relation Z b a f(x)dx = Z x1 x0 f(x)dx + Z x2 x1 f(x) + · · · + Z xn xn1 f(x)dx = Xn k=1 Z xk xk1 f(x)dx, which follows from a repeated application of the integration property Z b a f(x)dx = Z c a f(x)dx + Z d c f(x)dx. 4 Observing also that Z xk xk1 dx = (xk xk1) = xk, we see that ER = | Xn k=1 Z xk xk1 (f(x) f(xk))dx| = | Xn k=1 Z xk xk1 f (ck(x))(x xk)dx|, where in this step we have used the Mean Value Theorem to write f(x) f(xk) = f (ck(x))(x xk), where ck [x, xk] and so clearly depends on the changing value of x. One of our properties of sums and integrations is that if we bring the absolute value inside we get a larger value. (This type of inequality is typically called a triangle inequality.) In this case, we have ER Xn k=1 Z xk xk1 |f (ck(x))(x xk)|dx Xn k=1 Z xk xk1 M(xk x)dx = M Xn k=1 h (xk x) 2 2 xk xk1 i = M Xn k=1 (xk xk1) 2 2 = M 2 Xn k=1 x 2 k = M 2 Xn k=1 (b a) 2 n2 = M 2 (b a) 2 n2 Xn k=1 1 = M(b a) 2 2n . That is, the error for this approximation is ER = M(b a) 2 2n . Error estimate for Riemann sums with right endpoints. Suppose f(x) is continuously differentiable on the interval [a, b] and that |f (x)| M on [a, b] for some finite value M. Then the maximum error allowed by Riemann sums with evaluation at right endpoints is ER = M(b a) 2 2n . 5 In order to understand what this tells us, we recall that we have previously used the M-file rsum1.m to approximate the integral Z 2 0 e x 2 dx, which to four decimal places is .8821. When we took n = 4, our approximation was .6352, with an error of .8821 .6352 = .2458. In evaluating ER, we must find a bound on |f (x)| on the interval [0, 2], where f (x) = 2xex 2 . We can accomplish this with our methods of maximization and minimization from first semester; that is, by computing the derivative of f (x) and setting this to 0 etc. Keeping in mind that we are trying to maximize/minimize f (x) (rather than f(x)), we compute f (x) = 2e x 2 + 4x 2 e x 2 = e x 2 (2 + 4x 2 ) = 0, which has solutions x = ± 1 2 . The possible extremal points for x [0, 2] are 0, 1 2 , and 2, and we have f (0) = 0 f ( 1 2 ) = 2e 1 2 = .8578 f (2) = 4e 4 = .0733. The maximum value of |f (x)| over this interval is M = .8578. Our error estimate guarantees that our error for n = 4 will be less than or equal to .8578(2)2 8 = .4289. We observe that the actual error (.2458) is much less than the maximum error (.4289), which isn’t particularly surprising since the maximum error essentially assumes that f (x) will always be the largest it can be. Finally, suppose we would like to ensure that our approximation to this integral has an error smaller than .0001. We need only find n so that M(b a) 2 2n < .0001. That is, we require n > M(b a) 2 2(.0001) . For this example, n > .8578(4) .0002 = 17, 156. We can easily verify that this is sufficient with the following MATLAB calculation. 6 >>f=inline(’exp(-xˆ2)’); >>rsum1(f,0,2,17157) ans = 0.8821 1.2 Riemann Sums with Midpoints (The Midpoint Rule) In our section on the numerical evaluation of Riemann sums, we saw in the homework that one fairly accurate way in which to approximate the value of a definite integral was to use a Riemann sum with equally spaced subintervals and to evaluate the function at the midpoint of each interval. This method is called the midpoint rule. Since writing an algorithm for the midpoint rule is a homework assignment, we won’t give it here, but we do provide an error estimate. Error estimate for the Midpoint Rule. Suppose f(x) is twice continuously differentiable on the interval [a, b] and that |f (x)| M for some finite value M. Then the maximum error allowed by the midpoint rule is EM = M(b a) 3 24n2 . The proof of this result is similar to the one given above for evaluation at right endpoints, except that it requires a generalization of the Mean Value Theorem; namely Taylor approximation with remainder. Since Taylor approximation is covered later in the course, we wo
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.