[exercises] translate control flow

This commit is contained in:
Jan Grewe 2017-11-02 17:18:02 +01:00
parent eda5f615f0
commit 035810ce95

View File

@ -14,7 +14,7 @@
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large \"Ubung 4}}{{\bfseries\large Kontrollstrukturen}}{{\bfseries\large 08. November, 2016}}
\header{{\bfseries\large Exercise 4}}{{\bfseries\large Control Flow}}{{\bfseries\large 30. October, 2017}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
@ -28,109 +28,92 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Einf\"uhrung in die wissenschaftliche Datenverarbeitung}\\[1ex]
\textbf{\Large Introduction to scientific computing}\\[1ex]
{\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} \\
\end{center}
Die folgenden Aufgaben dienen der Wiederholung, \"Ubung und
Selbstkontrolle und sollten eigenst\"andig bearbeitet und gel\"ost
werden. Die L\"osung soll in Form eines einzelnen Skriptes (m-files)
im ILIAS hochgeladen werden. Jede Aufgabe sollte in einer eigenen
``Zelle'' gel\"ost sein. Die Zellen \textbf{m\"ussen} unabh\"angig
voneinander ausf\"uhrbar sein. Das Skript sollte nach dem Muster:
``control\_flow\_\{nachname\}.m'' benannt werden
(z.B. control\_flow\_mueller.m).
The exercises are meant for self-monitoring, revision of the lecture
topic. You should try to solve them on your own. Your solution should
be submitted as a single script (m-file) in the Ilias system. Each
task should be solved in its own ``cell''. Each cell must be
executable on its own. The file should be named according to the following pattern:
``variables\_datatypes\_\{lastname\}.m'' benannt werden
(e.g. variables\_datentypes\_mueller.m).
\begin{questions}
\question Implementiere \code{for} Schleifen bei denen die Laufvariable:
\question Implement \code{for} loops in which the \emph{running variable}:
\begin{parts}
\part ... von 0 bis 10 l\"auft. Gib den Wert der Laufvariable im
Schleifenk\"orper aus.
\part ... von 10 bis 0 l\"auft. Gib den Wert der Laufvariable aus.
\part ... von 0 bis 1 in einer Schrittweite von 0.1 l\"auft. Gib
die Laufvariable aus.
\part ... assumes values from 0 to 10. Print the value of the running variable for each iteration step.
\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.
\end{parts}
\question Zugriff auf Elemente in Vektoren:
\question Indexing in vectros:
\begin{parts}
\part Definiere einen Vektor \code{x} mit Werten von 1:100.
\part Benutze eine \code{for} Schleife um jedes Element von
\code{x} auszugeben indem die Laufvariable zum indizieren benutzt wird.
\part ... gleiches nur ohne eine Laufvariable zu nutzen.
\part Define a vector \code{x} that contains values from 1:100.
\part Use a \code{for} loop to print each element of \code{x}. Use the running variable for the indexing.
\begin{solution}
\code{for i = 1:length(x); disp(x(i)); end}
\end{solution}
\part Do the same without using the running variable for indexing.
\begin{solution}
\code{for i : x; disp(i); end;}
\end{solution}
\end{parts}
\question Erzeuge einen Vektor \verb+x+ mit 50 Zufallszahlen im
Bereich 0 - 10.
\question Create a vector \verb+x+ that contains 50 random numbers in the range 0 - 10.
\begin{parts}
\part Benutze eine Schleife um das arithmetische Mittel zu
berechnen. Der Mittelwert ist definiert als:
\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 $.
\part Benutze eine Schleife um die Standardabweichung zu
bestimmen:
\part Use a loop to estimate the stadard deviation:
$\sigma=\sqrt{\frac{1}{n}\sum\limits_{i=0}^{n}(x_i-\overline{x})^2}$).
\part Suche in der MATLAB Hilfe nach Funktionen, die das f\"ur
dich tun :-).
\part Search the MATLAB help for functions that do the calculations for you :-).
\end{parts}
\question Implementiere eine \code{while} Schleife
\question Implement a\code{while} loop
\begin{parts}
\part ... die 100 mal durchlaufen wird. Gib den aktuellen
Durchlauf im Schleifenk\"orper aus.
\part ... die endlos l\"auft. Sie kann mir \code{Strg + C}
abgebrochen werden.
\part ... that iterates 100 times. Print out the current iteration number.
\part ... iterates endlessly. You can interrupt the execution with the command \code{Strg + C}.
\end{parts}
\question Nutze eine endlose \code{while} Schleife um einzeln die
Elemente eines Vektor der L\"ange 10 auszugeben.
\question Use an endless \code{while} loop to individually print out the elements of a vector that contains 10 elements.
\question Benutze eine endlose \verb+while+ Schleife um so lange
Zufallszahlen (\verb+randn+) zu ziehen, bis eine Zahl gr\"o{\ss}er
1.33 gezogen wurde.
\question Use the endless \verb+while+ loop to draw random numbers
(\verb+randn+) until you it is larger than 1.33.
\begin{parts}
\part Z\"ahle die Anzahl n\"otiger Versuche.
\part Nuzte eine \code{for} Schleife um den vorherigen Test
1000 mal laufen zu lassen. Merke Dir alle Anzahlen und berechne den
Mittelwert davon.
\part Plotte die Anzahl notwendiger Versuche.
\part Spiele mit der Schwelle, was passiert?
\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 Create a plot that schows for each of the 1000 trials, how often you had to draw.
\part Play around with the threshold. What happens?
\end{parts}
\question Erstelle \verb+x+ einen Vektor mit 10 Zufallszahlen im
Bereich 0:10.
\question Create a vector \verb+x+ that contains 10 random numbers in the range 0:10.
\begin{parts}
\part Benutze eine \code{for} Schleife um all die Elemente zu
loeschen (\code{x(index) = [];}), die kleiner als 5 sind.
\part Loesche alle Elemente die kleiner als 5 und groesser als 2
sind.
\part Kann man das gleiche auch ohne eine Schleife erledigen?
\part Use a \code{for} loop to delete all those elements
(\code{x(index) = [];}) that are smaller than 5.
\part Delete all elements that are smaller than 5 and larger than 2.
\part Can you do the same without a loop?
\end{parts}
\question Teste den Zufallsgenerator! Dazu z\"ahle die Anzahl der
Elemente, die durch folgende Grenzen getrennt werden [0.0, 0.2,
0.4, 0.6, 0.8, 1.0]. Speichere die Ergebnisse in einem passenden
Vektor. Nutze eine Schleife um 1000 Zufallszahlen mit
\verb+rand()+ (siehe Hilfe) zu ziehen. Was waere die Erwartung,
was kommt heraus?
\question String parsing: Mitunter werden Dateinamen von
Datens\"atzen benutzt um anzuzeigen, unter welchen Bedingungen die
Daten gewonnen wurden. Man muss also den Dateinamen parsen und die
f\"ur einen relevante Information herausfiltern.
\question Test the random number generator! To do so count the
number of elements that fall in 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
loop to draw 1000 random numbers with \verb+rand()+ (see help). What
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.
\begin{parts}
\part Erstelle eine Variable
\verb+filename = '2015-10-12_100Hz_1.25V.dat'+. Der Unterstrich
ist offensichtlich das verwendete Trennzeichen.
\part Benutze eine \verb+for+ Schleife um durch alle Zeichen zu
laufen. Vergleiche jedes Zeichen mit dem Unterstrich und merke
Dir die Positionen in einem Vektor.
\part Benutze eine zweite Schleife um durch diesen
Positionsvektor zu laufen und benutze die darin enthaltene
Information um den \code{filename} in Teile zu schneiden.
\part Gib die einzelnen Teile auf dem Bildschirm aus.
\part Create a variable
\verb+filename = '2015-10-12_100Hz_1.25V.dat'+. Obviously, the
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 second loop to iterate over the previously filled `position' vector and use this information to
cut filename appropriately.
\part Print out the individual parts.
\end{parts}
\end{questions}