Académique Documents
Professionnel Documents
Culture Documents
n +
n 1
for n = 1, 2, 3, . . . .
Compare the dierence in the outputs of the two functions. The second one should give
more accurate results since it avoids subtracting two nearly equal numbers and thus
reduces cancellation errors.
4. Write a function that takes three doubles a, b and c as input and computes the roots of
the quadratic equation
ax
2
+ bx + c = 0.
When a = 0 and b = 0 the equation has only one root x = c/b. When a = 0, its roots
are given by
x
1
=
_
b +
b
2
4ac
_
2a
, x
2
=
_
b
b
2
4ac
_
2a
.
If the discriminant
b
2
4ac is negative, the two roots are complex numbers. Output
complex numbers in the form of r + mi, where i =
i=1
P
i
_
1 +
r
12
_
i
+ P
n
_
1 +
r
12
_
n
,
Please Turn Over. . .
assuming that each P
i
is no smaller than his regular monthly payment so that no
penalty is imposed on him. Write a function that computes the payo amount P
n
,
given S, r, P
1
, P
2
, . . ., and P
n1
. If the man has a 15-year mortgage of 200000 euros at
rate 0.0625, but pays P
i
= 2000 + 10i euros in month i for i = 1, 2, . . . , 59 and wants
to payo the mortgage in month 60, what is his payo amount P
60
? Hint: Horners
algorithm should be used in calculating the summation.
8. The base of the natural algorithm e is
e = 2.7182818284590452353602874713526624977572 . . .
to 41 signicant digits. It is known that
x
n
=
_
1 +
1
n
_
n
e, as n .
Write a function that generates the sequence {x
n
} and prints out the error e x
n
. A
very slow (worse than linear) convergence should be observed for x
n
e. Indeed it can
be shown that
|e x
n+1
|
|e x
n
|
1, as n .
A computationally eective way to compute e to arbitrary precision is to use the innite
series:
e =
n=0
1
n!
= 2 +
1
2!
+
1
3!
+ .
To reduce roundo errors, smaller terms should be added rst in calculating partial
sums. It can be shown that
e
m
n=0
1
n!
<
e
(m + 1)!
.
Write another function to generate the partial sums for approximating e and compare
the convergence. Horners algorithm may be used to eciently compute this kind of
partial sum.
9. Compute the integral:
_
2
1
sin x
x
dx
using composite trapezium rule and Simpson rule with ve digit accuracy after the
decimal point.
End of Assignment