Numerical Integration (Chapters 5 & 6, C&K 6th edition)

Slides:



Advertisements
Similar presentations
6. 4 Integration with tables and computer algebra systems 6
Advertisements

Numerical Integration
Lecture 5.
ES 240: Scientific and Engineering Computation. Chapter 17: Numerical IntegrationIntegration  Definition –Total area within a region –In mathematical.
1 Chapter 5 Numerical Integration. 2 A Review of the Definite Integral.
Section 5.7 Numerical Integration. Approximations for integrals: Riemann Sums, Trapezoidal Rule, Simpson's Rule Riemann Sum: Trapezoidal Rule: Simpson’s.
Numerical Integration of Functions
NUMERICAL DIFFERENTIATION AND INTEGRATION
Lecture 3: Integration. Integration of discrete functions
MANE 4240 & CIVL 4240 Introduction to Finite Elements Numerical Integration in 1D Prof. Suvranu De.
CHAPTER 4 THE DEFINITE INTEGRAL.
MA/CS 375 Fall MA/CS 375 Fall 2002 Lecture 32.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Numerical Differentiation and Integration Part 6 Calculus.
19.5 Numeric Integration and Differentiation
Integrals 5.
Chapter 4 Numerical Differentiation and Integration 1/16 Given x 0, approximate f ’(x 0 ). h xfhxf xf h )()( lim)('    x0x0 x1x1 h x1x1 x0x0.
1 Chapter 5 Numerical Integration. 2 A Review of the Definite Integral.
3. Numerical integration (Numerical quadrature) .
Numerical Integration Pertemuan 7 Matakuliah: S0262-Analisis Numerik Tahun: 2010.
Numerical Computation
1 Chapter 7 NUMERICAL INTEGRATION. 2 PRELIMINARIES We use numerical integration when the function f(x) may not be integrable in closed form or even in.
4.6 Numerical Integration -Trapezoidal Rule -Simpson’s Rule
Chapters 5 and 6: Numerical Integration
Wicomico High School Mrs. J. A. Austin AP Calculus 1 AB Third Marking Term.
MA2213 Lecture 4 Numerical Integration. Introduction Definition is the limit of Riemann sums I(f)
 Finding area of polygonal regions can be accomplished using area formulas for rectangles and triangles.  Finding area bounded by a curve is more challenging.
Homework questions thus far??? Section 4.10? 5.1? 5.2?
Integration Copyright © Cengage Learning. All rights reserved.
Chapter 5-The Integral Calculus, 2ed, by Blank & Krantz, Copyright 2011 by John Wiley & Sons, Inc, All Rights Reserved.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 22.
5. Integration 2.Quadrature as Box Counting 3.Algorithm: Trapezoid Rule 4.Algorithm: Simpson’s Rule 5.Integration Error 6.Algorithm: Gaussian Quadrature.
Numerical Differentiation and Quadrature (Integration)
Section 5.9 Approximate Integration Practice HW from Stewart Textbook (not to hand in) p. 421 # 3 – 15 odd.
CHAPTER 3 NUMERICAL METHODS
Riemann Sums and The Definite Integral. time velocity After 4 seconds, the object has gone 12 feet. Consider an object moving at a constant rate of 3.
The purpose of Chapter 5 is to develop the basic principles of numerical integration Usefule Words integrate, integral 积分(的), integration 积分(法), quadrature.
Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.
Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant.
Clicker Question 1 What is ? (Hint: u-sub) – A. ln(x – 2) + C – B. x – x 2 + C – C. x + ln(x – 2) + C – D. x + 2 ln(x – 2) + C – E. 1 / (x – 2) 2 + C.
Approximating Antiderivatives. Can we integrate all continuous functions? Most of the functions that we have been dealing with are what are called elementary.
Assignment 1: due 1/19/16 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
Chapter 5 Integrals 5.2 The Definite Integral
Chapter 11 Polynomial Functions
CHAPTER 3 NUMERICAL METHODS
NUMERICAL DIFFERENTIATION Forward Difference Formula
5.5 The Trapezoid Rule.
Chapter 7 Numerical Differentiation and Integration
Copyright © Cengage Learning. All rights reserved.
Gauss Quadrature Rule of Integration
Chapter 22.
Taylor series in numerical computations (review)
The Area Question and the Integral
Class Notes 18: Numerical Methods (1/2)
Chapter 7 Numerical Differentiation and Integration
Copyright © Cengage Learning. All rights reserved.
Composite Numerical Integration
Gauss Quadrature Rule of Integration
Copyright © Cengage Learning. All rights reserved.
Numerical Computation and Optimization
Finding zeros (also called roots) of a function
Chapters 5 and 6: Numerical Integration
SKTN 2393 Numerical Methods for Nuclear Engineers
Assignment 1: due 1/16/19 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
Numerical Integration
Objectives Approximate a definite integral using the Trapezoidal Rule.
Assignment 1: due 1/17/19 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
Chapter 5 Integration Section R Review.
Section 4 The Definite Integral
Numerical Integration
Presentation transcript:

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