##### Document Text Contents

Page 90

82 LECTURE 22. INTEGRATION: MIDPOINT AND SIMPSON’S RULES

In Matlab there is a built-in command for definite integrals: quad(f,a,b) where the f is an inline

function and a and b are the endpoints. Here quad stands for quadrature, which is a term for

numerical integration. The command uses “adaptive Simpson quadrature”, a form of Simpson’s

rule that checks its own accuracy and adjusts the grid size where needed. Here is an example of its

usage:

> f = inline(’x.^(1/3).*sin(x.^3)’)

> I = quad(f,0,1)

Exercises

22.1 Using formulas (22.1) and (22.2), for the integral

∫ 2

1

√

x dx calculate M4 and S4 (by hand, but

use a calculator). Find the percentage error of these approximations, using the exact value.

Compare with exercise 21.1.

22.2 Write a function program mymidpoint that calculates the midpoint rule approximation for

∫

f on the interval [a, b] with n subintervals. The inputs should be f , a, b and n. Use your

program on the integral

∫ 2

1

√

x dx to obtain M4 and M100. Compare these with the previous

exercise and the true value of the integral.

22.3 Write a function program mysimpson that calculates the Simpson’s rule approximation for

∫

f on the interval [a, b] with n subintervals. It should call the program mysimpweights to

produce the coefficients. Use your program on the integral

∫ 2

1

√

x dx to obtain S4 and S100.

Compare these with the previous exercise and the true value.

82 LECTURE 22. INTEGRATION: MIDPOINT AND SIMPSON’S RULES

In Matlab there is a built-in command for definite integrals: quad(f,a,b) where the f is an inline

function and a and b are the endpoints. Here quad stands for quadrature, which is a term for

numerical integration. The command uses “adaptive Simpson quadrature”, a form of Simpson’s

rule that checks its own accuracy and adjusts the grid size where needed. Here is an example of its

usage:

> f = inline(’x.^(1/3).*sin(x.^3)’)

> I = quad(f,0,1)

Exercises

22.1 Using formulas (22.1) and (22.2), for the integral

∫ 2

1

√

x dx calculate M4 and S4 (by hand, but

use a calculator). Find the percentage error of these approximations, using the exact value.

Compare with exercise 21.1.

22.2 Write a function program mymidpoint that calculates the midpoint rule approximation for

∫

f on the interval [a, b] with n subintervals. The inputs should be f , a, b and n. Use your

program on the integral

∫ 2

1

√

x dx to obtain M4 and M100. Compare these with the previous

exercise and the true value of the integral.

22.3 Write a function program mysimpson that calculates the Simpson’s rule approximation for

∫

f on the interval [a, b] with n subintervals. It should call the program mysimpweights to

produce the coefficients. Use your program on the integral

∫ 2

1

√

x dx to obtain S4 and S100.

Compare these with the previous exercise and the true value.