[translation] programming chapter first part translated 1/6

This commit is contained in:
Jan Grewe 2016-10-08 16:53:21 +02:00
parent 30e37ba4be
commit bc5aebc793

View File

@ -1,23 +1,23 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Programmierung in \matlab} \chapter{Programming in \matlab}
\section{Variablen und Datentypen} \section{Variables and data types}
\subsection{Variablen} \subsection{Variables}
Eine \determ{Variable} ist ein Zeiger auf eine Stelle im A \enterm{variable} is a pointer to a certain place in the computer's
Speicher. Dieser Zeiger hat einen Namen, den Variablennamen, und einen memory. This pointer is characterized by its name, the variable's
\determ{Datentyp} (Abbildung \ref{variablefig}). Im Speicher wird der name, and the \enterm{data type} (figure~\ref{variablefig}). In the
Wert der Variablen bin\"ar als eine Folge von \determ[Bit]{Bits} (0 computer's memory the value of the variable is stored in binary form
oder 1) gespeichert. Wird auf den Wert der Variable zugegriffen, wird that is as a sequence of zeros and ones (\enterm[Bit]{Bits}). When the
dieses Bitmuster je nach Datentyp interpretiert. Das Beispiel in variable is read from the memory, this binary pattern is interpreted
Abbildung \ref{variablefig} zeigt, dass das gleiche Bitmuster im einen according to the data type. The example shown in
Fall als 8-Bit Integer Datentyp zur Zahl 38 interpretiert wird und im figure~\ref{variablefig} shows that the very same bit pattern is either
anderen Fall als Character zum kaufm\"annischen ``und'' ausgewertet interpreted as a 8-bit integer type (numeric value 38) or as a
wird. In \matlab{} sind Datentypen nicht von sehr zentraler ampersand (&) character. In \matlab{} data types are of only minor
Bedeutung. Wir werden uns dennoch sp\"ater etwas genauer mit ihnen importance but there are occasions where it becomes important to know
befassen. the type of a variable and we will come back to them later on.
\begin{figure} \begin{figure}
\centering \centering
@ -29,19 +29,20 @@ befassen.
\includegraphics[width=.8\textwidth]{variableB} \includegraphics[width=.8\textwidth]{variableB}
\label{variable:b} \label{variable:b}
\end{subfigure} \end{subfigure}
\titlecaption{Variablen.}{Variablen sind Zeiger auf eine Adresse \titlecaption{Variables.}{Variables are point to a memory
im Speicher, die einen Namen und einen Datentypen beinhalten. Im address. They further are described by their name and
Speicher ist der Wert der Variable bin\"ar gespeichert. Abh\"angig data type. The variable's value is stored as a pattern of binary
vom Datentyp wird dieses Bitmuster unterschiedlich values (0 or 1). When reading the variable this pattern is
interpretiert.}\label{variablefig} interpreted according to the variable's
data type.}\label{variablefig}
\end{figure} \end{figure}
\subsection{Erzeugen von Variablen} \subsection{Creating variables}
In \matlab{} kann eine Variable auf der Kommandozeile, in einem Skript In \matlab{} variables can be created at any time on the command line
oder einer Funktion an beliebiger Stelle erzeugt werden. Listing or any place in a script or function. Listing~\ref{varListing1} shows
\ref{varListing1} zeigt drei Beispiele: three different possibilities:
\begin{lstlisting}[label=varListing1, caption={Erzeugen von Variablen.}] \begin{lstlisting}[label=varListing1, caption={Creating variables.}]
>> x = 38 >> x = 38
x = x =
38 38
@ -55,22 +56,22 @@ z =
A A
\end{lstlisting} \end{lstlisting}
Die Zeile 1 kann etwa so gelesen werden:''Erzeuge eine Variable mit Line 1 can be read like: ``create a variable with the name \varcode{x}
dem Namen \varcode{x} und weise ihr den Wert 38 zu''. Das and assign the value 38''. The equal sign is the so called
Gleichheitszeichen ist der sogenannte \codeterm{assignment operator}. Line 5 defines a variable \varcode{y}
\codeterm{Zuweisungsoperator}. Zeile 5 definiert eine Variable \varcode{y}, der and assigns an empty value. If not explicitly specified \matlab{}
ein leerer Wert zugewiesen wird. Da \matlab{}, wenn nicht anders variables will have the \codeterm{double} (a numeric data type, see
angegeben, immer den \codeterm{double} Datentypen benutzt, haben beide below) data type. In line 9, however, we create a variable \varcode{z}
Variablen diesen Datentyp. In Zeile 9 wird der Variablen \varcode{z} der Buchstabe and assign the character ``A'' to it. Accordingly, \varcode{z} does
``A'' zugewiesen. \varcode{z} ist nicht ein Flie{\ss}kommazahl von Typ \codeterm{double}, not have the numeric \codeterm{double} data type but is of the type
sondern ein \codeterm{character} (Zeichen). \codeterm{character}.
Der Datentyp einer Variable kann mit \code{class()} abgefragt werden. The actual data type of a variable can be found out with the
Eine Liste aller definierten Variablen gibt \code{who} \code{class()} function. \code{who} prints a list of all defined
zur\"uck. Detailliertere Informationen \"uber Variablen zeigt variables and \code{whos} provides detailed information
\code{whos} an. (listing~\ref{varListing2}).
\begin{lstlisting}[label=varListing2, caption={Erfragen des Datentyps einer Variable, Listen aller definierten Variablen.}] \begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
>>class(x) >>class(x)
ans = ans =
double double
@ -88,32 +89,30 @@ x y z
z 1x1 2 char z 1x1 2 char
\end{lstlisting} \end{lstlisting}
\begin{important}[Namen von Variablen] \begin{important}[Naming conventions]
Bei der Namensgebung ist zu beachten, dass \matlab{} auf Gro{\ss}- There are a few rules regarding the variable names. \matlab{} is
und Kleinschreibung achtet und ein Variablenname mit einem case-sensitive, i.e. \code{x} and \code{X} are two different
alphabetischen Zeichen beginnen muss. Umlaute, Sonder- und names. Names must begin with an alphabetic character. German (or
Leerzeichen sind in Variablennamen nicht erlaubt. other) umlauts, special characters and spaces are forbidden.
\end{important} \end{important}
\subsection{Arbeiten mit Variablen} \subsection{Working with variables}
We can certainly work, i.e. do calculations, with variables. \matlab{}
Nat\"urlich kann mit den Variablen auch gearbeitet, bzw. gerechnet knows all basic \codeterm[Operator!arithmetic]{arithmetic operators}
werden. \matlab{} kennt alle normalen such as \code[Operator!arithmetic!1add@+]{+},
\codeterm[Operator!arithmetischer]{arithmetischen Operatoren} wie \code[Operator!arithmetic!2sub@-]{-},
\code[Operator!arithmetischer!1add@+]{+}, \code[Operator!arithmetic!3mul@*]{*} and
\code[Operator!arithmetischer!2sub@-]{-}, \code[Operator!arithmetic!4div@/]{/}. The power is denoted by the
\code[Operator!arithmetischer!3mul@*]{*} und \code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
\code[Operator!arithmetischer!4div@/]{/}. Die Potenz wird \"uber das show their use.
Dachsymbol \code[Operator!arithmetischer!5pow@\^{}]{\^{}}
dargestellt. Listing \ref{varListing3} zeigt, wie sie benutzt werden.
\pagebreak[4] \pagebreak[4]
\begin{lstlisting}[label=varListing3, caption={Rechnen mit Variablen.}] \begin{lstlisting}[label=varListing3, caption={Working with variables.}]
>> x = 1; >> x = 1;
>> x + 10 >> x + 10
ans = ans =
11 11
>> x % x wurde nicht veraendert >> x % x has not changed!
ans = ans =
1 1
@ -131,83 +130,83 @@ z =
z = z =
15 15
>> clear z % loesche die Variable z >> clear z % deleting a variable
\end{lstlisting} \end{lstlisting}
Beachtenswert ist in Zeilen 2 und 6, dass mit dem Inhalt einer Note: in lines 2 and 6 the values of the variables have been used
Variablen gerechnet werden kann, ohne dass dadurch ihr Wert without changing their values. Whenever the value of a variable should
ver\"andert wird. Wenn der Wert einer Variablen ver\"andert werden change, the \code[Operator!Assignment!=]{=} operator has to be used
soll, dann muss der neue Wert explizit einer Variablen zugewiesen werden (lines 14 and 18). Line 23, finally shows how to delete a variable.
(mit dem \code[Operator!Zuweisung!=]{=} Zuweisungsoperator,
z.B. Zeilen 14 und 18). Zeile 23 zeigt wie eine einzelne Variable
gel\"oscht wird.
\subsection{Datentypen} \subsection{Data types}
Der Datentyp bestimmt, wie die im Speicher abgelegten Bitmuster As mentioned above, the data type associated with a variable defines how the stored bit pattern is interpreted. The major data types are:
interpretiert werden. Die wichtigsten Datentpyen sind:
\begin{itemize} \begin{itemize}
\item \codeterm{integer}: Ganze Zahlen. Hier gibt es mehrere \item \codeterm{integer}: Integer numbers. There are several subtypes
Unterarten, die wir in \matlab{} (meist) ignorieren k\"onnen. which, for most use-cases, can be ignored when working in \matlab{}.
\item \codeterm{double}: Flie{\ss}kommazahlen. Im Gegensatz zu den reelen Zahlen, die durch diesen Datentyp dargestellt werden, sind sie abz\"ahlbar. \item \codeterm{double}: Floating point numbers. In contrast to the
\item \codeterm{complex}: Komplexe Zahlen. real numbers that are represented with this data type the number of
\item \codeterm{logical}: Boolesche Werte, die als wahr numeric values that can be represented is limited (countable?).
(\code{true}) oder falsch (\code{false}) interpretiert werden. \item \codeterm{complex}: Complex numbers having a real and imaginary
\item \codeterm{char}: ASCII Zeichen part.
\item \codeterm{logical}: Boolean values that can be evaluated to
\code{true} or \code{false}.
\item \codeterm{char}: ASCII characters.
\end{itemize} \end{itemize}
Unter den numerischen Datentypen gibt es verschiedene Arten mit There is a variety of numeric data types that require different memory
unterschiedlichem Speicherbedarf und Wertebreich (siehe demands and ranges of representable values (table~\ref{dtypestab}).
Tabelle~\ref{dtypestab}).
\begin{table}[t] \begin{table}[t]
\centering \centering
\titlecaption{Grundlegende numerische Datentypen und ihr Wertebereich.}{} \titlecaption{Numeric data types and their ranges.}{}
\label{dtypestab} \label{dtypestab}
\begin{tabular}{llcl}\hline \begin{tabular}{llcl}\hline
Datentyp & Speicherbedarf & Wertebereich & Beispiel \erh \\ \hline Data type & memory demand & range & example \erh \\ \hline
\code{double} & 64 bit & $\approx -10^{308}$ bis $\approx 10^{308} $& Flie{\ss}kommazahlen.\erb\\ \code{double} & 64 bit & $\approx -10^{308}$ to $\approx 10^{308}
\code{int} & 64 bit & $-2^{31}$ bis $2^{31}-1$ & Ganzzahlige Werte \\ $& Floating point numbers.\erb\\ \code{int} & 64 bit & $-2^{31}$
\code{int16} & 16 bit & $-2^{15}$ bis $2^{15}-1$ & Digitalisierte Spannungen. \\ to $2^{31}-1$ & Integer values. \\ \code{int16} & 16 bit &
\code{uint8} & 8 bit & $0$ bis $255$ & Digitalisierte Imaging Daten. \\ \hline $-2^{15}$ to $2^{15}-1$ & Digitizes measurements. \\ \code{uint8}
& 8 bit & $0$ bis $255$ & Digitized intensities of colors in
images. \\ \hline
\end{tabular} \end{tabular}
\end{table} \end{table}
\matlab{} arbeitet meist mit dem \codeterm{double} Datentyp wenn numerische By default \matlab{} uses the \codeterm{double} data type whenever
Daten gespeichert werden. Dennoch lohnt es sich, sich ein wenig mit numerical values have to be stored. Nevertheless there are use-cases
den Datentypen auseinanderzusetzen (Box \ref{daqbox}). in which different data types are better suited. Box~\ref{daqbox}
exemplifies such a case.
\begin{ibox}[t]{\label{daqbox}Digitalisierung von Messdaten}
Szenario: Die elektrische Aktivit\"at (z.B. die Membranspannung) \begin{ibox}[t]{\label{daqbox}Digitizing measurements}
einer Nervenzelle wird gemessen. Die gemessene Spannung wird mittels
Messkarte digitalisiert und auf dem Computer f\"ur weitere Analysen Scenario: The electric activity (e.g. the membrane potential) of a
gespeichert. Typischerweise k\"onnen mit solchen Messkarten nerve cell is recorded. The measurements are digitized and stored on
Spannungen im Bereich $\pm 10$\,V gemessen werden. Die Aufl\"osung the hard disk of a computer for later analysis. This is done using a
der Analog-Digitalwandler betr\"agt heutzutage meistens 16 bit. Das Data Acquisition system (DAQ) that converts the analog measurements
heisst, dass der gesamte Spannungsbereich in $2^{16}$ Schritte into computer digestible digital format. Typically these systems
eingeteilt ist. Die gemessenene Spannung wird auf digitalisierte have a working range of $\pm 10$\,V. This range is usually resolved
Werte abgebildet.\vspace{0.25cm} with a precision of 16 bit. This means that the full potential range
is mapped onto $2^{16}$ digital values.\vspace{0.25cm}
\begin{minipage}{0.5\textwidth} \begin{minipage}{0.5\textwidth}
\includegraphics[width=0.9\columnwidth]{data_acquisition} \includegraphics[width=0.9\columnwidth]{data_acquisition}
\end{minipage} \end{minipage}
\begin{minipage}{0.5\textwidth} \begin{minipage}{0.5\textwidth}
Um die Spannung auf den \code{int16} Datentyp abzubilden: Mapping of the potential range onto a \code{int16} data type:
\[ y = x \cdot 2^{16}/20\] mit $x$ der gemessenen Spannung und $y$ \[ y = x \cdot 2^{16}/20\] with $x$ being the measured potential and $y$
dem digitalisierten Wert bei einem Spannungsbereich von the digitized value at a potential range of $\pm10$\,V and a
$\pm10$\,V. Das ergibt ganzzahlige Werte zwischen $-2^{15}=-32768$ resolution of 16 bit. Resulting values are integer numbers in the
und $2^{15}-1 = 32767$. range $-2^{15}=-32768$ to $2^{15}-1 = 32767$.
Durch Umkehrung kann der digitalisierte Wert wieder The measured potential can be calculated from the digitized value
in eine Spannung zur\"uckgewandelt werden: by inverting the equation:
\[ x = y \cdot 20/2^{16} \] \[ x = y \cdot 20/2^{16} \]
\end{minipage}\vspace{0.25cm} \end{minipage}\vspace{0.25cm}
Um Speicherplatz zu sparen ist es sinnvoll, die gemessenen Daten als In this context it is most efficient to store the measured values as
\code{int16} anstelle der \code{double} Werte im Rechner abzulegen. Die \code{int16} instead of \code{double} numbers. Storing floating
Daten als Spannungswerte, also als Flie{\ss}kommawerte, point numbers requires four times more memory (8 instead of 2
abzulegen ben\"otigt den 4-fachen Speicherplatz (8 statt 2 Bytes) \codeterm{Byte}, 64 instead of 16 bit) and offers no additional
und bietet keine zus\"atzliche Information. information.
\end{ibox} \end{ibox}