Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapters 5 and 6: Numerical Integration

Similar presentations


Presentation on theme: "Chapters 5 and 6: Numerical Integration"— Presentation transcript:

1 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

2 Derive the trapezoid rule

3 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

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

5 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

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

7 We will approximate by the trapezoid rule in several forms.

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

9 MatLab script to apply trapezoid rule function
to estimate with 10 equally spaced points. 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])

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

11 Extended MatLab script to apply trapezoid rule to
with logarithmically spaced points 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])

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

13 What is the essential difference between equal and logarithmic spacing?

14 Changing variable of integration

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

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

17 Extended MatLab script to apply trapezoid rule to
with a new integration 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])

18 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) , (2) , (3)

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

20 Assignment 5, Due 1/31/19 On page 242 of the text (6th 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. 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

21 Graph of integrand in Assignment 5
Integrand(x) X

22 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

23 Write MatLab code for composite trapezoid rule

24 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

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

26 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

27 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

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

29 Apply theory to

30 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

31 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

32 (b-a)=3, |f “(x)|max =1, n=9 |e| < (b-a)3|f “(x)|max/12n2 ~ ? 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)

33 Exact value = Trap-rule 10 pts = Actual absolute error = ~ 1% Trap-rule value Upper bound on absolute error = ~ 4% Trap-rule value

34 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 What is the actual absolute error in this trapezoid rule approximation?

35 Deriving numerical integration methods

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

37 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

38 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)}.

39 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)}

40

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

42 Example: derive Simpson’s rule

43 Simpson’s rule for one pair of subintervals

44 Simpler way to derive Simpson’s rule

45 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

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

47 n pairs of subintervals

48 MatLab code for composite Simpson’s rule:
Layout of points when npairs =4 i= |-----|------| | |------| |------| | n=2npairs=8 a a+h b Number of subintervals is even Number of points is odd

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

50 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, , in each case.

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

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

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

54 Proof of Gauss quadrature theorem

55 Exact for any set of n+1 nodes on [a,b]
Exact for polynomial degree 2n+1

56 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

57 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

58 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

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

60 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

61 Points and weights of Gauss quadrature are determined for
Every integral from a to b can transformed into integral from -1 to 1

62 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

63 Example: Review: Gauss quadrature with n+1 points
Points and weights from 2, 3, 4, and 5 points:table 6.1, text p236 Example:

64 (no anti-derivative) with 2 nodes

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

66 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

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

68

69

70 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

71

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

73 Change the variable of integration in
by the transformation y = ln(1/x) = -ln(x)

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

75 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

76 Note: Only applies to 1 type of integral Less symmetry than with Gauss quadrature Use first and last columns

77 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

78

79 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

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

81 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


Download ppt "Chapters 5 and 6: Numerical Integration"

Similar presentations


Ads by Google