Chapters 5 and 6: Numerical Integration

Slides:



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

Numerical Integration
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
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.
Numerical Integration
MA/CS 375 Fall MA/CS 375 Fall 2002 Lecture 32.
CISE301_Topic7KFUPM1 SE301: Numerical Methods Topic 7 Numerical Integration Lecture KFUPM Read Chapter 21, Section 1 Read Chapter 22, Sections 2-3.
Quadrature Greg Beckham. Quadrature Numerical Integration Goal is to obtain the integral with as few computations of the integrand as possible.
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
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.
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)
The Integral chapter 5 The Indefinite Integral Substitution The Definite Integral As a Sum The Definite Integral As Area The Definite Integral: The Fundamental.
Copyright © 2014, 2010 Pearson Education, Inc. Chapter 2 Polynomials and Rational Functions Copyright © 2014, 2010 Pearson Education, Inc.
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.
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
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.
SE301_Topic 6Al-Amer20051 SE301:Numerical Methods Topic 6 Numerical Integration Dr. Samir Al-Amer Term 053.
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 11 Polynomial Functions
NUMERICAL DIFFERENTIATION Forward Difference Formula
Copyright © Cengage Learning. All rights reserved.
Gauss Quadrature Rule of Integration
Numerical Integration Formulas
Chapter 22.
Taylor series in numerical computations (review)
The Area Question and the Integral
Class Notes 18: Numerical Methods (1/2)
The normal distribution
Chapter 7 Numerical Differentiation and Integration
NUMERICAL 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
SKTN 2393 Numerical Methods for Nuclear Engineers
Simpson’s 1/3rd Rule of Integration
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
Numerical Integration (Chapters 5 & 6, C&K 6th edition)
9.7 Taylor Polynomials & Approximations
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
Chapter 4, Integration of Functions
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

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

As done here for a line, use values of the integrand to determine Any numerical integration formula that involves n+1 evaluations of the integrand can be made exact to polynomials of degree up to n. Example: n=2, exact for a line. 2 As done here for a line, use values of the integrand to determine coefficients of the polynomial then use anti-derivatives to get the exact results.

Trapezoid rule: 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

function A=trap_arbitrary_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.

Write a script to apply trapezoid rule function to estimate with x=linspace(1,5,10) (10 equally spaced points) Define percent error in your result as 100*|(trap-exact)/exact| Display the exact value, trap-rule approximation and their percent difference.

MatLab script to apply trapezoid rule function 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 your script to estimate with 10 “logarithmically” spaced points between 1 and 5. y-vector defined by linspace(0,log(5),10) x-vector defined by exp(y) What is the percent error in this result?

Extended MatLab script to apply trapezoid rule to with logarithmically 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]) y=linspace(0,log(5),10) newx=exp(y) A2 = trap_arbitrary_points(newx,integrand(newx)) PD2 = 100*abs((A2-exact)/exact) disp([A2, exact, 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?

What is the essential difference between equal and logarithmic spacing?

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) Expand your script to approximate using the integration variable y with 10 equally spaced points between 0 and ln(5)

Extended MatLab script to apply trapezoid rule to with a new integration 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([A2,exact,PD2]) xofy=newx A3 = trap_arbitrary_points(y, xofy.*exp(-xofy)) PD3=100*abs((A3-exact)/exact) disp([A3,exact,PD3])

Summary of results to 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. 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, additional factors affect the accuracy of approximation. Two important factors are: 1. Where the integrand is evaluated. 2. Which integration variable is used.

Assignment 5, Due 1/31/19 On page 242 of the text (6th edition), the value of is given as -18.79829683678703, 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. xk = exp(yk) where yk= 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

Graph of integrand in Assignment 5 Integrand(x) X

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

Write 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

Estimating 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 = (b-a)h2|f “(x)|/12, since h=(b-a)/n, where a<x<b Note: n = # of intervals, not # of points.

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

Apply theory to

Estimate the error in approximating by the composite trapezoid rule with 10 points |e| = (b-a)3|f “(x)|/12n2 a<x<b but we don’t know its value Can’t get a value for f ”(e) but we can bound |f “(x)| |e| < (b-a)3|f “(x)|max/12n2 Plot |f “(x)| between a and b to find its maximum value Remember! n is the number of subinterval

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% Trap-rule value Upper bound on absolute error = 0.0278 ~ 4% Trap-rule value

Assignment 6, Due 2/7/19 Estimate an upper bound on the absolute error in approximating by the composite trapezoid rule with 10 points. Show a plot to determine the maximum absolute value of the second derivative of the integrand. Assume the exact value is 0.882081 What is the actual absolute error in this trapezoid rule approximation?

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 As with the trapezoid rule, one approach to deriving such formulas is to replace the integrand by a polynomial.

Review: replace integrand with line (polynomial of degree = 1) 2 This approach to find a polynomial that is equal to the integrand at user specified points is hard to generalize to higher degrees

Lagrange interpolation formula is a general formula for a polynomial of degree n that passes through a set of n+1 user-supplied point {xk,f(xk)}.

Lagrange interpolation formula is a general formula for a polynomial of degree n that passes through a set of n+1 user-supplied point {xk,f(xk)}

Review Use Lagrange interpolation formula to derive Simpson’s rule Replace the integrand by a polynomial of degree 2

Example: derive Simpson’s rule

Simpson’s rule for one pair of subintervals

Simpler way to derive Simpson’s rule

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

Assignment 7, Due 2/14/19 Find A and B as a function of a and b by requiring formula to be exact for f(x) = 1 and f(x) = x.

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 Number of subintervals is even Number of points is odd

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 8, Due 2/14/19 Approximate the integral by the trapezoid and 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.

Error bound for composite Simpson’s rule |error| < (b-a)h4|f(4)(x)|max/180 a < x < b (text p221) h = width subintervals Since |error| proportional to |f(4)(x)|, 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 x3 from 0 to 2 with 5 points. Do you get the exact value?

Review: Let {x0, x1, …, xn} 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’’(z)| Simpson’s rule: (n = 2) exact for a cubic In general, error proportional to |f(4)(z)|

Gauss quadrature 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 can be 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 9, Due 2/21/19: Write a MatLab code for Gauss quadrature with 2, 3, 4 and 5 points. Make a table that includes the estimated value and percent difference (100|(exact-estimate)/exact| in Gauss quadrature when the integrand is evaluated at 2, 3, 4, and 5 points. Take -18.79829683678703 as the exact value. Compare results with 3 and 5 points to results from HW 8 for trapezoid and Simpson’s rule. Quiz #2: numerical integration: 3/5/19

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

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-c1*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 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

Assignment 10, Due 2/26/19: 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. Quiz #2: numerical integration: 3/5/19

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

Quiz #2: numerical integration: 3/5/19 Suggested problems from the text on numerical integration   Chapter 5.1 Problems 1, 6 Computer problem 2 Chapter 5.2 Problems 2, 4, 7, 12, 13, 15, 16 Computer problems 2 and 5(by Laguerre and Gauss quadrature) Chapter 6.1 Problems 2, 4, Chapter 6.2 Problems 1, 5, 6, 9, 11, 12 Computer problems 2, 3, 6, 8, 10