CS1112 Fall 2009 Project 3 Objectives 1 Cube Roots

advertisement
CS1112 Fall 2009 Project 3
due Monday 10/5 at 11pm
You must work either on your own or with one partner. You may discuss background issues and general solution
strategies with others, but the project you submit must be the work of just you (and your partner). If you work with
a partner, you and your partner must first register as a group in CMS and then submit your work as a group.
Objectives
This assignment involves writing functions and plotting. Themes include probing the quality of a given piece
of software and identifying its limitations (Problem 1a), improving a given piece of software (Problem 1b),
building on existing software (Problems 1c,1d), and using the computer to exploit mathematical insights
(Problem 2). Read Chapters 4 and 5 in Insight as you work though this assignment.
1
Cube Roots
√
3
Assume V > 0. The problem of computing V is the problem of “building” a perfectly cubical box whose
volume is V . Suppose we have a non-cubical box B with edges x,√x, and V /x2 . Note that B has the proper
volume, but its edges are not equal. A little thought reveals that 3 V must be in between x and V /x2 . This
suggests that if we compute the average of the three edges
xnew = (x + x + V /x2 )/3 = 2x + V /x2 /3
then
the xnew-by-xnew -by-V /x2new box should be “more cubical,” i.e., xnew should be a better approximation
√
3
to V than x. Here is a function that iterates this idea and returns an approximate cube root:
function x = CubeRoot0(V,itMax)
% itMax is the number of improvement steps
% x is an approximate cube root of V
% Initial guess...
x = 1;
% Perform itMax improvements...
for k=1:itMax
x = (2*x + V/x^2)/3;
end
This function can be downloaded from the course website. If you play around with it you discover that
modest values of itMax produce very good approximate cube roots if V is near 1, but that large values of
itmax are required if this is not the case.
In this problem you are to design an efficient function CubeRoot2 that produces highly accurate cube roots
without the “big itMax” pitfalls associated with CubeRoot0. Assume for the time being that V ≥ 1. Our
plan is to compute a real number m and an integer e so that
V = m × 8e
1≤m<8
(1)
(It is always possible to do this.) We can then compute the required cube root from the formula
√
√
3
V = 3 m × 2e .
√
using CubeRoot0 (with modest itMax) to get 3 m accurately.
(a) The first step is to examine the relative error behavior of CubeRoot0 on the interval [1,8] for various
choices of itMax. Write a script CubeRootTest that produces a 10-line table which displays the maximum
relative error for itMax equal to 1, 2, 3,...,10. For a given itMax, we define the “maximum relative error” to
be the largest value of
√
3
| V − CubeRoot0(V,itMax)|
√
3
V
1
that can be obtained by letting V take on any of the values in linspace(1,8,1000). With so many
samplings, we can be confident that this computed
maximum captures the “worst case scenario” when we
√
3
use CubeRoot0(V,itmax) to approximate V for 1 ≤ V ≤ 8. Submit CubeRootTest to CMS.
(b) Notice that the given implementation√of CubeRoot0 uses x = 1 as an initial guess. A better idea is to
interpolate the cube root function f(z) = 3 z at z = 1 and z = 8
3
2.5
2
1.5
1
0.5
0
V
−0.5
−1
0
2
4
6
8
10
√
and notice that the “*” on the connecting line segment is a pretty good approximation to 3 V . Modify
CubeRoot0 so that it incorporates this idea and re-run CubeRootTest. What is the optimum choice for
itMax given that we want CubeRoot0 to return a cube root that has relative error in the vicinity of 10−15?
Submit you implementation of CubeRoot0 to CMS. It should include a comment at the end which answers
the “optimum itMax” question.
(c) We now want to extend our cube root capabilities to handle arbitrarily large V -values. Implement a
function
function x = CubeRoot1(V)
% V >= 1
% x is an approximate cube root of V with relative error roughly
% equal to 10^-15.
Your implementation should first compute the values of m and e in (1). This can be done using logarithms,
but for full credit you must use a while loop. Hint:
2560 × 80
320 × 81
40 × 82
5 × 83
2560 =
=
=
=
In this case, m = 5 and e = 3.
√
Use your implementation of CubeRoot0 with optimum itMax to compute 3 m. You can test your finished
implementation of CubeRoot1 with a suitably modified version of CubeRootTest. Submit CubeRoot1 to
CMS.
(d) We can use CubeRoot1 to evaluate the cube root of a small positive number. Suppose 0 < V < 1. Noting
that
√
3
V = 1/ 3 1/V
2
we simply use CubeRoot1 to compute 3 1/V and invert the result. We can also use CubeRoot1 to compute
the cube root of a negative number. Suppose V < 0. Noting that
√
√
3
V = − 3 −V
√
we simply use CubeRoot1 to compute 3 −V and negate. Implement a function
function x = CubeRoot2(V)
% V is any real number.
% x is an approximate cube root of V with relative error roughly
% equal to 10^-15.
that makes use of these ideas. Don’t forget to handle the case V = 0. Submit CubeRoot2 to CMS.
2
Roots of a Cubic Polynomial
If the cubic polynomial
p(x) = x3 + bx2 + cx + d
has three real distinct roots
r1 < r2 < r3 ,
then its graph looks like this:
Note that if L and R are the zeros of
p (x) = 3x2 + 2bx + c
with L < R, then
r1 < L < r2 < R < r3
In this problem you write a function ShowCubic(b,c,d) that produces an informative plot of the cubic p(x)
and displays approximations of r1 , r2 and r3 in the plot title. Organize the body of ShowCubic as follows:
Step 1. Compute L and R using the quadratic formula and produce a plot of p(x) across the interval [L, R].
Step 2. Using ginput, acquire the value of r2 by clicking on the middle root that is displayed in the plot.
In particular, the command
[r2,y] = ginput(1);
will wait for you to click on the figure window. It will then record the xy coordinates of the click. Thus,
if you click on the middle root r2 , then the above ginput command will assign to r2 an approximation
to the middle root. (The y-value is irrelvant.)
3
Step 3. By equating coefficients, develop recipes for τ and μ so that
x3 + bx2 + cx + d = (x − r2 )(x2 + τ x + μ).
Using the quadratic formula, compute r1 and r3 which are roots of x2 + τ x + μ = 0.
Step 4. Open up a second figure window using the command figure and plot p(x) across the [r1 −h, r3 +h]
where h = (r3 − r1 )/10. (We want to display p a bit beyond the “root zone.” Display the computed
approximations to the three roots in the title using sprintf. Use sensible formats. Your plot should
include the x-axis. Add other features to make it look nice! You may assume throughout that the
input parameters to ShowCubic define a cubic with three distinct real roots. Do not worry about the
complex root situation.
A test script QPlot can be downloaded from the course website. Submit your implementation of ShowCubic
to CMS.
4
Download