MATH/CMPSC 455, HOMEWORK 1 (1)

MATH/CMPSC 455, HOMEWORK 1
(1) Implement the nested multiplication algorithm that can be used to compute
any polynomial P (x) = a0 + a1 x + a2 x2 + · · · + an xn . Please also implement
the Method 1 and Method 2 mentioned in the class. Use them to evaluate
1
P (x) = −1 + 5x − 3x2 + 3x3 + 2x4 at x = . Report and compare the CPU time
2
of these three methods. (Requirement: the inputs of your subroutines should be
the coefficient array a = [a0 , a1 , · · · , an ], the degree of the polynomial n and the
variable x)
Solution: Run the script compare.m in Matlab, and get the following results on
my laptop (Macbook, OS 10.6.8, 2.4GHz Intel Core 2 Duo, 8GB RAM, Matlab
R2011a).
• Method 1 takes 1.14 seconds.
• Method 2 takes 0.73 seconds.
• Nested Multiplication takes 0.24 seconds.
The numerical results may differ due to the implementation and computational environment. But in general, Nested Multiplication should be the fastest
one. Method 1 should be the slowest one.
(2) Use the nested multiplication subroutine to evaluate P (x) = 1 − x + x2 − x3 +
· · · + x98 − x99 at x = 1.00001. Find a simpler, equivalent expression, and use it
to estimate the error of the nested multiplication.
Solution: In the Command window of Matlab, type
• First generate the coefficient a = [1, −1, 1, −1, · · · , 1, −1]:
>> a = ones(100,1);
>> a(2:2:100)=-1;
• Define x = 1.00001:
>> x = 1.00001;
• Define the order of the polynomial n = 100
>> n = 100;
• In order to see more accurate result, using high precision
>> format long e;
• Now call the nested multiplication subroutine
>> value = nested(x, n, a);
1
2
• The result is
>> -5.002450796476321e-04
Now consider the following equivalent formula
P (x) = 1 − x + x2 − x3 + · · · + x98 − x99
(1 + x)(1 − x + x2 − x3 + · · · + x98 − x99 )
(1 + x)
100
1−x
=
.
1+x
=
Use this formula to compute P (x) in Matlab, we can get
>> -5.002450796474608e-04
Compare with the result of Nested Multiplication, the error is about
1.713039432527097e-16
The reason that Nested Multiplication have some error is that x = 1.0001 is
close to 1, so the computation may suffer due to the loss of significance.
(3) Calculate the expression that follow in double precision arithmetic (using MATLAB) for x = 10−1 , . . . , 10−14 . Then using an alternative form the expression
that doesn’t suffer from subtracting nearly equal numbers, repeat the calculation and make a table of results. Report the number of correct digits in the
original expression for each x.
1 − sec x
(a)
tan2 x
1 − (1 − x)3
(b)
x
Solution:
We can easily get the following equivalent form:
1 − sec x
1
=
−
tan2 x
sec x + 1
1 − (1 − x)3
= 3 − 3x − x2
x
From the two tables, it is easy to see the modified formula works better. And
the original formula may suffer due to the loss of significance.
(4) Compute f 0 (x) at a point x by its definition
f (x + h) − f (x)
.
h→0
h
f 0 (x) = lim
3
Table 0.1. Results of Question 3 (1): exact value should be −0.5
x
10−1
10−2
10−3
10−4
10−5
10−6
10−7
10−8
10−9
10−10
10−11
10−12
10−13
10−14
1 − sec x
tan2 x
-4.987479137114346e-01
-4.999874997909555e-01
-4.999998750142894e-01
-4.999999936279312e-01
-5.000000413368523e-01
-5.000444502908372e-01
-5.107025913275687e-01
0
0
0
0
0
0
0
1
sec x + 1
-4.987479137114288e-01
-4.999874997916638e-01
-4.999998749999792e-01
-4.999999987500000e-01
-4.999999999875000e-01
-4.999999999998750e-01
-4.999999999999987e-01
-5.000000000000000e-01
-5.000000000000000e-01
-5.000000000000000e-01
-5.000000000000000e-01
-5.000000000000000e-01
-5.000000000000000e-01
-5.000000000000000e-01
−
Table 0.2. Results of Question 3 (2): exact value shoule be 3
x
10−1
10−2
10−3
10−4
10−5
10−6
10−7
10−8
10−9
10−10
10−11
10−12
10−13
10−14
1 − (1 − x)3
x
2.709999999999999e+00
2.970099999999998e+00
2.997000999999999e+00
2.999700010000161e+00
2.999970000083785e+00
2.999997000041610e+00
2.999999698660716e+00
2.999999981767587e+00
2.999999915154206e+00
3.000000248221113e+00
3.000000248221113e+00
2.999933634839635e+00
3.000932835561798e+00
2.997602166487923e+00
3 − 3x − x2
2.690000000000000e+00
2.969900000000000e+00
2.996999000000000e+00
2.999699990000000e+00
2.999969999900000e+00
2.999996999999000e+00
2.999999699999990e+00
2.999999970000000e+00
2.999999997000000e+00
2.999999999700000e+00
2.999999999970000e+00
2.999999999997000e+00
2.999999999999700e+00
2.999999999999970e+00
4
Then use MATLAB to implement by imitating the limit operation by using a
sequence of numbers h such as h = 4−1 , 4−2 , 4−3 , . . . , 4−n , . . . , namely,
f (x + h) − f (x)
, with h = 4−1 , 4−2 , 4−3 , . . . , 4−n , . . . .
h
Try to approximate f 0 (x) of f (x) = 1/x, f (x) = log x, f (x) = ex , f (x) = tan x,
f (x) = cosh x, and f (x) = x3 − 23x at any point you like. Make a table for the
numerical results and explain the results you get.
f 0 (x) ≈
f (x + h) − f (x)
h
at some special x (such as x = 2). For very small h, since x + h ≈ x,
and f (x + h) ≈ f (x), the results will be 0. But in general, the formula
f (x + h) − f (x)
works for approximating f 0 (x). I did not include the table
h
since the numerical results for this problem are quite straightforward.
Solution: For this question, just plug in h = 4−1 , 4−2 , 4−3 , . . . , 4−n to