Starting from:
$35

$29

COMPUTER SCIENCE ASSIGNMENT #6 Solution




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.. Handwrit-ten answers are ok but they will need to be scanned and merged into a single pdf le 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 speci-cation.




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



(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 = R03h f(x)dx that results from approximating the integral I by I R03h P (x)dx.

Note: if you know only 3 function values of f(x) and they are at 3 unequally-spaced 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):







f(x)



0.00:5




0.24 0:50727




0.36 0:51656

Use the quadrature formula from (b) to approximate R00:36 f(x)dx.

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




the exact value is about R00:36 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

9f
5
! + 9f(0) + 9f
5
!


r












r




5


3
8
5
3






























for approximating

1



f(x)dx:




1




Use the de nition of "degree of precision" to do this, and show all of your work.




See Handout #27. Note that the interval of integration here is xed at [ 1; 1].




Use exact arithmetic here { do not use a numeric approximation for
3=5.
(b) (2 points) Use the quadrature formula in (a) to approximate
p
1








Z 1
e xp


dx:


x + 2


2



Either use your calculator or MATLAB to do the computations for evaluating the quadrature formula (use at least 7 signi cant 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.




(a) (4 points) Fill in the blanks in the following MATLAB function M- le trap



so that it implements the composite trapezoidal rule, using a sequence of m = 1; 2; 4; 8; 16; : : : trapezoids, to approximate Rab f(x)dx.




This M- le 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

xm+1
f(x)dx

x1




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- le 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 di erence between 2 successive approximations (with respect to relative error) is < tol, then the algorithm terminates.







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.




(4 points)



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




3
sin(1=x)dx using maxiter = 20 and tol = 10 6;

0:1


Z 1
p e3x
dx using maxiter = 20 and tol = 10 10:



x3 + 1



DELIVERABLES: The MATLAB calls and output for each. If you use function les to evaluate each of the functions I also want the .m les for those.









































































4

More products