MA/CS 375 Fall 2002 Lecture 32 MA/CS 375 Fall 2002 1 Review Roots of a Polynomial • Suppose we wish to find all the roots of a polynomial of order P • Then there are going to be at most P roots!. • We can use a variant of Newton’s method. MA/CS 375 Fall 2002 2 Review Newton Scheme For Multiple Root Finding Initiate guesses to the roots x 1 ,x 2 ,..x P L oop over k= 1:P Iterate: xk xk f df x k dx xk i k 1 1 f xk i 1 x k x i to find x k to a given tolerance E nd loop MA/CS 375 Fall 2002 3 (applied to find roots of Legendre polynomials) Multiple Root Finder Review + Correction MA/CS 375 Fall 2002 Should read abs(delta) > tol 4 Review Legendre Polynomials • Legendre polynomials are a special set of polynomials which are orthogonal in the L2 inner product: 1 L n x L m x dx 0 if n m 1 MA/CS 375 Fall 2002 5 Review Legendre Polynomials • Legendre polynomials can be calculate using the following recursion relation: L0 x 1 L1 x x 2n 1 n L n 1 x x Ln x L n 1 x n= 1,2,... n 1 n 1 MA/CS 375 Fall 2002 6 Review Roots of the 10th Order Legendre Polynomial Notice how they cluster at the end points MA/CS 375 Fall 2002 7 Numerical Quadrature • A numerical quadrature is a set of two vectors. • The first vector is a list of x-coordinates for nodes where a function is to be evaluated. • The second vector is a set of integration weights, used to calculate the integral of a function which is given at the nodes MA/CS 375 Fall 2002 8 Example of Quadrature • Say we wish to calculate an approximation to 1 the integral of f over [-1,1] : f x dx 1 • Suppose we know the value of f at a set of N points then we would like to find a set of weights w1,w2,..,wN so that: 1 i N f x dx w f x i 1 MA/CS 375 Fall 2002 i i 1 9 Example: Simpson’s Rule Recall: • The idea is to sample a function at N points. • Then using a shifting stencil of 3 points construct a quadratic interpolant through those 3 points. • Then integrate the area under the interpolant in the range bracketed by the three points. • Sum up all the contributions from the sets of three points. MA/CS 375 Fall 2002 10 Example: Simpson’s Rule 1 f x dx 1 2 3( N 1) f x 4 f x 2 f x 4 f x ... f x 1 2 3 nodes 4 { x1 , x 2 , x 3 , x 4 , N , xN } n 1 xn 1 2 N 1 quadrature: w eights w 1 , w 1 , w 1 , w 1 , w1 , w N MA/CS 375 Fall 2002 w2 , w4 , w6 , , w N 1 w3 , w5 , w7 , , w N 2 , w1 2 3 N 1 8 3 N 1 4 3 N 1 11 Example: Simpson’s Rule 1 f x dx 1 2 3( N 1) f x 4 f x 2 f x 4 f x ... f x 1 2 3 4 N becomes: 1 f x dx w f x w f x w f x .. w 1 1 2 2 3 3 N f xN 1 in summation notation: 1 nN f x dx w f x n MA/CS 375 Fall 2002 1 n 1 n 12 Newton-Cotes Formula • The next approach we are going to use is the well known Newton-Cotes quadrature. • Suppose we are given a set of points x1,x2,..,xN. Then we require that the constant is exactly integrated: w1 x w 2 x 0 1 MA/CS 375 Fall 2002 0 2 wN x 0 N 1 x x dx 1 1 1 1 1 0 13 Now we require that 1,x,x2,..,xN-1 are integrated exactly w1 x w 2 x 0 1 0 2 wN x x x dx 1 1 1 1 N x x dx 2 1 1 1 0 1 w1 x w 2 x 1 1 1 2 wN x 2 w1 x w2 x N 1 2 wN x N 1 N x 1 MA/CS 375 Fall 2002 1 1 1 N 1 1 1 0 N 1 N 1 1 x dx N 1 N 14 In Matrix Notation: 1 1 1 0 x N w1 2 2 1 1 1 w2 xN 2 N 1 xN wN N N 1 1 N 1 x1 1 x 1 N 1 x1 0 x x 0 2 x 1 2 N 1 2 1 Notice anything familiar? MA/CS 375 Fall 2002 15 It’s the transpose of the Vandermonde matrix 11 1 1 0 x N w1 2 2 1 1 1 w2 xN 2 N 1 xN wN N N 1 1 N 1 x1 1 x1 t V w N 1 x1 0 MA/CS 375 Fall 2002 x 0 2 x 1 2 N 1 x2 16 Integration by Interpolation • In essence this approach uses the unique (N-1)’th order interpolating polynomial If and integrates the area under the If instead of the area under f • Clearly, we can estimate the approximation error using the estimates for the error in the interpolation we used before. MA/CS 375 Fall 2002 17 Newton-Cotes Weights 11 1 1 12 1 2 2 N N 1 1 N 1 w1 w2 wN MA/CS 375 Fall 2002 1 t V 18 Using Newton-Cotes Weights 1 1 MA/CS 375 Fall 2002 f x dx i N wi f xi w f t i 1 19 Using Newton-Cotes Weights (Interpretation) i N 1 f x dx w f x w i f i 1 1 11 1 1 i t 1 1 1 2 2 2 1 1 N N N 1 V f i.e. we calculate the coefficients of the interpolating polynomial expansion using the Vandermonde, then since we know the integral of each term we can sum up the integral of each term to get the total. MA/CS 375 Fall 2002 20 Matlab Function for Calculating Newton-Cotes Weights MA/CS 375 Fall 2002 21 Demo: Matlab Function for Calculating Newton-Cotes Weights 1) set N=5 points 2) build equispaced nodes 3) calculate NC weights 4) evaluate F=X^3 at nodes 5) evaluate integral 6) F is anti-symmetric on [-1,1] so its integral is 0 7) Answer correct MA/CS 375 Fall 2002 22 Individual Exercise • Download the contents of: http://www.math.unm.edu/~timwar/MA375F02/Integration • make sure your matlab path points to your copy of this directory • using a script figure out what order polynomial the weights produced with newtoncotes can exactly integrate for a given set of N points (say N=3,4,5,6,7,8) created with linspace MA/CS 375 Fall 2002 23 Gauss Quadrature • The construction of the Newton-Cotes weights does not utilize the ability to choose the distribution of nodes for greater accuracy. • We can in fact choose the set of nodes to increase the order of polynomial that can be integrated exactly using just N points. MA/CS 375 Fall 2002 24 Suppose: f x x rxsx If w here: f P If 2 p 1 xi 1,1 f xi w here If P s x i 0 w here s P rP MA/CS 375 Fall 2002 p p 1 p 1,1 1,1 1,1 25 Remainder term, which must have p roots located at the interpolating nodes Suppose: f x x rxsx If w here: f P If 2 p 1 xi 1,1 f xi w here If P s x i 0 w here s P rP MA/CS 375 Fall 2002 p p 1 p 1,1 1,1 1,1 26 Let’s integrate this formula for f over [-1,1] f x 1 x rxsx If 1 f x dx 1 1 If 1 x dx r x s x dx 1 i N 1 w f x r x s x dx i i 1 i 1 At this point we can choose the nodes {xi}. If we choose them so that they are the p+1 roots of the (p+1)’th order Legendre function then s(x) is in fact the N=(p+1)’th order Legendre function itself!. MA/CS 375 Fall 2002 27 1 1 f x dx 1 1 If 1 x d x s x r x d x 1 1 1 If 1 x d x 1 i N LN x r x dx 1 w f x i i 1 i LN x r x dx 1 • But we also know that if r is a lower order polynomial than (p+1)’th order, it can be expressed as a linear combination of Legendre polynomials {L1, L2, L3 , … , LN }. • By the orthogonality of the Legendre polynomials we know that the s is in fact orthogonal to Lp+1 MA/CS 375 Fall 2002 28 Hence: 1 i N f x dx w f x i 1 i for all f P 2 N 1 i 1 i.e. the quadrature is exact for all polynomials of order up to p=(2N-1) MA/CS 375 Fall 2002 29 Summary of Gauss Quadrature • We can use the multiple root finder to locate the roots of the N’th order Legendre polynomial. • We can then use the Newton-Cotes formula with the roots of the N’th order Legendre polynomial to calculate a set of N weights. • We now have a quadrature !!! which will integrate polynomials of order 2N-1 with N points MA/CS 375 Fall 2002 30 Team Exercise • Use the root finder (gaussNR) and Newton-Cotes routines (newtoncotes) to build a quadrature for N points (N arbitrary). • Use it to integrate exp(x) over the interval [-1,1] • Use it to integrate 1./(1+25*x.^2) over the interval [-1,1] • For N=2,3,4,5,6,7,8,9 plot the integration error for both functions on the same graph. MA/CS 375 Fall 2002 31