-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlooplist.tex
More file actions
234 lines (172 loc) · 10.4 KB
/
looplist.tex
File metadata and controls
234 lines (172 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
%\section{Loops and lists}
The exercises of this chapter are about loops and list, and correspond to Chapter
3 in the book by Sundnes and Chapter 2 in the book of Langtangen.
\begin{Problem}{\textbf{Multiply by five}} \label{prob21}
%\addcontentsline{toc}{section}{Exercise 2.1: Multiply by five - \texttt{multiplication.py}}
\noindent Write a code printing out $5\cdot 1$, $5\cdot 2$, ..., $5\cdot 10$, using either
a \pythoninline{for} or a \pythoninline{while} loop.
Filename: $\texttt{multiplication.py}$
\end{Problem}
\begin{Problem}{\textbf{Multiplication table}}\label{prob22}
%\addcontentsline{toc}{section}{Exercise 2.2: Multiplication table - \texttt{mult\_table.py}}
\noindent Write a new code based on the one from Problem \ref{prob21}. This code should print the
whole miltiplication table from $1\cdot 1$ to $10 \cdot 10$.
\emph{Hint: You may want to consider using one loop inside another.}
Filename: $\texttt{mult\_table.py}$
\end{Problem}
\begin{Problem}{\textbf{Stirling's approximation}}\label{prob23}
%\addcontentsline{toc}{section}{Exercise 2.3: Stirling's approximation - \texttt{stirling.py}}
\noindent Stirling's approximation can be written $\ln (x!) \approx x\ln x - x$. This is a
good approximation for large $x$. Write out a nicely formatted table of integer
$x$ values, the actual value of $\ln (x!)$, and Stirling's approximation to $\ln (x!)$.
Filename: $\texttt{stirling.py}$
\end{Problem}
\begin{Problem}{\textbf{Errors in summation}}\label{sum_for}
%\addcontentsline{toc}{section}{Exercise 2.4: Errors in summation - \texttt{sum\_for.py}}
\noindent The following code is supposed to compute the sum $s = \sum_{k = 1}^{M}\frac{1}{(2k)^2}$, for $M=3$.
\begin{python}
s = 0
M = 3
for i in range(M):
s += 1/2*k**2
print(s)
\end{python}
The program has three errors and therefore does not work. Find the three errors and write a correct program. Put comments in your program to indicate what the mistakes were.
There are two basic ways to find errors in a program:
\begin{enumerate}
\item Read the program carefully line by line, and think about the consequences of each statement. Look for inconsistencies in the form of variables being used before
they are defined, and perform the calculations in the statements by hand to see how variables change their value.
\item Run the program and check for errors. In a Python program there may be errors of two different types. The first is an error that Python itself will
notice, and make the program stop with an error message. The message will indicate which line the error occurs, and alghough the error messages may be a little
cryptic these errors are usually quite easy to find and correct. The second type are errors where the program seems to run and complete normally, but the
result is not what we want. These errors are often harder to find, and a good method to find them is often to add \pythoninline{print} statements in the
code to write intermediate values of variables to the screen, and compare these values with hand calculations.
\end{enumerate}
Try the first method first (\emph{code inspection}) and find as many errors as you can. Thereafter, try the second method, by for instance
adding a print statement inside the for-loop to print the value of \pythoninline{k} and \pythoninline{s}.
Filename: $\texttt{sum\_for.py}$
\end{Problem}
\begin{Problem}{\textbf{Sum as a \pythoninline{while} loop}}\label{sum_while}
%\addcontentsline{toc}{section}{Exercise 2.4: Errors in summation - \texttt{sum\_for.py}}
Write the (corrected) \pythoninline{for} loop from the previous exercise as a
a \pythoninline{while} loop. Check that the two loops compute the same answer.
Filename: $\texttt{sum\_while.py}$
\end{Problem}
\begin{Problem}{\textbf{Binomial coefficient}}\label{prob26}
%\addcontentsline{toc}{section}{Exercise 2.5: Binomial coefficient - \texttt{binomial.py}}
\noindent The binomial coefficient is indexed by two integers $n$ and $k$ and is written
$\binom{n}{k}$. It is given by the formula
\begin{equation}\label{eq: binomial_factorial}
\binom{n}{k} = \frac{n!}{k!(n-k)!}.
\end{equation}
We can write this out, and get
\begin{equation}\label{eq: binomial_product}
\binom{n}{k} = \prod_{j = 1}^{n - k}\frac{k + j}{j}.
\end{equation}
Use Eq.~(\ref{eq: binomial_product}) and a \pythoninline{for} loop to find the binomial coefficient
for $n = 14$ and $k = 3$. Compute the same value using Eq.~(\ref{eq: binomial_factorial})
and check that the results are correct.
\emph{Hint: The $\prod$ sign is a product sign. Thus
$\prod_{j = 1}^{n - k}\frac{k + j}{j} = \frac{k + 1}{1}\frac{k + 2}{2}
\dots\frac{k + (n - k)}{(n - k)}$. When checking the result you will need
\pythoninline{math.factorial}.}
Filename: $\texttt{binomial.py}$
\end{Problem}
\begin{Problem}{\textbf{Table showing population growth}} \label{population_table}
\noindent Consider again the bacterial colony from Problem \ref{prob13}. We now want to
study how the population grows with time, by calculating the number
of individuals for $n + 1$ uniformly spaced $t$ values throughout the interval
$[0, 48]$. Set $n = 12$ and write a \py{for} loop which computes and stores
$t$ and $N$ values in two lists \pythoninline{t} and \pythoninline{N}. Thereafter,
traverse the two lists with a separate \pythoninline{for} loop and
write out a nicely formatted table of $t$ and $N$
values.
Filename: \texttt{population\_table.py}
\end{Problem}
\begin{Problem}{\textbf{Nested list}}\label{population_table2}
\paragraph{a)} Compute two lists of $t$ and $N$ values as explained in Problem
\ref{population_table}. Store the two lists in a new nested list \pythoninline{tN1} (havinglength
two) where the element \pythoninline{tN1[0]} is the list containing $t$-values,
and \pythoninline{tN[1]} is the list containing the $N$-values.
Write out a table
with $t$ and $N$ values in two columns by looping over the data in the \pythoninline{tN1}
list. Each $t$ and $N$ value should be written in the table as integers.
\paragraph{b)} Make a nested list \pythoninline{tN2} containing exactly the same data as in exercise
\textbf{a)}, but with a different structure. The \pythoninline{tN2} list shall
have the same length as the original $t$- and $N$-lists,
and each element \pythoninline{tN2[i]}
shall be a list of lenght two, which contains the $i$-th element of both the $t$-list and the $N$-list.
Loop over the \pythoninline{tN2} list and write out the $t$ and $N$
values in the table as integers. The output should look exactly as in exercise
\textbf{a)}, although the underlying structure of the lists is different.
Filename: $\texttt{population\_table2.py}$
\end{Problem}
\begin{Problem}{\textbf{Calculate Cesaro mean}}
\noindent Let $(a_n)_{n=1}^\infty$ be a sequence of numbers, $s_k=\sum_{n=0}^k a_n=a_0+\dots,+a_k$,
and
\begin{equation*}
S_N = \frac{1}{N-1}\sum_{k=0}^{N-1} s_k.
\end{equation*}
Let $(a_n)_{n=1}^\infty$ be the sequence with $a_n=(-1)^n$.
Calculate $S_N$ for \newline $N=1, 2, 3, 4, 5, 10, 50$ and print the results in a table.
Filename: $\texttt{cesaro\_mean.py}$
\end{Problem}
\begin{Problem}{\textbf{Catalan numbers}}
\noindent A number on the form
\begin{equation*}
C_n=\frac{1}{n+1}\binom{2n}{n}=\frac{(2n)!}{(n+1)!n!}
\end{equation*}
is called a \emph{Catalan number}. Compute and print the first 10 Catalan numbers.
Filename: $\texttt{catalan.py}$
\end{Problem}
\begin{Problem}{\textbf{Molar Mass of Alkanes}}
\noindent
Alkanes are saturated hydrocarbons with the chemical formula $\mathrm{C_nH_{2n+2}}$. If there are $n$ Carbon atoms in the alkane, there will be $m = 2n+2$ Hydrogen atoms. The molar mass of the hydrocarbon is $M_{\mathrm{C_n H_{m}}} = n M_{\mathrm{C}} + m M_{\mathrm{H}} $, where $M_C$ is the molar mass of Carbon and $M_H$ is the molar mass of Hydrogen.
Use a \pythoninline{for}-loop or a \pythoninline{while}-loop to compute and print out the molar mass of the alkanes with two through nine Carbon atoms ($n \in [2, 9]$). The output should specify the chemical formula of the alkane as well as the molar mass. An example on how the formatted output should look like for $n = 2$ is given below.
\begin{lstlisting}
M(C2H6) = 30.069 g/mol
\end{lstlisting}
You can set the molar masses of the atoms to be $M_{\mathrm{C}} = 12.011 \ \mathrm{g/mol}$ and $M_{\mathrm{H}} = 1.0079 \ \mathrm{g/mol}$
Hint: An output of a chemical formula like the one above can be constructed using a formatted string (f-string). If we have \py{n=2} and
\py{m=2*n+2}, the first part of the output above is given by the f-string \verb!f"M(C{n:d}H{6:d}) = ..."!.
Filename: $\texttt{alkane.py}$
\end{Problem}
\begin{Problem}{\textbf{Simulate a program by hand}}\label{interest_rate_loop}
\noindent Consider the following program for computing with interest rates:
\begin{python}
initial_amount = 100
r = 5.5 # interest rate
amount = initial_amount
years = 0
while amount <= 1.5*initial_amount:
amount = amount + r/100*amount
years = years + 1
print(years)
\end{python}
\paragraph{a)} Use a calculator or an interactive Python shell and work through the
program calculations by hand. Write down the value of \py{amount} and \py{years}
in each pass of the loop.
\paragraph{b)} Change the program to make use of the operator \py{+=} wherever possible.
\paragraph{c)} Explain with words what type of mathematical problem that is solved by this
program.
Filename: $\texttt{interest\_rate\_loop.py}$
\end{Problem}
\begin{Problem}{\textbf{Matrix elements}}
\begin{center}
\emph{This exercise involves no programming. \newline The answers should be written in a text file called $\texttt{matrix.txt}$}
\end{center}
\noindent Consider a two dimensional $3 \times 3$ matrix
\begin{equation*}
A = \begin{pmatrix}
a_{1 1} & a_{1 2} & a_{1 3} \\
a_{2 1} & a_{2 2} & a_{2 3} \\
a_{3 1} & a_{3 2} & a_{3 3}
\end{pmatrix} .
\end{equation*}
In Python, the matrix $A$ can be represented as a nested list \pythoninline{A}, either as a list of rows or a list of columns. Find the indices \pythoninline{i, j} of the Python list \pythoninline{A} such that \pythoninline{A[i][j]} = $a_{1 1}$ and the indices \pythoninline{k, l} such that \pythoninline{A[k][l]} = $a_{3 2}$ for the two following cases:
\paragraph{a)}
When $A$ is represented as a list of rows. This means that \pythoninline{A} contains three lists, where each list corresponds to a row in $A$.
\paragraph{b)}
When $A$ is represented as a list of columns. This means that each element in \pythoninline{A} contains a list with the elements of a column in $A$.
Filename: $\texttt{matrix.txt}$
\end{Problem}