[exercise5] control flow, some rewording

This commit is contained in:
Jan Grewe 2018-10-29 21:48:14 +01:00
parent ed5f323b25
commit 6c60fbc765

View File

@ -14,7 +14,7 @@
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry} \usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot} \pagestyle{headandfoot}
\header{{\bfseries\large Exercise 4}}{{\bfseries\large Control Flow}}{{\bfseries\large 30. October, 2017}} \header{{\bfseries\large Exercise 5}}{{\bfseries\large Control Flow}}{{\bfseries\large 30. October, 2018}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email: \firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de} jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{} \runningfooter{}{\thepage}{}
@ -33,39 +33,44 @@
\begin{center} \begin{center}
\textbf{\Large Introduction to scientific computing}\\[1ex] \textbf{\Large Introduction to scientific computing}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex] {\large Jan Grewe, Jan Benda}\\[-3ex]
Abteilung Neuroethologie \hfill --- \hfill Institut f\"ur Neurobiologie \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\ Neuroethologie \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center} \end{center}
The exercises are meant for self-monitoring, revision of the lecture The exercises are meant for self-monitoring and revision of the
topic. You should try to solve them on your own. Your solution should lecture. You should try to solve them on your own. Your solution
be submitted as a single script (m-file) in the Ilias system. Each should be submitted as a single script (m-file) in the Ilias
task should be solved in its own ``cell''. Each cell must be system. Each task should be solved in its own ``cell''. Each cell must
executable on its own. The file should be named according to the following pattern: be executable on its own. The file should be named according to the
``variables\_datatypes\_\{lastname\}.m'' benannt werden following pattern: ``variables\_datatypes\_\{lastname\}.m''
(e.g. variables\_datentypes\_mueller.m). (e.g. variables\_datentypes\_mueller.m).
\begin{questions} \begin{questions}
\question Implement \code{for} loops in which the \emph{running variable}: \question Implement \code{for} loops in which the \emph{running variable}:
\begin{parts} \begin{parts}
\part ... assumes values from 0 to 10. Print the value of the running variable for each iteration step. \part ... assumes values from 0 to 10. Print the value of the running variable for each iteration step.
\begin{solution}
for i = 1:10
disp(i);
end;
\end{solution}
\part ... assumes values from 10 to 0. Print out the value for each iteration. \part ... assumes values from 10 to 0. Print out the value for each iteration.
\part ... assumes values from 0 to 1 in steps of 0.1. Print out the value for each iteration. \part ... assumes values from 0 to 1 in steps of 0.1. Print out the value for each iteration.
\end{parts} \end{parts}
\question Indexing in vectros: \question Indexing in vectors:
\begin{parts} \begin{parts}
\part Define a vector \code{x} that contains values from 1:100. \part Define a vector \code{x} that contains values ranging from 101 to 200.
\part Use a \code{for} loop to print each element of \code{x}. Use the running variable for the indexing. \part Use a \code{for} loop to print each element of \code{x}. Use the running variable for the indexing.
\begin{solution} \begin{solution}
\code{for i = 1:length(x); disp(x(i)); end} \code{for i = 1:length(x); disp(x(i)); end}
\end{solution} \end{solution}
\part Do the same without using the running variable for indexing. \part Do the same without use the running variable directly (not for indexing).
\begin{solution} \begin{solution}
\code{for i : x; disp(i); end;} \code{for i : x; disp(i); end;}
\end{solution} \end{solution}
\end{parts} \end{parts}
\question Create a vector \verb+x+ that contains 50 random numbers in the range 0 - 10. \question Create a vector \verb+x+ that contains 50 random numbers in the value range 0 - 10.
\begin{parts} \begin{parts}
\part Use a loop to calculate the arithmetic mean. The mean is defined as: \part Use a loop to calculate the arithmetic mean. The mean is defined as:
$\overline{x}=\frac{1}{n}\sum\limits_{i=0}^{n}x_i $. $\overline{x}=\frac{1}{n}\sum\limits_{i=0}^{n}x_i $.
@ -80,14 +85,14 @@ executable on its own. The file should be named according to the following patte
\part ... iterates endlessly. You can interrupt the execution with the command \code{Strg + C}. \part ... iterates endlessly. You can interrupt the execution with the command \code{Strg + C}.
\end{parts} \end{parts}
\question Use an endless \code{while} loop to individually print out the elements of a vector that contains 10 elements. \question Use an endless \code{while} loop to individually print out the elements of a vector that contains 10 elements. Stop the execution after all elements have been printed.
\question Use the endless \verb+while+ loop to draw random numbers \question Use the endless \verb+while+ loop to draw random numbers
(\verb+randn+) until you it is larger than 1.33. (\verb+randn+) until you hit one that is larger than 1.33.
\begin{parts} \begin{parts}
\part Count the number of required iterations. \part Count the number of required iterations.
\part Use a \code{for} loop to run the previous test 1000 times. Remeber all counts and calculate the mean of it. \part Use a \code{for} loop to run the previous test 1000 times. Remember all counts and calculate the mean of it.
\part Create a plot that schows for each of the 1000 trials, how often you had to draw. \part Create a plot that shows for each of the 1000 trials, how often you had to draw.
\part Play around with the threshold. What happens? \part Play around with the threshold. What happens?
\end{parts} \end{parts}
@ -100,19 +105,19 @@ executable on its own. The file should be named according to the following patte
\end{parts} \end{parts}
\question Test the random number generator! To do so count the \question Test the random number generator! To do so count the
number of elements that fall in the classes defined by the edges number of elements that fall into the classes defined by the edges
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]. Store the results in a vector. Use a [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]. Store the results in a vector. Use a
loop to draw 1000 random numbers with \verb+rand()+ (see help). What loop to draw 1000 random numbers with \verb+rand()+ (see help). What
would be the expectation? What is the result? would be the expectation? What is the result?
\question String parsing: It is quite common to use the names of datasets to encode the conditions under which they were recorded. To find out the required information we have to \emph{parse} he name. \question String parsing: It is quite common to use the names of datasets to encode the conditions under which they were recorded. To find out the required information we have to \emph{parse} the name.
\begin{parts} \begin{parts}
\part Create a variable \part Create a variable
\verb+filename = '2015-10-12_100Hz_1.25V.dat'+. Obviously, the \verb+filename = '2015-10-12_100Hz_1.25V.dat'+. Obviously, the
underscore was used as a delimiter. underscore was used as a delimiter.
\part Use a \verb+for+ loop to find the positions of the underscores and store these in a vector. \part Use a \verb+for+ loop to find the positions of the underscores and store these in a vector.
\part Use a second loop to iterate over the previously filled `position' vector and use this information to \part Use a second loop to iterate over the previously filled `position' vector and use this information to
cut filename appropriately. cut \verb+filename+ appropriately.
\part Print out the individual parts. \part Print out the individual parts.
\end{parts} \end{parts}