[exercises] translate scripts and functions

This commit is contained in:
Jan Grewe 2017-11-05 16:42:29 +01:00
parent 78f6b352a2
commit eb10de858e

View File

@ -16,7 +16,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 5}}{{\bfseries\large Skripte und Funktionen}}{{\bfseries\large 15. November, 2016}} \header{{\bfseries\large \"Ubung 5}}{{\bfseries\large Scripts and functions}}{{\bfseries\large 07. November, 2017}}
\firstpagefooter{Prof. Jan Benda}{Phone: 29 74 573}{Email: \firstpagefooter{Prof. Jan Benda}{Phone: 29 74 573}{Email:
jan.benda@uni-tuebingen.de} jan.benda@uni-tuebingen.de}
\runningfooter{}{\thepage}{} \runningfooter{}{\thepage}{}
@ -54,79 +54,82 @@
\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} \\ Neuroethology \hfill --- \hfill Institute for Neurobiology \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 and revision of the
Selbstkontrolle und sollten eigenst\"andig bearbeitet und gel\"ost lecture topic. You should try to solve them on your own. In contrast
werden. Im Gegensatz zu den vorherigen \"Ubungsbl\"attern k\"onnen die to previous exercises, the solutions can not be saved in a single file
L\"osungen nicht mehr in einer Datei gemacht werden. Die L\"osungen but each question needs an individual file. Combine the files into a
also als zip-Archiv auf ILIAS hochladen. Das Archiv sollte nach dem Muster: single zip archive and submit it via ILIAS. Name the archive according
``skripte\_funktionen\_\{nachname\}.zip'' benannt werden. to the pattern: ``scripts\_functions\_\{surname\}.zip''.
\begin{questions} \begin{questions}
\question Berechne die Fakult\"at einer Zahl. \question Calculate the factorial of a given number $n$.
\begin{parts} \begin{parts}
\part Version 1: Schreibe eine Skript, das die Fakult\"at von 5 berechnet und das \part{}
Resultat auf dem Bildschirm ausgibt. Version 1: Write a script that calculates the factorial of 5 and
prints out the result.
\begin{solution} \begin{solution}
\lstinputlisting{factorialscripta.m} \lstinputlisting{factorialscripta.m}
\end{solution} \end{solution}
\part Version 2: Wie Version 1, aber als Funktion, die als \part{}
Argument die Zahl, von der die Fakult\"at berechnet werden soll, Version 2: like version 1, but as a function that takes $n$ as
\"ubernimmt. input argument.
\begin{solution} \begin{solution}
\lstinputlisting{printfactorial.m} \lstinputlisting{printfactorial.m}
\lstinputlisting{factorialscriptb.m} \lstinputlisting{factorialscriptb.m}
\end{solution} \end{solution}
\part Version 3: Wie Version 2, die Funktion soll den berechneten \part{}
Wert nicht ausgeben, sondern als Funktionswert zur\"uckgeben. Das Version 3: like version 2, but the calculated result should not be
aufrufende Skript soll dann den berechneten Wert auf dem printed on the command line but returned by the function. Write a
Bildschirm ausgeben. script that calls the function and prints out the result.
\begin{solution} \begin{solution}
\lstinputlisting{myfactorial.m} \lstinputlisting{myfactorial.m}
\lstinputlisting{factorialscriptc.m} \lstinputlisting{factorialscriptc.m}
\end{solution} \end{solution}
\end{parts} \end{parts}
\question Grafische Darstellung einer Sinuswelle. \question Graphical display of a sinewave.
\begin{parts} \begin{parts}
\part Implementiere eine Funktion, die einen Sinus mit der \part{}
Amplitude 1 und der Frequenz $f = $ 50\,Hz plottet ($\sin(2\pi \cdot Implement a function that plots a sine with the amplitude 1 and
f \cdot t)$). Rufe die Funktion auf. the frequency $f=50$\,Hz $\left(\sin(2\pi \cdot f \cdot t)\right)$. Call the
function in a script.
\begin{solution} \begin{solution}
\lstinputlisting{plotsine50.m} \lstinputlisting{plotsine50.m}
\lstinputlisting{plotsinea.m} \lstinputlisting{plotsinea.m}
\end{solution} \end{solution}
\part Erweitere die Funktion so, dass die L\"ange der Zeitachse, \part{}
die Amplitude, und die Frequenz als Argumente \"ubergeben werden Improve the function that it takes the duration of the time axis,
k\"onnen. Die Schrittweite soll in der Funktion aus der Frequenz the amplitude and the frequency as input arguments. The
berechnet werden. calculation should use a temporal stepsize that is 0.01 of the
frequency.
\begin{solution} \begin{solution}
\lstinputlisting{plotsine.m} \lstinputlisting{plotsine.m} \lstinputlisting{plotsineb.m}
\lstinputlisting{plotsineb.m}
\end{solution} \end{solution}
\part Verlagere alle plot Befehle in das aufrufende Skript \part{}
und ver\"andere die Funktion so, dass sie sowohl den Sinus als Write a script that calls the function and controls the
auch die Zeitachse zur\"uckgibt. plotting. Change the function in a way that it returns a proper
time-axis and the calculated sinwave.
\begin{solution} \begin{solution}
\lstinputlisting{sinewave.m} \lstinputlisting{sinewave.m}
\lstinputlisting{plotsinec.m} \lstinputlisting{plotsinec.m}
\end{solution} \end{solution}
\part Schreibe eine zweite Funktion, die den Sinus plotted und \part{}
daf\"ur die Zeitachse und den Sinus als Argument erh\"alt. Diese Write a second function that does the plotting. It accepts the
Funktion soll die Achsen richtig beschriften. Schreibe ein kleines time and the sine as arguments. Make sure, that the plot is
Skript, dass beide Funktionen aufruft, um einen Sinus von 5\,Hz properly labeled. Write a small script that uses both funtions to
mit der Amplitude 2 \"uber 1.5 Sekunden zu plotten. plot a sine of $5$\,Hz frequency and an amplitude of 2 for a
duration of 1.5\,s.
\begin{solution} \begin{solution}
\lstinputlisting{plotsinewave.m} \lstinputlisting{plotsinewave.m}
\lstinputlisting{plotsined.m} \lstinputlisting{plotsined.m}
@ -138,40 +141,46 @@ also als zip-Archiv auf ILIAS hochladen. Das Archiv sollte nach dem Muster:
%Vektor zur\"uckgibt. Welche Argumente muss die Funktion %Vektor zur\"uckgibt. Welche Argumente muss die Funktion
%\"ubernehmen? %\"ubernehmen?
\question Random Walk. \question Random Walk. In a 1-D random walk an \emph{agent} walks
\begin{parts} randomly either in the one ($+1$) or the other ($-1$)
\part Lies die Aufgabe bis zum Ende durch. \"Uberlege dir dann ein direction. With each simulation step one direction is chosen and the
geeignetes ``Programmlayout'' aus Funktionen und Skripten. position is updated accordingly.
Was w\"are eine geeigente Funktion f\"ur diese Aufgabe? Welche \begin{parts}
Argumente sollte sie entgegennehmen? Was soll sie berechnen und \part{}
zur\"uckgeben? Read the exercise completely before starting the implementation
and then come up with a proper program layout of scripts and
functions. What would be a suitable function to solve the task? Which
arguments should it take? Which results should it return?
\begin{solution} \begin{solution}
One function that computes one realisation of a random walk. One function that computes one realisation of a random walk.
Scripts for plotting and analysis. Scripts for plotting and analysis.
\lstinputlisting{randomwalkthresh.m} \lstinputlisting{randomwalkthresh.m}
\end{solution} \end{solution}
\part Simuliere und plotte die Positionen von 10 Realisationen \part{}
eines random walk mit gleichen Wahrscheinlichkeiten f\"ur beide Run the simulation 10 times and plot the time-course of the
Richtungen. Jeder Walker startet an der Position 0 und soll so positions of the \emph{agents} into the same plot. The probability
lange laufen, bis er den Wert 50 \"uberschreitet oder den Wert of the two directions should be the same. Each \emph{agent} starts
$-50$ unterschreitet. at position $0$ and the simulation should run until the position is
greater than $50$ or less than $-50$.
\begin{solution} \begin{solution}
\lstinputlisting{randomwalkscriptb.m} \lstinputlisting{randomwalkscriptb.m}
\end{solution} \end{solution}
\part Jetzt wollen wir die Wahrscheinlichkeit $p$ f\"ur eine \part{}
Bewegung zu gr\"o{\ss}eren Positionen im Bereich $0.5 \le p < 0.8$ Now we want to know how the probability of $p_{+1}$ (the
variieren. Simuliere 10 Realisationen des random walk f\"ur vier probability to walk into the $+1$ direction) impacts the random
verschiedene Wahrscheinlichkeiten. walk. Vary $p_{+1}$ in the range $0.5 \le p_{+1} < 0.8$. Do 10
random walks for four probabilities (apply the same thresholds for
stopping the simulations as before).
\begin{solution} \begin{solution}
\lstinputlisting{randomwalkscriptc.m} \lstinputlisting{randomwalkscriptc.m}
\end{solution} \end{solution}
\part Wie entwickelt sich die mittlere ben\"otigte Schrittanzahl \part{}
in Abh\"angigkeit der Wahrscheinlichkeit? Stelle die Mittelwerte How does $p_{+1}$ affect the number of simulation steps? Plot the
und die Standardabweichungen graphisch dar. averages and standard deviations as a function of $p_{+1}$.
\begin{solution} \begin{solution}
\lstinputlisting{randomwalkscriptd.m} \lstinputlisting{randomwalkscriptd.m}
\end{solution} \end{solution}