Download Solutions - Personal.psu.edu

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
MATH 451, HW2 – Take home midterm
1
Homework 2 – Take home midterm
1. 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 when
using nested multiplication. Compare your results and explain your findings.
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);
• 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.
2. Calculate the quantities that follow in double precision arithmetic (using MATLAB) for x =
10−1 , . . . , 10−14 . Then, derive an alternative form the expression that doesn’t suffer from
subtracting nearly equal numbers, repeat the calculation and make a table of your results.
Report also the number of correct digits in the original expression for each x.
(a)
(b)
1−sec x
tan2 x
1−(1−x)3
x
Solution:
We can easily get the following equivalent form:
1 − sec x
1
=−
2
tan x
sec x + 1
3
1 − (1 − x)
= 3 − 3x − x2
x
Table 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
− sec 1x+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
3. Approximate f 0 (x) at a point x by its definition
f (x + h) − f (x)
.
h→0
h
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 0 (x) = lim
f 0 (x) ≈
f (x + h) − f (x)
, with h = 4−1 , 4−2 , 4−3 , . . . , 4−n , . . . .
h
2
Table 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
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.
Compare this with approximating f 0 (x) as follows:
f 0 (x) ≈
f (x + h) − f (x − h)
, with h = 4−1 , 4−2 , 4−3 , . . . , 4−n , . . . .
2h
Which approach is more accurate? Explain why.
(x)
Solution: For this question, just plug in h = 4−1 , 4−2 , 4−3 , . . . , 4−n to f (x+h)−f
at some
h
special x (such as x = 2). For very small h, since x + h ≈ x, and f (x + h) ≈ f (x), the
(x)
works for approximating f 0 (x) with
results will be 0. But in general, the formula f (x+h)−f
h
discrete error that is order h. I did not include the table since the numerical results for this
problem are quite straightforward. The discretization error for the second approximation is
order h2 so that as h → 0 the second approximation will be more accurate.
0
4. Consider a function f (x) such that f (0) = 0, f (1) = 1 and f (1) = 1.
(a) Compute the linear Lagrange form of the interpolating polynomial, p, such that p(0) =
f (0) and p(1) = f (1).
(b) Use divided differences to construct the Hermite interpolating polynomial, p, such that
0
0
p(0) = f (0), p(1) = f (1) and p (1) = f (1).
Solution: For both (a) and (b) P1 (x) = x.
3
5.
(a) Consider using linear interpolation to approximate f (x) = e−x on [0,1] with accuracy
10−8 using a table of function values at equally spaced points. Find the largest spacing
for which it is possible.
(b) If we instead use cubic interpolation, find the corresponding largest spacing. What is
an approximate factor by which the spacing can be reduced by this change from linear
to cubic interpolation? (Hint: The max of (x − x0 )(x − x1 )(x − x2 )(x − x3 ) on [0,1]
4
.)
with equal spacing is 9h
16
Solution:
i) For linear interpolation, we have
f (x) −
(x1 − x)f (x0 ) + (x − x0 )f (x1 )
(x − x0 )(x − x1 ) 00
=
f (ξ),
x1 − x0
2
where ξ ∈ H(x, x0 , x1 ). Thus, for x0 ≤ x ≤ x1 , the error is estimated as
|E(x)| ≤
(x − x0 )(x1 − x)
,
2
since we interpolate at x using only the two nearest nodes.
Setting h = x1 − x0 , we have
max (x − x0 )(x1 − x) =
x0 ≤x≤x1
h2
,
4
as the maximum of the error at the midpoint of the subinterval.
Finally, we obtain
√
h2
≤ 10−8 ⇒ h = 2 2 · 10−4 .
8
ii) For cubic interpolation, we have
f (x) − p3 (x) =
(x − x0 )(x − x1 )(x − x2 )(x − x3 ) (4)
f (ξ),
4!
where x0 < x1 < x2 < x3 . Thus,
|E(x)| ≤
9h43
16 · 4!
and we have
h3 = 2 · (8/3)1/4 10−2 .
Finally,
h3 /h ≈ 10−2 /10−4 = 100.
4
6. Compute the quadratic polynomial approximation of f (x) = ax3 using the nodes, x0 , x1 , x2 ,
that are roots of the Chebychev polynomial T3 (x) = 4x3 − 3x. Give a bound on the error of
this approximation to f (x) on the interval [−1, 1].
√
√
Solution: The nodes are given by x1 = − 3/2, x2 = 0, and x3 = 3/2. This uniquely
determines the polynomial. You can use your favorite method for its construction. Also,
|E(x)| ≤ 6a/(6 ∗ 22 ) = a/4.
7. Let H denote the n × n Hilbert matrix, whose (i, j) entry is 1/(i + j − 1). Write a program
to solve Hx = b by Gaussian elimination for n = 3, n = 7 and n = 13. Take b to be as
follows:
bi = Hi1 + Hi2 + · · · + Hin .
Compare your result to the exact solution, which is a vector of all ones.
p
8. Let A be the n × n matrix with entries Ai,j = (i − j)2 + n/10. Define x = [1, . . . , 1]T
and b = Ax. For n = 100, 200, 300, 400, and 500, use the Matlab’s backslash command
to compute xc , the double-precision computed solutions. Calculate the infinity norm of the
error for each solution. Find the relative errors for the problem Ax = b, and compare them
with the corresponding condition numbers.


1
0
0 1
 −1
1
0 1 
. (b) Let A be the n × n
9. (a) Find the P A = LU factorization of A = 
 −1 −1
1 1 
−1 −1 −1 1
matrix of the same form as in (a). Describe the entries of each matrix of its P A = LU
factorization.
10. Carry out (a) Jacobi Method, (b) Guass-Seidel Method to solve the sparse system within five
correct decimal places (relative error in the infinitys norm) for n = 2, 4, 8, 16, 32, 64. Plot
the the number of iterations as a function of the size of the problem (n). The correct solution
1
is
is [1, ..., 1]. The system, with h = n+1

2 −1
 −1 2 −1
1 

.. .. ..

.
.
.
2
h 

−1 2 −1
−1 2
5

x1


1
h2


  0 


 
  ..   .. 
 .  =  . .


 

  0 
1
xn
h2
Related documents