COMPUTER SCIENCE 349A

ASSIGNMENT #6 (Total: 20 points)

This is a really large class and the logistics of grading assignments are challenging. Me

and the markers require your help in making this process go smoothly. Please ensure that

your assignments conform to the following requirements – any violation will result in getting

a zero for the particular assignment.

• READ CAREFULLY ALL THE TEXT FOR EACH QUESTION

• All assignments should be submitted electronically through the ConneX course website

and shoud be SINGLE PDF FILES. No other formats will be accepted.. Handwritten answers are ok but they will need to be scanned and merged into a single pdf file

together with any code examples and associated plots.

• The assignment number, student name and student number should be clearly visible

on the top of every page of your assignment submission.

• PLEASE DO NOT COPY THE ASSIGNMENT DESCRIPTION IN YOUR

SUBMISSION

• The asnwers to the questions should be in the same order as in the assignment specification.

• Some of the questions of the assignments are recycled from previous years but typically

with small changes in either the description or the numbers. Any submission that

contains numbers from previous years in any questions will be immediately graded

with zero.

• Any assignment related email questions should have a subject line of the form CSC349A

Assignment X, where X is the number of the corresponding assignment.

1

1. (a) (2 points) Determine the Lagrange form of the interpolating polynomial P(x)

that interpolates a function f(x) at x = 0, 2h and 3h, where h > 0. (Multiply the

linear factors together, but leave P(x) as a sum of 3 quadratics in the variable x.)

DELIVERABLES: All your work in constructing the polynomial. This is to be

done by hand not MATLAB.

(b) (2 points) Derive the quadrature formula of the form

a0f(0) + a1f(2h) + a2f(3h)

for approximating I =

R 3h

0

f(x)dx that results from approximating the integral I

by I ≈

R 3h

0

P(x)dx.

Note: if you know only 3 function values of f(x) and they are at 3 unequallyspaced points 0, 2h and 3h, then this kind of quadrature formula can be used to

approximate I.

DELIVERABLES: All your work in deriving the quadrature formula.

(c) (2 points) Suppose that you know only the following function values of f(x):

x f(x)

0.0 0.5

0.24 0.50727

0.36 0.51656

Use the quadrature formula from (b) to approximate R 0.36

0

f(x)dx.

Note: the above data corresponds to the function f(x) = 1/(1 + cos(x)), and

the exact value is about R 0.36

0

f(x)dx = 0.1819695…. Use this information only

to assess the accuracy of your computed approximation. If you do not obtain a

fairly good approximation in (c), then your answer in (b) is incorrect. That is,

the relative error between your answer and the true answer should be way less

than 1%.

DELIVERABLES: All your work to calculate the quadrature.

2. (a) (4 points) Determine the degree of precision of the quadrature formula

5

9

f

−

r

3

5

!

+

8

9

f(0) + 5

9

f

r

3

5

!

for approximating

Z 1

−1

f(x)dx.

Use the definition of ”degree of precision” to do this, and show all of your work.

See Handout #27. Note that the interval of integration here is fixed at [−1, 1].

Use exact arithmetic here – do not use a numeric approximation for p

3/5.

(b) (2 points) Use the quadrature formula in (a) to approximate

Z 1

−1

e

−x

√

x + 2dx.

2

Either use your calculator or MATLAB to do the computations for evaluating the

quadrature formula (use at least 7 significant digits).

Note: the exact value of this integral is 3.01747… Your computed approximation

based on a sampling of f(x) at only three points is remarkably accurate (as this

quadrature formula has a fairly high degree of precision).

DELIVERABLES: Show all your work.

3. (a) (4 points) Fill in the blanks in the following MATLAB function M-file trap

so that it implements the composite trapezoidal rule, using a sequence of m =

1, 2, 4, 8, 16, . . . trapezoids, to approximate R b

a

f(x)dx.

This M-file uses the MATLAB built-in function trapz. If

x = (x1, x2, . . . , xm+1) and y = (f(x1), f(x2), . . . , f(xm+1)),

then the execution of

z = trapz(x, y)

approximates

Z xm+1

x1

f(x)dx

using the composite trapezoidal rule (with m trapezoids). (Note that the entries

of x are labeled starting at x1 because MATLAB does not allow an index of 0.)

The function M-file trap has 4 input parameters:

• a and b (the lower and upper limits of the integral);

• maxiter (the maximum number of iterations that are allowed; the above

program will implement the composite trapezoidal rule with a sequence of

1, 2, 4, 8, 16, . . . trapezoids, up to a maximum of 2maxiter trapezoids);

• tol (a tolerance for testing the relative error of the computed approximation).

If the difference between 2 successive approximations (with respect to relative

error) is < tol, then the algorithm terminates.

You also must define and store a MATLAB function M-file

y = f(x)

which must be written to accept a vector x as the argument. Note that this means

that you must use arithmetic operators like .* , .^ , ./ and so on, rather than

just *, ^, /.

function trap(a, b, maxiter, tol)

m = 1;

x = linspace(a, b, m+1);

y = f(x);

approx = trapz(x, y);

disp(’ m integral approximation’);

fprintf(’ %5.0f %16.10f \n ’, m, approx);

3

for i = 1 : maxiter

m = ________________________________ ;

oldapprox = __________________________________ ;

x = linspace ( _______ , ________ , __________ ) ;

y = f(x);

approx = trapz(x, y);

fprintf(’ %5.0f %16.10f \n ’, m, approx);

if abs( _______________________________ ) < tol

return

end

end

fprintf(’Did not converge in %g iterations’, maxiter)

DELIVERABLES: A copy of your function trap. Feel free to add an extra

parameter f at the end of parameter list to make your function calls simpler.

(b) (4 points)

Use the above MATLAB script to approximate the following two integrals:

Z 3

0.1

sin(1/x)dx using maxiter = 20 and tol = 10−6

,

Z 1

0

e

3x

√

x

3 + 1

dx using maxiter = 20 and tol = 10−10

.

DELIVERABLES: The MATLAB calls and output for each. If you use function

files to evaluate each of the functions I also want the .m files for those.

4

## Reviews

There are no reviews yet.