This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/programming/exercises/control_flow.tex

127 lines
5.4 KiB
TeX

\documentclass[12pt,a4paper,pdftex]{exam}
\usepackage[german]{babel}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage[small]{caption}
\usepackage{sidecap}
\usepackage{pslatex}
\usepackage{amsmath}
\usepackage{amssymb}
\setlength{\marginparwidth}{2cm}
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large Exercise 5}}{{\bfseries\large Control Flow}}{{\bfseries\large 30. October, 2018}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\newcommand{\code}[1]{\texttt{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Introduction to scientific computing}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex]
Neuroethologie \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
The exercises are meant for self-monitoring and revision of the
lecture. 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''
(e.g. variables\_datentypes\_mueller.m).
\begin{questions}
\question Implement \code{for} loops in which the \emph{running variable}:
\begin{parts}
\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 0 to 1 in steps of 0.1. Print out the value for each iteration.
\end{parts}
\question Indexing in vectors:
\begin{parts}
\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.
\begin{solution}
\code{for i = 1:length(x); disp(x(i)); end}
\end{solution}
\part Do the same without use the running variable directly (not for indexing).
\begin{solution}
\code{for i : x; disp(i); end;}
\end{solution}
\end{parts}
\question Create a vector \verb+x+ that contains 50 random numbers in the value range 0 - 10.
\begin{parts}
\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 Use a loop to estimate the stadard deviation:
$\sigma=\sqrt{\frac{1}{n}\sum\limits_{i=0}^{n}(x_i-\overline{x})^2}$).
\part Search the MATLAB help for functions that do the calculations for you :-).
\end{parts}
\question Implement a \code{while} loop
\begin{parts}
\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 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
(\verb+randn+) until you hit one that is larger than 1.33.
\begin{parts}
\part Count the number of required iterations.
\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 shows for each of the 1000 trials, how often you had to draw.
\part Play around with the threshold. What happens?
\end{parts}
\question Create a vector \verb+x+ that contains 10 random numbers in the range 0:10.
\begin{parts}
\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 Test the random number generator! To do so count the
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
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} the name.
\begin{parts}
\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 \verb+filename+ appropriately.
\part Print out the individual parts.
\end{parts}
\end{questions}
\end{document}