\documentclass[12pt,a4paper,pdftex]{exam} \newcommand{\exercisetopic}{Scripts and Functions} \newcommand{\exercisenum}{6} \newcommand{\exercisedate}{6. November, 2018} \input{../../exercisesheader} \firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} \input{../../exercisestitle} The exercises are meant for self-monitoring and revision of the lecture. You should try to solve them on your own. In contrast to previous exercises, the solutions can not be saved in a single file but each question needs an individual file. Combine the files into a single zip archive and submit it via ILIAS. Name the archive according to the pattern: ``scripts\_functions\_\{surname\}.zip''. \begin{questions} \question Calculate the factorial of a given number $n$. \begin{parts} \part{} Version 1: Write a script that calculates the factorial of 5 and prints out the result. \begin{solution} \lstinputlisting{factorialscripta.m} \end{solution} \part{} Version 2: like version 1, but as a function that takes $n$ as input argument. \begin{solution} \lstinputlisting{printfactorial.m} \lstinputlisting{factorialscriptb.m} \end{solution} \part{} Version 3: like version 2, but the calculated result should not be printed on the command line but returned by the function. Write a script that calls the function and prints out the result. \begin{solution} \lstinputlisting{myfactorial.m} \lstinputlisting{factorialscriptc.m} \end{solution} \end{parts} \question Graphical display of a sinewave. \begin{parts} \part{} Implement a function that plots a sine with the amplitude 1 and the frequency $f=50$\,Hz $\left(\sin(2\pi \cdot f \cdot t)\right)$. Call the function in a script. \begin{solution} \lstinputlisting{plotsine50.m} \lstinputlisting{plotsinea.m} \end{solution} \part{} Improve the function that it takes the duration of the time axis, the amplitude, and the frequency as input arguments. The calculation should use a temporal stepsize that is 0.01 of the frequency. \begin{solution} \lstinputlisting{plotsine.m} \lstinputlisting{plotsineb.m} \end{solution} \part{} Write a script that calls the function and controls the plotting. Change the function in a way that it returns a proper time-axis and the calculated sinwave. \begin{solution} \lstinputlisting{sinewave.m} \lstinputlisting{plotsinec.m} \end{solution} \part{} Write a second function that does the plotting. It accepts the time and the sine as arguments. Make sure, that the plot is properly labeled. Write a small script that uses both funtions to plot a sine of $5$\,Hz frequency and an amplitude of 2 for a duration of 1.5\,s. \begin{solution} \lstinputlisting{plotsinewave.m} \lstinputlisting{plotsined.m} \end{solution} \end{parts} %\question Schreibe eine Funktion, die bin\"are Datens\"atze %('signal.bin' und 'signal2.bin' vom Montag) liest und die Daten als %Vektor zur\"uckgibt. Welche Argumente muss die Funktion %\"ubernehmen? \question Random Walk. In a 1-D random walk an \emph{agent} walks randomly either in the one ($+1$) or the other ($-1$) direction. With each simulation step one direction is chosen and the position is updated accordingly. \textbf{There are some differences to the solution discussed in the lecture!} \begin{parts} \part{} 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} One function that computes one realisation of a random walk. Scripts for plotting and analysis. \lstinputlisting{randomwalkthresh.m} \end{solution} \part{} Run the simulation 10 times and plot the time-course of the positions of the \emph{agents} into the same plot. The probability of the two directions should be the same. Each \emph{agent} starts at position $0$ and the simulation should run until the position is greater than $50$ or less than $-50$. \begin{solution} \lstinputlisting{randomwalkscriptb.m} \end{solution} \part{} Now we want to know how the probability of $p_{+1}$ (the probability to walk into the $+1$ direction) impacts the random walk. Vary $p_{+1}$ in the range $0.5 \le p_{+1} < 0.8$. Do 10 random walks for the four probabilities (apply the same thresholds for stopping the simulations as before). \begin{solution} \lstinputlisting{randomwalkscriptc.m} \end{solution} \part{} How does $p_{+1}$ affect the number of simulation steps? Plot the averages and standard deviations as a function of $p_{+1}$. \begin{solution} \lstinputlisting{randomwalkscriptd.m} \end{solution} \end{parts} %\question Modellierung des exponentiellen Wachstums einer isolierten %Population. Das exponentielle Wachstum einer isolierten Population %wird \"uber folgende Differentialgleichung beschrieben: %\begin{equation} % \frac{dN}{dt} = N \cdot r, %\end{equation} %mit $N$ der Populationsgr\"o{\ss}e und $r$ der Wachstumsrate. %\begin{parts} % \part L\"ose die Gleichung numerisch mit dem Euler Verfahren. % \part Implementiere eine Funktion, die die Populationsgr\"o{\ss}e % und die Zeit zur\"uckgibt. % \part Plotte die Populationsgr\"o{\ss}e als Funktion der Zeit. %\end{parts} %\question Etwas realistischer ist das logistische Wachstum einer %isolierten Population, bei der das Wachstum durch eine Kapazit\"at %gedeckelt ist. Sie wird mit folgender Differentialgleichung %beschrieben: %\begin{equation} % \frac{dN}{dt} = N \cdot r \cdot \left( 1 - \frac{N}{K} \right) %\end{equation} %mit $N$ der Population, der Wachstumsrate $r$ und $K$ der ``tragenden'' %Kapazit\"at. %\begin{parts} % \part Implementiere die L\"osung des logistischen Wachstums in % einer Funktion. Benutze das Euler Verfahren. Die Funktion soll die % Parameter $r$, $K$ sowie den Startwert von $N$ als Argumente % \"ubernehmen. % \part Die Funktion soll die Populationsgr\"o{\ss}e und die Zeit % zur\"uckgeben. % \part Simuliere das Wachstum mit einer Anzahl unterschiedlicher % Startwerte f\"ur $N$. % \part Stelle die Ergebnisse in einem Plot graphisch dar. % \part Plotte das Wachstum $dN/dt$ als Funktion der % Populationsgr\"o{\ss}e $N$. %\end{parts} \end{questions} \end{document}