Homework Set No. 4: 1. Trapezoid and Simpson's Methods

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Homework Set No.

4
The problems will be graded selectively.
NB! Problems marked with (*) are more challenging, extra points will be awarded for
them. For that reason, no hints will be given for those problems before the due date.

1. Trapezoid and Simpson’s methods

Given function f (x) = e−x , we would like to study different numerical approximations
to the integral Z 0.8
f (x) dx.
0.0
We will use the values of f (x) at the points 0.0, 0.2, 0.4, 0.6, 0.8. Generate the data set
before you start the numerical integration. Use 6-digits accuracy.

a). Write out the trapezoid rule and compute the numerical integration with 6 digits.

b). Write out the Simpson’s rule and compute the numerical integration with 6 digits.

c). What is the exact value of the integral? What is the absolute error by using
trapezoid and Simpson’s rule? Which method is better?

d). The error for trapezoid rule with (n+1) points is

b − a 2 ′′ b−a
ET (f ; h) = − h f (ξ), h= ,
12 n
for some ξ ∈ (a, b). The error for Simpson’s rule with (2n+1) points is

b − a 4 (4) ′ b−a
ES (f ; h) = − h f (ξ ), h= ,
180 2n
for some ξ ′ ∈ (a, b). If we wish the absolute value of the error to be smaller than
10−4 , how many points would be needed for each method?

2. Matlab problem: Trapezoid rule

Preparation: Use the help in Matlab to learn how to use the function feval.
Write a Matlab function which computes the integral by trapezoid rule. Your function
should be used by the following command in Matlab command window:

>> v=trapezoid(’funItg’,a,b,n)

where funItg.m is the name of the file of the function f (x), and a,b is the interval, and
n is the number of sub-intervals (i.e., n+1 will be the number of points.)

1
Test your function on problem 1. Write a script that computes for n = 4, 8, 16, 32, 64, 128.
Compute also the absolute error for each n, and make a plot of the absolute error against
n. (Use loglog to plot. ) How does the error change when n is doubled? Do you expect
this from the error estimate? Please comment.

What to hand in? Print out your function in the file trapezoid.m, your script file,
the plot of error, and your comments.

3. Matlab problem: Simpson’s rule

Write a Matlab function which computes the integral by Simpson’s rule. Your function
should be used by the following command in Matlab command window:

>> v=Simpson(’funItg’,a,b,n)

where funItg.m is the name of the file of the function f (x), and a, b is the interval,
and n is the number of sub-intervals (i.e., 2n+1 will be the number of points.)
Test your function on problem 1. Write a script that computes for n = 2, 4, 8, 16, 32, 128,
the absolute error for each n, and a plot (with loglog) of the absolute error against n.
How does the error change when n doubled? Compare the results with trapezoid rule
and comment.

What to hand in? Your function in Simpson.m, your script file, the plot of error,
and your comments.

4. Romberg algorithm

Preparation: Use helpdesk in Matlab to learn how to use the functions feval and
quad.

a) Write a Matlab function that computes the Romberg integration. One should be
able to call the function by:

>> R=romberg(’f’,a,b,n)

where f is the name of the function where f (x) is implemented, and a and b defines
the integrating interval, and n is the size of your Romberg table. The function
should return the whole Romberg table. The best approximation of the interval
would be the value in R(n,n).
You may follow the pseudo-code in the lecture notes, or implement your own.

2
b) Use your romberg to compute the integrals
Z π
i) sin(x)dx (= 2)
0
Z 1√
ii) xdx (= 2/3)
0

Compute also the errors. The exact values of the integrals are given in the paren-
theses above. Print the errors along the diagonal of the table, and note how it
changes along the diagonal of the table. Use format short e in Matlab to display
the error data.

c)* (extra points) Explain why Romberg algorithm works poorly for the last integral.

d) Use Matlab functions quad and quadl to compute the integrals in b). Use 1e-9
as tolerance for both integrations. Mark your observation.

What to hand in? The Matlab file romberg.m, the file for your function f.m, a script
file that does b) and d), and the Romberg tables your get in b). Give a try to answer
the question in c).

5. Numerical integration and extrapolation.


sin t
Let f (t) be Ra continuous function for all t, and for t 6= 0 is t .
x
Let I(x) = 0 f (t)dt and J = I(1).

a) Compute approximations to J by trapezoid rule with 1,3 and 9 equal intervals.


Try to use fewest possible time of computing f (t). Show which formula you use.

b)* (extra points) Combine trapezoid approximations from a) to get another (better)
approximation to J with the same principle that lies behind Romberg integration.
Show which formula you use.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy