diff --git a/programming/lecture/programming.tex b/programming/lecture/programming.tex index 6779b5f..47d1d04 100644 --- a/programming/lecture/programming.tex +++ b/programming/lecture/programming.tex @@ -1259,9 +1259,9 @@ end \end{lstlisting} \begin{exercise}{logicalIndexingBenchmark.m}{logicalIndexingBenchmark.out} - Above we claimed the logical indexing is faster that manual - selection of element of a vector. By now we have all the tools at - hand to test this. \\ + Above we claimed the logical indexing is faster and much more + convenient than the manual selection of elements of a vector. By now + we have all the tools at hand to test this. \\ For this test create a large vector with 100000 (or more) random numbers. Filter from this vector all numbers that are less than 0.5 and copy them to a second vector. Surround you code with the brother @@ -1279,43 +1279,48 @@ end the agent takes a step in a random direction. \begin{itemize} \item The program should do 10 random walks with 1000 steps each. - \item With each step decide randomly whether the position is changed by $+1$ or $-1$. + \item With each step decide randomly whether the position is changed + by $+1$ or $-1$. \item Store all positions. - \item Create a figure in which you plot the position as a function of the steps. + \item Create a figure in which you plot the position as a function + of the steps. \end{itemize} \end{exercise} -\section{Skripte und Funktionen} +\section{Scripts and functions} -\subsection{Was ist ein Programm?} +\subsection{What is a program?} -Ein Programm ist eine Sammlung von Anweisungen, die in einer Datei auf -dem Rechner abgelegt sind. Wenn es durch den Aufruf zum Leben erweckt -wird, dann wird es Zeile f\"ur Zeile von oben nach unten ausgef\"uhrt. +A program is little more than a collection of statement stored in a +file on the computer. When it is \emph{called}, it is brought to life +and executed line-by-line from top to bottom. -\matlab{} kennt drei Arten von Programmen: +\matlab{} knows three types of programs: \begin{enumerate} -\item \codeterm[Skript]{Skripte} -\item \codeterm[Funktion]{Funktionen} -\item \codeterm[Objekt]{Objekte} (werden wir hier nicht behandeln) +\item \codeterm[Script]{Scripts} +\item \codeterm[Function]{Functions} +\item \codeterm[Object]{Objects} (not covered here) \end{enumerate} -Alle Programme werden in den sogenannten \codeterm{m-files} gespeichert -(z.B. \file{meinProgramm.m}). Um sie zu benutzen werden sie von der -Kommandozeile aufgerufen oder in anderen Programmen -verwendet. Programme erh\"ohen die Wiederverwertbarkeit von -Programmcode. Bislang haben wir ausschlie{\ss}lich Skripte -verwendet. Dabei wurde jede Variable, die erzeugt wurde im -\codeterm{Workspace} abgelegt und konnte wiederverwendet werden. Hierin -liegt allerdings auch eine Gefahr. In der Regel sind Datenanalysen auf -mehrere Skripte verteilt und alle teilen sich den gemeinsamen -Workspace. Verwendet nun ein aufgerufenes Skript eine bereits -definierte Variable und weist ihr einen neuen Wert zu, dann kann das -erw\"unscht und praktisch sein. Wenn es aber unbeabsichtigt passiert -kann es zu Fehlern kommen, die nur sehr schwer erkennbar sind, da ja -jedes Skript f\"ur sich enwandtfrei arbeitet. Eine L\"osung f\"ur -dieses Problem bieten die \codeterm[Funktion]{Funktionen}. - -\subsection{Funktionen} + +Programs are stored in so called \codeterm{m-files} +(e.g. \file{myProgram.m}). To use them they have to be \emph{called} +from the command line of within another program. Storing your code in +programs increases the re-usability. So far we have used +\emph{scripts} to store the solutions of the exercises. Any variable +that was created appeared in the \codeterm{workspace} and existed even +after the program was finished. This is very convenient but also bears +some risks. Consider the case that \file{script_a.m} creates a certain +variable and assigns a value to it for later use. Now it calls a +second program (\file{script_b.m}) that, by accident, uses the same +variable name and assigns a different value to it. When +\file{script_b.m} is done, the control returns to \file{script_a.m} +and if it now want to read the previously stored variable, it will +contain a different value than expected. Bugs like this are hard to +track down since each of the programs alone is perfectly fine and +works as intended. A solution for this problem are the +\codeterm[Function]{functions}. + +\subsection{Functions} Eine Funktion in \matlab{} wird \"ahnlich zu einer mathematischen Funktion definiert: