Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.

Slides:



Advertisements
Similar presentations
Numerical Integration
Advertisements

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 DIFFERENTIATION AND INTEGRATION
Today’s class Romberg integration Gauss quadrature Numerical Methods
CHAPTER 4 THE DEFINITE INTEGRAL.
Integrals and the Fundamental Theorem (1/25/06) Today we review the concepts of definite integral, antiderivative, and the Fundamental Theorem of Calculus.
Chapter 5 Integrals 5.2 The Definite Integral In this handout: Riemann sum Definition of a definite integral Properties of the definite integral.
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM.
NUMERICAL DIFFERENTIATION The derivative of f (x) at x 0 is: An approximation to this is: for small values of h. Forward Difference Formula.
CISE301_Topic7KFUPM1 SE301: Numerical Methods Topic 7 Numerical Integration Lecture KFUPM Read Chapter 21, Section 1 Read Chapter 22, Sections 2-3.
CISE301_Topic71 SE301: Numerical Methods Topic 7 Numerical Integration Lecture KFUPM (Term 101) Section 04 Read Chapter 21, Section 1 Read Chapter.
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
Simpson’s 1/3 rd Rule of Integration. What is Integration? Integration The process of measuring the area under a.
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.
The Definite Integral.
4.6 Numerical Integration -Trapezoidal Rule -Simpson’s Rule
1 Simpson’s 1/3 rd Rule of Integration. 2 What is Integration? Integration The process of measuring the area under a curve. Where: f(x) is the integrand.
Chapters 5 and 6: Numerical Integration
CSE 330 : Numerical Methods
Wicomico High School Mrs. J. A. Austin AP Calculus 1 AB Third Marking Term.
Integration. Antiderivatives and Indefinite Integration.
MA2213 Lecture 4 Numerical Integration. Introduction Definition is the limit of Riemann sums I(f)
AP CALCULUS PERIODIC REVIEW. 1: Limits and Continuity A function y = f(x) is continuous at x = a if: i) f(a) is defined (it exists) ii) iii) Otherwise,
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
Simpson Rule For Integration.
CISE301_Topic11 CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4:
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.
3. Fundamental Theorem of Calculus. Fundamental Theorem of Calculus We’ve learned two different branches of calculus so far: differentiation and integration.
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)
CHAPTER 3 NUMERICAL METHODS
Barnett/Ziegler/Byleen Business Calculus 11e1 Chapter 13 Review Important Terms, Symbols, Concepts 13.1 Antiderivatives and Indefinite Integrals A function.
The Definite Integral.
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.
Maclaurin and Taylor Polynomials Objective: Improve on the local linear approximation for higher order polynomials.
SE301_Topic 6Al-Amer20051 SE301:Numerical Methods Topic 6 Numerical Integration Dr. Samir Al-Amer Term 053.
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.
NUMERICAL DIFFERENTIATION Forward Difference Formula
Copyright © Cengage Learning. All rights reserved.
Gauss Quadrature Rule of Integration
Chapter 22.
The Area Question and the Integral
Fundamental Theorem of Calculus (Part 2)
NUMERICAL INTEGRATION
Copyright © Cengage Learning. All rights reserved.
Gauss Quadrature Rule of Integration
Copyright © Cengage Learning. All rights reserved.
Numerical Computation and Optimization
Chapters 5 and 6: Numerical Integration
SKTN 2393 Numerical Methods for Nuclear Engineers
Numerical Integration
Numerical Integration (Chapters 5 & 6, C&K 6th edition)
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.
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
Numerical Integration
Presentation transcript:

Chapters 5 and 6: Numerical Integration 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

Usually x 1 = a and x n = b Replace f(x) on [a,b] by line p(x) Multiple intervals with f(x) on [x k-1,x k ] replaced by a line

Trapezoid rule: In-class work: Write a MatLab code for Trapezoid rule with arbitrary points where the integrand is evaluated. Input 2 arrays: x = values where the integrand is evaluated. f = values of the integrand at those points Output: trapezoid rule approximation to integral

Does your trapezoid rule code look something like function A=trap_arbitrary_points(x,f) n=length(x); sum=0; for k=2 to n sum=sum+(x k – x k-1 )(f k + f k-1 )/2; end A=sum; end

Write a script to apply trapezoid rule function to estimate with x k = exp(y k ) where y k =linspace(0,ln(5),10) What is the percent error in your result?

Does your script look something like inline definition of integrand y-vector defined by linspace(0,log(5),10) x-vector defined by exp(y) A = trap_arbitrary_points(x,integrand(x)) exact = result using anti-derivative percent difference=100abs((A-exact)/exact)

Expand your script to estimate with 10 equally spaced points between 1 and 5. What is the percent error in this result? How does it compare to your result with unequally spaced points where the integrand was evaluated?

Does your new script look something like inline definition of integrand y-vector defined by linspace(0,log(5),10) x-vector defined by exp(y) A1 = trap_arbitrary_points(x,integrand(x)) exact=result using anti-derivative percent difference1=100abs((A1-exact)/exact) x-vector defined by linspace(1,5,10) A2 = trap_arbitrary_points(x,integrand(x)) percent difference2=100abs((A2-exact)/exact) Which spacing gave a more accurate trapezoid-rule approximation?

What is the difference between the two choices of where the integrand is evaluated? Display the 2 choices Plot the 2 choices What is your conclusion? Can you rationalize the difference in accuracy?

Does your new script look something like inline definition of integrand y-vector defined by linspace(0,log(5),10) x-vector defined by exp(y) A1 = trap_arbitrary_points(x,integrand(x)) exact=result using anti-derivative percent difference1=100abs((A1-exact)/exact) newx-vector defined by linspace(1,5,10) A2 = trap_arbitrary_points(newx,integrand(newx)) percent difference2=100abs((A2-exact)/exact) disp(x) disp(newx) plot(x,’^-’) plot(newx,’o-’)

Changing 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)

Expand your script to approximate x(y)= exp(y) using the integration variable y with 10 equally spaced points between 0 and ln(5)

Does your new script look something like inline definition of integrand y-vector defined by linspace(0,log(5),10) x-vector defined by exp(y) A1 = trap_arbitrary_points(x,integrand(x)) exact=result using anti-derivative percent difference1=100abs((A1-exact)/exact) x-vector defined by linspace(1,5,10) A2 = trap_arbitrary_points(x,integrand(x)) percent difference2=100abs((A2-exact)/exact) newx=exp(y) newintegrand=newx.*exp(-newx) A3 = trap_arbitrary_points(y,newintegrand) percent difference3=100abs((A3-exact)/exact) Which method of estimating the integral is most accurate?

In-class work last Thursday: Estimate the integral by the trapezoid rule with 10 points when the points are chosen in the following ways: 1. Equally spaced on [1, 5] 2. x k = exp(y k ) where y k = 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) , (2) , (3)

What we learned from in-class work last Thursday: 1. Concentrating sample points where integrand is large gives a slight improvement in accuracy over equally-spaced sample points. 2. A different integration variable may give a significant improvement in accuracy.

Assignment 5, Due 2/4/16 On page 242 of the text (6 th edition), the value of is given as , which can be taken as the “exact” value. Estimate this integral by the trapezoid rule with 10 points when the points are chosen in the following ways: 1. Equally spaced on [1, 3] 2. x k = exp(y k ) where y k = linspace(0,ln(3),10) 3. Equally spaced on [0, ln(3)] in the new integration variable y = ln(x). Calculate the percent difference from the exact value in each case

Accuracy increases with the number of points where the integrand is evaluated. More trapezoids means shorter bases. Line segments replacing the integrand are shorter and better able to follow changes in the integrand over the range of integration. With equally spaced sample points, the formula for trapezoid rule simplifies due to common width of base. Called the “composite” trapezoid rule Trapezoid Rule

When the integrand is replaced by a polynomial, the integral between a and b, can be done analytically if we know the coefficients of the polynomial. Given values of the integrand f(x) at n points we can determine a polynomial of degree n-1 that passes through those points. If we are given only f(a) and f(b) we can determine the equation of a line (degree=1) that passes through them.

Which is the same result that we got using the area of trapezoid 2

Use Taylor formula to derive an analytic expression for the error in the composite trapezoid rule as a function of the number of points where the integrand is evaluated.

Estimate error in Trap Rule: single subinterval of size h exact result but we don’t know the value of   a    a+h By fundamental theorem of calculous

Estimate error in Trap Rule: single subinterval of size h Difference between exact and trap rule Exact from previous slide Trap rule approximation a    a+h

Estimate error in Trap Rule: n subinterval of size h n is number of subinterval 1

by the composite trapezoid rule with 10 points  = (b-a) 3 |f “(  )|/12n 2 a<  <b but we don’t know its value Can’t get a value for f ”(  but we can bound |f “(  )|  < (b-a) 3 |f “(  )| max /12n 2 Plot |f “(x)| between a and b to fine its maximum value Remember! n is the number of subinterval In class work: Estimate the error in approximating

radians (by default) |sin(x)| fplot(‘abs(sin(x))’,[2,5])

by the composite trapezoid rule with 10 points Get the exact value by anti derivative Calculate trapezoid-rule estimate Actual error = abs(estimate-exact) In class work: What is the actual the error in approximating

by the composite trapezoid rule with an error less than 0.25x10 -4  = (b-a)h 2 |f “(  )|/12 < 0.25x10 -4 solve for h Same problem different form: estimate how small h must be to evaluate

by the composite trapezoid rule with 10 points  < (b-a) 3 |f “(  )| max /12n 2 Don’t forget the chain rule when you take the derivative Assignment 6, Due 2/9/16 Estimate the error in approximating

Does your plot of |f ’’(x)| look something like this? Under tools, select data cursor Is your estimate of upper bound on absolute error in trap-rule ~ 31.7? Compare to trap-rule result with 10 points. What does this tell you about your trap-rule estimate of the integral?

Plot of f(x) Would you expect trap-rule with 10 points to give a good result? Trap-rule with 10 points to give an idea about the “scale” of

If the scale of is ~ 0.2, then might be a reasonable upper bound on absolute error in trap-rule approximation How many points are required to reduce the estimated upper bound on the trap-rule error to ~ 0.002? Better question: How small must h be to achieve this? Require  = (b-a)h 2 |f “(  )| max /12 < 0.002; solve for h Given h, find npts = 1+(b-a)/h Evaluate trap-rule with npts points is what percent of your trap-rule estimate with npts?

Solution: h 2 < 12(0.002)/(6(142.6)) ~ 2.8x10 -5 h < npts > With 1134 points, the trap-rule approximation is The trap-rule approximation with 10 pts ( ) was not acceptable, as expected from the upper bound of 31.7 on its absolute error. The scale of the trap-rule approximation with 10 pts is correct and provides enough information to design trap-rule approximation with high accuracy. MatLab’s numerical integration method, quad, returns a value Absolute error in trap-rule with 1134 points is much less than the value of that we required.

Problems from text on application of error formulas p p p p p a p227 Numbers apply to 6 th addition

Most numerical integration formulas have the form Example: composite Trapezoid rule  0 =  n = 1/(2n)  k = 1/n if 1< k<n-1 n = number of subintervals One approach to deriving such formulas is to replace the integrand by polynomial of degree n

Lagrange interpolation formula is a way to replace integrand by a polynomial of degree n that is equal to the integrand at n+1 user-supplied points. See chapter 4 page th edition 2 Review: Trapezoid rule derived by replacing integrand by line equal to integrand at 2 points

General formula for a polynomial of degree n that passes through a set of n+1 user-supplied point {x k,f(x k )}. p(x)

Example: derive Simpson’s rule

Simpson’s rule for one pair of subintervals

n pairs of subintervals

Pseudo code for Simpson’s rule A=function simprule(fh,a,b,npairs) h=(b-a)/2npairs sumeven=0 sumodd=0 for i=2,2,2npairs-2 sumeven end for i=1,2,2npairs-1 sumodd end A=h(fa+4sumodd+2sumeven+fb)/3

Assignment 7, Due 2/18/16 1. Approximate the integral 2. Use the Lagrange interpolation formula to derive a trapezoid rule approximation to the integral Using the value of the integrand –a and +a. Show that your result is the same as that derived from the area of a trapezoid? by the trapezoid and Simpson rules with 5 equally spaced points on [1,3]. Calculate the percent difference from the “exact” value, , in each case.

Error bound for composite Simpson’s rule |error| < (b-a)h 4 |f (4) (  )| max /180 a <  < b (text p221) h = width subintervals Since |error| proportional to |f (4) (  )|, Simpson’s rule is exact for a cubic, one degree higher than we should expect from a formula derived by replacing the integrand by a polynomial of degree 2. Test your CSR code on integral of x 3 from 0 to 2 with 5 points. Do you get the exact value?

Problem text p228, 6 th ed Evaluated by composite Simpson’s rule with 5 points Bound the error. Compare to actual |error|. |error| = (b-a)h 4 |f (4) (  )|/180a <  < b (text p221) h = width subintervals

Problem text p228, 6 th ed composite Simpson’s rule with 5 points = |exact-CSR| = |error| = (b-a)h 4 |f (4) (  )|/180a <  < b (text p221) h = width subintervals In this case, h = 0.25 f (4 ) (x) = 24/x 5 |f (4) (  )| < 24 1<  <2 |error| < (1)(0.25) 4 24/180 =

by the CSR with an error less than Estimate the minimum number of points to evaluate |error| = (b-a)h 4 |f (4) (  )|/180a <  < b (text p221) h = width subintervals Hint: Find upper bound on h. Use to find a lower bound on pairs of subintervals. Use to find lower bound on number of points.

by the CSR with an error less than Estimate the minimum number of points to evaluate |error| = (b-a)h 4 |f (4) (  )|/180a <  < b (text p221) h = width subintervals h < npairs > npairs min = 4 npts = 2(npairs min ) + 1= 9

Simpson’s rule by simpler approach In each pair of subintervals of the CSR, we replace integrand by a polynomial of degree 2 that is equal to the integrand at 3 points. Lagrange interpolation formula gives us that polynomial without having to solve a 3x3 system of linear equations for the coefficients. We integrate the Lagrange polynomial over the pair of subintervals to get the weights in the Simpson’s rule approximation. An alternative and simpler approach is the develop a system of linear equations for the weights.

Find weights that give exact results for polynomials up to degree 2 Simpson’s rule with 1 pair of subintervals for range of integration [-a,a]

Problems from text p240 on deriving integration formulas

Assignment 8, Due 2/23/16 problem text p240 Find A and B by requiring formula to be exact for f(x) = 1 and f(x) = x

Why is this not the Simpson’s rule?

Which is likely to be more accurate?

Review: Let {x 0, x 1, …, x n } be n+1 points on [a, b] where integrand evaluated, then numerical integration formula Will be exact for polynomials of degree at least n Trapezoid rule: (n = 1) exact for a line In general, error proportional to |f’’(  )| Simpson’s rule: (n = 2) exact for a cubic In general, error proportional to |f (4) (  )|

Gauss quadrature extents Simpson’s rule idea 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. nodes {x 0, x 1, …, x n } 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 Gauss quadrature

Proof of Gauss quadrature theorem

For any set of n+1 nodes on [a,b]

Summary of Gauss Quadrature Theorem For any set of n+1 nodes on [a,b], is exact for polynomials up to degree n For the special set of node in Gauss quadrature, the formula is correct of polynomials of degree at most 2n+1 Nodes and weights, A i, depend on both [a,b] and n Scaling property of integrals lessens the impact of these dependences

Terms with odd powers of x in g(x) do not contribute due to symmetry Degree of g(x) = number of nodes Number of conditions on g(x) = number of nodes Number of conditions not Sufficient to determine all coefficients

Determines ratio of C 1 and C 3 only. As noted before, number of conditions on g(x) insufficient to determine all 4 coefficients of a cubic

Using plan (b) Compare with table 6.1, text p236

n = number of intervals Note symmetry of points and weights

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: Summary: Gauss quadrature n subintervals

with 2 nodes (no anti-derivative)

Pseudo-code GQ2345(a,b,f) % Gauss quadrature for integral of f between a and b with 2-5 points calculate points and weights by formulas in text p236 note symmetry calculate scale factor and coefficients in x(y) for each number of points calculate where in [a,b] integrand will be sampled calculate samples of integrand calculate weighted average of integrand samples multiply by scale factor return values

exact=2 quad= Trap rule 800 = Code and tests of 3 point Gauss quadrature

Assignment 9, Due 3/1/16: Make a table that includes the estimated value and percent difference (100|(exact-estimate)/exact| in approximated by the composite trapezoid rule, the composite Simpson’s rule and Gauss quadrature when the integrand is evaluated at 2, 3, 4, and 5 points, if possible. Hand in your MatLab codes. Quiz on numerical integration 3/8/16

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

Note 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 be sampled calculate weighted average of integrand samples return values

Approximate integral in x variable by Guass quadrature with 2, 3, 4, and 5 points Approximate integral in y variable by Laguerre quadrature with 2, 3, 4, and 5 points Report your results as a table with approximate values and percent difference from exact. Assignment 10, Due 3/3/16:

Gauss with x as integration variable Laguerre with y = -ln(x) as integration variable