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