Problem 21
Make a table for approximations of \(\cos x\). The function \(\cos (x)\) can be approximated by the sum $$ C(x ; n)=\sum_{j=0}^{n} c_{j} $$ where $$ c_{j}=-c_{j-1} \frac{x^{2}}{2 j(2 j-1)}, \quad j=1,2, \ldots, n $$ and \(c_{0}=1\). Make a Python function for computing \(C(x ; n)\). (Hint: Represent \(c_{j}\) by a variable term, make updates term = -term*... inside a for loop, and accumulate the term variable in a variable for the sum.) Also make a function for writing out a table of the errors in the approximation \(C(x ; n)\) of \(\cos (x)\) for some \(x\) and \(n\) values given as arguments to the function. Let the \(x\) values run downward in the rowsand the \(n\) values to the right in the columns. For example, a table for \(x=4 \pi, 6 \pi, 8 \pi, 10 \pi\) and \(n=5,25,50,100,200\) can look like \(\begin{array}{rccccc}\mathrm{x} & 5 & 25 & 50 & 100 & 200 \\ 12.5664 & 1.61 \mathrm{e}+04 & 1.87 \mathrm{e}-11 & 1.74 \mathrm{e}-12 & 1.74 \mathrm{e}-12 & 1.74 \mathrm{e}-12 \\ 18.8496 & 1.22 \mathrm{e}+06 & 2.28 \mathrm{e}-02 & 7.12 \mathrm{e}-11 & 7.12 \mathrm{e}-11 & 7.12 \mathrm{e}-11 \\ 25.1327 & 2.41 \mathrm{e}+07 & 6.58 \mathrm{e}+04 & -4.87 \mathrm{e}-07 & -4.87 \mathrm{e}-07 & -4.87 \mathrm{e}-07 \\ 31.4159 & 2.36 \mathrm{e}+08 & 6.52 \mathrm{e}+09 & 1.65 \mathrm{e}-04 & 1.65 \mathrm{e}-04 & 1.65 \mathrm{e}-04\end{array}\) Observe how the error increases with \(x\) and decreases with \(n .\) Name of program file: cossum.py.
Problem 22
Write a sort function for a list of 4 -tuples. Below is a list of the nearest stars and some of their properties. The list elements are 4 -tuples containing the name of the star, the distance from the sun in light years, the apparent brightness, and the luminosity. The apparent brightness is how bright the stars look in our sky compared to the brightness of Sirius A. The luminosity, or the true brightness, is how bright the stars would look if all were at the same distance compared to the Sun. The list data are found in the file stars. list, which looks as follows: The purpose of this exercise is to sort this list with respect to distance, apparent brightness, and luminosity. To sort a list data, one can call sorted(data), which returns the sorted list (cf. Table 2.1). However, in the present case each element is a 4 -tuple, and the default sorting of such 4 -tuples result in a list with the stars appearing in alphabethic order. We need to sort with respect to the 2nd, 3rd, or 4th element of each 4 -tuple. If a tailored sort mechanism is necessary, we can provide our own sort function as a second argument to sorted, as in sorted(data, mysort). Such a tailored sort function mysort must take two arguments, say a and b, and returns \(-1\) if a should become before \(\mathrm{b}\) in the sorted sequence, 1 if \(\mathrm{b}\) should become before a, and 0 if they are equal. In the present case, a and \(b\) are 4-tuples, so we need to make the comparison between the right elements in a and b. For example, to sort with respect to luminosity we write def mysort \((a, b):\) if \(a[3]b[3]:\) return 1 else: return 0 Write the complete program which initializes the data and writes out three sorted tables: star name versus distance, star name versus apparent brightness, and star name versus luminosity. Name of program file: sorted_stars_data.py.
Problem 23
Find prime numbers. The Sieve of Eratosthenes is an algorithm for finding all prime numbers less than or equal to a number \(N\). Read about this algorithm on Wikipedia and implement it in a Python program. Name of program file: find_primes.py.
Problem 25
Resolve a problem with a function. Consider the following interactive session: Why do we not get any output when calling \(f(5)\) and \(f(10) ?\) (Hint: Save the \(\mathrm{f}\) value in a variable \(\mathrm{r}\) and write print \(\mathrm{r}\).)
Problem 27
Determine the types of some objects. Consider the following calls to the makelist function from page \(96:\) \(11=\) makelist \((0,100,1)\) \(12=\) makelist \((0,100,1.0)\) \(13=\) makelist \((-1,1,0.1)\) \(14=\) makelist \((10,20,20)\) \(15=\) makelist \(([1,2],[3,4],[5])\) \(16=\) makelist \(((1,-1,1)\), ('myfile. dat', 'yourfile. dat')) \(17=\) makelist('myfile.dat', 'yourfile.dat', 'herfile.dat') Determine in each case what type of objects that become elements in the returned list and what the contents of value is after one pass in the loop. Hint: Simulate the program by hand and check out in an interactive session what type of objects that result from the arithmetics. It is only necessary to simulate one pass of the loop to answer the questions. Some of the calls will lead to infinite loops if you really execute the makelist calls on a computer. This exercise demonstrates that we can write a function and have in mind certain types of arguments, here typically int and float objects. However, the function can be used with other (originally unintended) arguments, such as lists and strings in the present case, leading to strange and irrelevant behavior (the problem here lies in the boolean expression value \(<=\) stop which is meaningless for some of the arguments).
Problem 29
Find an error in a program. Consider the following program for computing $$ f(x)=e^{r x} \sin (m x)+e^{s x} \sin (n x) $$ def \(f(x, m, n, r, s):\) return expsin \((x, r, m)+\operatorname{expsin}(x, s, n)\) \(x=2.5\) print \(f(x, 0.1,0.2,1,1)\) from math import exp, sin def expsin \((x, p, q):\) return \(\exp \left(p^{* x}\right) * \sin \left(q^{* x}\right)\) Running this code results in NameError: global name 'expsin' is not defined What is the problem? Simulate the program flow by hand or use the debugger to step from line to line. Correct the program.