$29
Question #1 - 10 marks.
Consider the function
f(x) = 1 + cos x
(x − π)
2
where x 6= π is in radians.
(a) Using b = 10, k = 4, idealized, floating-point arithmetic with rounding, compute
fl(f(x)) at x = 3.154. Note that fl(π) = 3.142.
Furthermore, The correct value of f(3.154) to 8 correct significant digits is 0.49999359.
Your floating-point approximation should have a relative error greater than 30%.
(b) Determine the fourth order Taylor polynomial approximation for cos (x) expanded
about x = π (Do not include the remainder term.) Leave this polynomial in terms of
expressions involving (x − π)
k
, for integer values of k.
(c) Substitute the polynomial approximation from (b) into the formula for f(x), and simplify in order to obtain a polynomial approximation for f(x). Note: do not multiply
out the remaining factor (x − π)
2
; leave it in this form.
(d) Show that the problem of computing f(3.145) is well-conditioned. Use the definition
of condition and the notation in Handout 6 to show this.
Hint: Use the polynomial approximation for f(x) from (c) to show that the exact value
of f(3.154 + ε) is approximately equal to 0.49999 (that is, approximately equal to the
exact value of f(3.154)) whenever
ε
3.154
is small.
(e) Show that the computation of fl(f(3.154)) in (a) is unstable. Use the definition of
stability and the notation in Handout 7 to show this.
Hint: Use the result in (c) to show that when
ε
3.154
is small, the exact value of
f(3.154 + ε), done in (d), cannot be close to the floating-point calculation fl(f(3.154))
in (a), for any ε.
Question #2 - 10 Marks
(a) Write a MATLAB function M-file with header
function root = Bisect ( xl , xu , eps , imax, f, enablePlot )
corresponding to the pseudocode given in Handout #8 for the Bisection method (in xl it is
an “ell” not a “one”). Note, in the pseudocode ”exit” is used to break out of the function. In
Matlab, this will cause the program to close. You want to use ”return” in implementation.
The only differences from that given algorithm are the following:
• print a caption for your computed approximations by inserting the following statement
just before the while statement:
fprintf ( ’ iteration approximation \n’)
2
• print each successive computed approximation by inserting the following statement
after the computation of xr at the beginning of the while loop:
fprintf ( ’ %6.0f %18.8f \n’, i, xr )
• print a message to indicate that the algorithm has failed to converge in imax steps by
replacing the last statement in the pseudocode by the following:
fprintf ( ’ failed to converge in %g iterations\n’, imax )
• The extra argument enablePlot is used to select an optional plot of showing each
iteration of the bisection method when enablePlot is set to 1. When enablePlot is set
to 0 no figure will be generated. The command hold on can be used to in the same
figure using the plot command. For example try the following to understand how this
works:
hold on;
x = [0:0.1:1];
plot(x, exp(x));
plot(x, log(x));
For each iteration you should plot the function between xl and xu as well as stars
indicating on the graph the values of f(xl), f(xu) and f(xr). The following example
MATLAB code (continuation of the previous example) can help you understand the
syntax to accomplish this.
z = [0.2, 0.4, 0.8];
fz = exp(z);
plot(x, exp(x), z, fz, ’*g’);
hold off;
DO NOT INCLUDE ALL THE ENDPOINTS IN YOUR ANSWER. DO
INCLUDE THE ENDPOINTS FOR ITERATIONS 1, 2, 4, 6.
DELIVARABLES: A copy of your MATLAB M-file.
(b) Water is flowing in a trapezoidal channel at a rate of Q = 20m3/s. The critical depth
y for such a channel must satisfy the equation:
0 = 1 −
Q2
gA3
c
B
where g = 9.81m/s2
, Ac = the cross-sectional area (m2
), and B = the width of the channel
at the surface (m). For this case, the width and the cross-sectional area can be related to
depth y by
B = 3 + y and Ac = 3y +
y
2
2
3
Express this problem as a root finding problem for an appropriately defined function of the
critical depth y.
DELIVARABLES: Show all your work in deriving your formula.
(c) Use the function M-file Bisect to solve the above problem (b) with initial guesses of
xl = 0.5 and xu = 2.5, and itereate until the approximate error falls below 1% or the number
of iterations exceeds 10. You will need to write an additional MATLAB function M-file. If
function y = your_function(x)
corresponds to the function of which you are computing a zero call this with
Bisect ( xl , xu , eps , imax, @your_function, enable_plot)
with the appropriate parameter values.
DELIVARABLES:
• the additional function M-file.
• a copy of the MATLAB statement(s) you used to call Bisect.
• your output from Bisect including the figure with the endpoints for iterations (1,2,4,6).
4