Numerical Integration (Chapters 5 & 6, C&K 6th edition) Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable of integration estimating error in numerical integration uniform method to derive numerical integration
Derive the trapezoid rule
Replace integrand f(x) on [a,b] by line p(x) Area of a trapezoid Multiple intervals with f(x) on [xk-1,xk] replaced by a line Usually x1 = a and xn = b
Trapezoid rule with integrand evaluated at n arbitrary points in the range of integration: Basic idea behind MatLab code: Input 2 arrays of length n: x = values where the integrand is evaluated. f = values of the integrand at those points In a for-loop, accumulate the area of n-1 trapezoids Output sum as trapezoid rule approximation to integral
MatLab code for trapezoid rule integration with integrand evaluated at n arbitrary points in the range of integration function A=trap_arbitraty_points(x,f) n=length(x); sum=0; for k=2:n sum=sum+(x(k)-x(k-1))*(f(k)+f(k-1))/2; end A=sum;
We will approximate by the trapezoid rule in several forms and compare their accuracy as percent difference from the exact value. How do we find the exact value of an integral? What is the anti-derivative of e-x ?
Write a script to apply trapezoid rule function to estimate with 10 equally spaced points and compare to exact value
Pseudo-code for script with equally spaced values of x. Definition a function handle for the integrand Exact = result using anti-derivative Use linspace(1,5,10) to create array of points where integrand will be evaluated A = trap_arbitrary_points(x, integrand(x)) Percent difference=100abs((A-exact)/exact) Note that integrand is evaluated inside the call to trapezoid rule. When an array is passed to a function, MatLab returns an arrary
MatLab script to apply trapezoid rule to estimate with 10 equally spaced points. integrand=@(x) exp(-x); exact=exp(1)-exp(5); x=linspace(1,5,10); A1=trap_arbitrary_points(x,integrand(x)); PD1=100*abs((A1-exact)/exact); disp([A1,exact, PD1])
Expand previous script to estimate with 10 “logarithmically” spaced points between 1 and 5. Logarithmic spacing puts more points at smaller values of x in the range of integration ([1,5] in this case)
Extended MatLab script to apply trapezoid rule to integrand=@(x) exp(-x); exact=exp(1)-exp(5); x=linspace(1,5,10); A1=trap_arbitrary_points(x,integrand(x)); PD1=100*abs((A1-exact)/exact); disp([A1,exact,PD1]) y=linspace(0,log(5),10) newx=exp(y) A2 = trap_arbitrary_points(newx,integrand(newx)) PD2 = 100*abs((A2-exact)/exact) disp(PD2)
What is the difference between the two choices of where the integrand is evaluated? Plot the 2 choices What is your conclusion? Can you rationalize the difference in accuracy?
equal spacing x-value log spacing index
Changing variable of integration
Use the transformation y = ln(x) to change the variable of integration Use the transformation y = ln(x) to change the variable of integration. dy = dx/x x(y)= exp(y) Integrand is a function of y through x(y)
x(y)= exp(y) Extended MatLab script to apply trapezoid rule to Use the integration variable y with 10 equally spaced points between 0 and ln(5). Recall log(x) is the function name for natural logirithms.
Extended MatLab script to apply trapezoid rule to integrand=@(x) exp(-x); exact=exp(1)-exp(5); x=linspace(1,5,10); A1=trap_arbitrary_points(x,integrand(x)); PD1=100*abs((A1-exact)/exact); disp([A1,exact,PD1]) y=linspace(0,log(5),10) newx=exp(y) A2 = trap_arbitrary_points(newx,integrand(newx)) PD2 = 100*abs((A2-exact)/exact) disp(PD2) xofy=newx A3 = trap_arbitrary_points(y, xofy.*exp(-xofy)) PD3=100*abs((A3-exact)/exact) disp(PD3)
Summary of results to estimate by the trapezoid rule with 10 points when the points are chosen in the following ways: 1. Equally spaced on [1, 5] 2. Logarithmically spaced (xk = exp(yk) where yk= linspace(0,ln(5),10)) 3. Equally spaced on [0, ln(5)] in the new integration variable y = ln(x). Calculate the percent difference from the exact value in each case Results for percent difference: (1) 1.6407, (2) 1.1756, (3) 0.0995
What we learned from this exercise For a given numerical-integration formula and the number of points where the integrand will be evaluated, two additional factors affect the accuracy of approximation. 1. Where the integrand is evaluated. 2. Which integration variable is used.
Composite trapezoid rule
Composite trapezoid rule Trapezoid rule for n arbitrarily spaced points Can be simplifies for equally spaced points due to the common width of trapezoid base, (b-a)/(npt-1). In the sum over averages of trapezoid heights, f(x1)=f(a) and f(xn)=f(b) occur once. All other values of the integrand occur twice. w1 = wnpt = ½ wk = 1 2<k<npt-1 Write a MatLab code for composite trapezoid rule
MatLab code for composite trapezoid rule function A=ctraprule(fh,a,b,npts) h=(b-a)/(npts-1); x=a; sum=0; for i=2:npts-1 x=x+h; sum=sum+fh(x); end %contribution from internal points sum=sum+(fh(a)+fh(b))/2; A=h*sum; end
Assignment 5, Due 2/27/19 Assume -18.79829683678703 is the “exact” value of the integral. Estimate this integral by the composite trapezoid rule with 3, 5, and 7 points. Calculate the percent difference from the exact value in each case using PD=100*abs((exact-trap)/exact).
Estimating absolute error in composite trapezoid rule More points in the range of integration were the integrand is evaluated makes the trapezoid rule more accurate. To find a relation between error and number of points: Use Taylor formula to derive an analytic expression for and the trapezoid rule approximation h[f(a+h) + f(a)]/2. Difference will be error in the composite trapezoid rule for one interval of width h. Generalize to n intervals between a and b. Based on math of next 3 slides, |e| = (b-a)3|f “(x)|/12n2 where a<x<b To get an upper bound on |e|, use maximum value of |f “(x)| in the range of integrations Note: n = # of intervals = # of points - 1.
Estimate error in Trap Rule: single subinterval of size h a<x1<a+h By fundamental theorem of calculous exact result but we don’t know the value of x1
Estimate error in Trap Rule: single subinterval of size h a<x2<a+h Trap rule approximation Exact from previous slide Difference between exact and trap rule
Estimate error in Trap Rule: n subinterval of size h 1 n is number of subinterval
Example: upper bound on the error in approximating by the composite trapezoid rule with 10 points |e| < (b-a)3|f “(x)|max /12n2 Plot |f “(x)| between 2 and 5 to find its maximum value
Note: maximum does no occur at either end point of integration. fplot(‘abs(sin(x))’,[2,5]) |f “(x)|max = 1 |sin(x)| radians (by default) Note: maximum does no occur at either end point of integration. Note: max(|sin(x)|) is not equal to max(sin(x)) for 2<x<5
(b-a)=3, |f “(x)|max = 1, n = 9 |e| < (b-a)3|f “(x)|max/12n2 ~ 0.0278 What is the actual absolute error in approximating by the composite trapezoid rule with 10 points Get the exact value by anti derivative Calculate trapezoid-rule estimate Actual absolute error = abs(traprule-exact)
Exact value = -0.6998 Trap-rule 10 pts = -0.6933 Actual absolute error = 0.0065 ~ 1% of exact value Upper bound on absolute error = 0.0278 ~ 4% of exact value
Deriving numerical integration methods
Most numerical integration formulas have the form which is a weighted average of values of the integrand. Example: composite Trapezoid rule w0 = wn = (b-a)/(2n) wk = (b-a)/n if 1< k<n-1 n = number of subintervals One approach to deriving such formulas is to replace the integrand by a polynomial.
replace integrand with line (polynomial of degree = 1) 2 This approach to deriving numerical integration approximations is hard to generalize to polynomials of degree higher than 1.
Derive Simpson’s rule with 1 pair of subintervals for range of integration [-a,a] Find weights that give exact results for polynomials up to degree 2
Derive Simpson’s rule with 1 pair of subintervals for range of integration [-a,a] Find weights that give exact results for polynomials up to degree 2
Composite Simpson’s rule: Generalized result for 1 pair of subintervals on [-a,a] to n pairs of subintervals on [a,b]
n pairs of subintervals
MatLab code for composite Simpson’s rule: Layout of points when npairs =4 i=0 1 2 3 4 5 6 7 8 |-----|------|-------|-------|------|-------|------|-------| n=2npairs=8 a a+h b
Review: MatLab code for composite Simpson’s rule: npairs =4 |-----|------|-------|-------|------|-------|------|-------| n=2npairs=8 a a+h b function A = simprule(fh,a,b,npairs) n=2*npairs; %number of sub intervals h=(b-a)/n; sum_even=0; for i=2:2:n-2 sum_even=sum_even+fh(a+i*h); end sum_odd=0; for i=1:2:n-1 sum_odd=sum_odd+fh(a+i*h); A=h*(fh(a)+4*sum_odd+2*sum_even+fh(b))/3;
Assignment 6, Due 3/6/19 Approximate the integral by Simpson rules with 3, 5, and 7 equally spaced points on [1,3]. Calculate the percent difference from the “exact” value, -18.79829683678703, in each case. Compare with results using the trapezoid rule (assignment #5).
Let {x0, x1, …, xn} be n+1 points on [a, b] where integrand evaluated, then the numerical integration formula Will be exact for polynomials of degree at least n. Derive weights by a system of n+1 linear equations obtained by requiring the formula to be exact for f(x) = 1, x, x2, …xn Trapezoid rule: (n = 1) exact for a line In general, error proportional to |f’’(z)| Simpson’s rule: (n = 2) exact for a cubic In general, error proportional to |f(4)(z)|
Gauss quadrature Gauss quadrature extents the idea of Simpson’s rule to its logical extreme: Give up all freedom about where the integrand will be evaluated to maximize the degree of polynomial for which formula is exact. Theorem: nodes {x0, x1, …, xn} exist on [a, b] such that is exact for polynomials of degree at most 2n+1 Nodes are zeros of polynomial g(x) of degree n+1 defined by for 0 < k < n How many coefficients are in a polynomial of degree n+1?
Proof of Gauss quadrature theorem
Exact for any set of n+1 nodes on [a,b] Exact for polynomial degree 2n+1
Example of Gauss quadrature 3 nodes on [-1, 1] such that approximation Step in deriving this quadrature result 1. Find cubic g(x) such that for 0 < k < 2 2. Find roots of g(x) 3. Find the weights A0, A1 and A2
Degree of g(x) = number of nodes conditions on g(x) = number of nodes Number of conditions not sufficient to determine all the coefficients Terms with odd powers of x in g(x) do not contribute due to symmetry
Determines ratio of C1 and C3 only. As noted before, number of conditions on g(x) insufficient to determine all 4 coefficients of a cubic but are sufficient to determine the zeros
Using plan (b) Compare with table 6.1, text p236
Table from C&K 6th ed. p236 Note symmetry of points and weights 1.odd number always contains 0 2.nonzero points symmetric about origin always have same weight
Points and weights of Gauss quadrature are determined for Every integral from a to b can transformed into integral from -1 to 1
Nodes and weights determined for [-1,1] can be used for [a,b] Given values of y from table, this x(y) determines values on [a,b] where integrand will be evaluated
Example: Review: Gauss quadrature with n+1 points Points and weights from 2, 3, 4, and 5 points:table 6.1, text p236 Example:
(no anti-derivative) with 2 nodes
Assignment 7, Due 3/20/19: Write MatLab functions for Gauss quadrature with 2, 3, 4 and 5 points. Apply these function to the integral Take -18.79829683678703 as the exact value. Hand in a copy of the command window where you called the Gauss functions and calculated the percent differences from the exact value (100|(exact-Gauss)/exact|). Compare percent differences using Gauss quadrature with 3 and 5 points to results using trapezoid and Simpson’s rules (HW 5 and 6).
Table from C&K 6th ed. p236 Note symmetry of points and weights 1.odd number always contains 0 2.nonzero points symmetric about origin always have same weight
fh(x); fh(x);
MatLab code for Gauss Quadrature with 2, 3, 4 and 5 point function [g2,g3,g4,g5]=GQ2345(fh,a,b); c1=(b-a)2; c2=(a+b)2; y2=sqrt(1/3); g2=fh(c2+c1*y2)+fh(c2-c1*y2); y3=sqrt(3/5); w3=5/9; g3=fh(c2)*8/9; g3=g3+w3*(fh(c2+c1*y3)+fh(c2-c3*y3)); y4(1)=sqrt((3-4*sqrt(0.3))/7); y4(2)=sqrt((3+4*sqrt(0.3))/7); w4(1)=0.5+sqrt(10/3)/12; w4(2)=0.5-sqrt(10/3)/12; g4=0; for i=1:2 g4=g4+w4(i)*(fh(c2+c1*y4(i))+fh(c2-c1*y4(i)); end y5(1)=sqrt((5-2*sqrt(10/7))/9); y5(2)=sqrt((5+2*sqrt(10/7))/9); w5(1)=0.3*(5*sqrt(0.7)-0.7)/(5*sqrt(0.7)-2)); w5(2)=0.3*(5*sqrt(0.7)+0.7)/(5*sqrt(0.7)+2)); g5=fh(c2)*128/225; g5=g5+w5(i)*((fh(c2+c1*y5(i))+fh(c2-c1*y5(i)); g2=c1*g2; g3=c1*g3; g4=c1*g4; g5=c1*g5;
More extensive tables of nodes and weights than given in text. Note that symmetry used to compress the table Verify that values for n = 2 to 5 are the same as n = 1 to 4 in table on P236 of text
Why is the integral hard to evaluate by numerical integration? Does the integral have a finite value? If so, can it be estimated by trapezoid rule, Simpson’s rule and Gauss quadrature?
Change the variable of integration in by the transformation y = ln(1/x) = -ln(x)
Change the variable of integration in y = ln(1/x) = -ln(x) when x=0 y=infinite when x=1 y=0 dy = -dx/x x(y) = exp(-y)
By arguments similar to the Guass-quadrature theorem, Laguerre derived an approximation to infinite integrals in which the integrand is evaluated at the zeroes of the Laguerre polynomials This formula only can be applied when f(x) is rapidly decreasing with increasing x, which is required for the integral to be finite
Note: Only applies to 1 type of integral Less symmetry than with Gauss quadrature Use first and last columns
Pseudo-code LQ2345(f) % Leguerre quadrature for integral between 0 and infinity with 2-5 points enter points and weights from table initialize sum for each number of points calculate integrand where it is to be sampled calculate weighted average of integrand samples return values
Gauss with x as integration variable Laguerre with y = -ln(x) as integration variable