You are on page 1of 2

PH 509 Computational Physics

2021-22 Semester-II, Lab - 01

Assignments are due on Wednesday (12 January 2022) by 09:00 am

1. A classic numerical problem is the summation of a series to evaluate a function. For


example, consider the infinite series for sin(𝑥):
3 5 7
𝑥 𝑥 𝑥
sin(𝑥) = 𝑥 − 3!
+ 5!
− 7!
+···
Reading: Landau, Paez and Bordeianu, Section 2.5
a. Write a program that implements the pseudocode given in Landau’s book for the
indicated values of 𝑥. Present the results as a table with headings
𝑥 𝑖𝑚𝑎𝑥 𝑠𝑢𝑚 |𝑠𝑢𝑚 − sin(𝑥)|/ sin(𝑥)
where sin(𝑥) is obtained from the built-in function. Modify the code that sums the series in a
“good way” (no factorials) to one that calculates the sum using explicit factorials. Start with a
−8
tolerance of ϵ = 10 .

b. Check that for a sufficiently small value of 𝑥, your algorithm converges to the correct
answer.

c. Compare the number of decimal places of precision obtained with that expected
𝑡ℎ
𝑛 𝑡𝑒𝑟𝑚
from the convergence criterion: | 𝑠𝑢𝑚
| < ϵ.

d. Explore that without using the identity sin(𝑥 + 2𝑛π) = sin(𝑥), there is a range of
somewhat large values of 𝑥 for which the algorithm converges to the wrong answer!

e. Further, explore that if you keep increasing the input argument 𝑥, the algorithm does
not even converge!

f. For the same values of input 𝑥 in the last two parts, now use the identity
sin(𝑥 + 2𝑛π) = sin(𝑥) to recalculate the values sin(𝑥).

g. Set your tolerance to a number smaller than the machine precision and see how it
affects your conclusions

2. In class, we wrote an example code to calculate the machine precision (up to a


multiplicative factor 2) defined as the maximum positive number ϵ𝑚that (on a
computer) can be added to the number stored as 1 (denoted by 1𝐶) without changing
it: 1𝐶 + ϵ𝑚 = 1𝐶.
a. Generalize the program to find such positive numbers ϵ𝑥 for any arbitrary real
number 𝑥 defined similarly: ϵ𝑥: 𝑥𝐶 + ϵ𝑥 = 𝑥𝐶. Tabulate these values of ϵ𝑥 for
different values of 𝑥 using the following columns: [𝑥, ϵ𝑥, ϵ𝑥/𝑥]. Comment on
any pattern that you see for ϵ𝐶 vis-a-vis the magnitude of the number |𝑥|.

b. Comment on the ratio ϵ𝑥/𝑥 for each of these numbers 𝑥. Does this give you
a clue as to why we expressed the ‘approximate’ computer representation of
a number 𝑎 ≃ 𝑎 ( 1 + ϵ𝑎). (This was asked by a student in class). Write
down your observations and comments.

3. Reading: Landau, Paez and Bordeianu 3.1.1 (Numerical errors from Subtractive
cancellation)
2
Consider the quadratic equation 𝑎 𝑥 + 𝑏𝑥 + 𝑐 = 0 which has an analytic solution
that can be written in one of the following two ways:
2
−𝑏 ± 𝑏 −4𝑎𝑐
𝑥1, 2 = 2𝑎
or,
' −2𝑐
𝑥 = 2
1,2 𝑏 ± 𝑏 −4𝑎𝑐
Upon inspection, we find that subtractive cancellation leads to an increase in the
2
error of one of the roots when 𝑏 ≫ 4𝑎𝑐.
a. Write a program to calculate both pairs of solutions for arbitrary values of 𝑎, 𝑏,
and 𝑐.

b. Investigate how errors become large as the subtractive cancellation increases


and relate this to the known machine precision. (A good test case is
−𝑛
𝑎 = 1, 𝑏 = 1, 𝑐 = 10 , 𝑛 = 1, 2, 3, ···).

4. Calculating the sum of a simple series may require some thought and care. Consider
the two series:
𝑁
(𝑢𝑝)
𝑆 = ∑ (1/𝑛) and,
𝑛=1
1
(𝑑𝑜𝑤𝑛)
𝑆 = ∑ (1/𝑛)
𝑛=𝑁
Both series are finite (as 𝑁 is finite), and when summed analytically both give the
(𝑢𝑝)
same answers. Nonetheless, the numerical value of 𝑆 will not be precisely that of
(𝑑𝑜𝑤𝑛)
𝑆 .
(𝑢𝑝) (𝑑𝑜𝑤𝑛)
a. Write a program to calculate 𝑆 and 𝑆 as a function of 𝑁.

(𝑢𝑝) (𝑑𝑜𝑤𝑛) (𝑢𝑝) (𝑑𝑜𝑤𝑛)


b. Make a log-log plot of (𝑆 −𝑆 )/(|𝑆 | + |𝑆 |)as a function of
𝑁.

c. Observe the linear regime on your graph and explain why the downward sum
is generally more precise?

You might also like