This commit is contained in:
Jan Grewe 2019-10-18 11:01:13 +02:00
commit 55b3451152
2 changed files with 21 additions and 20 deletions

View File

@ -1,4 +1,4 @@
\documentclass[12pt,a4paper,pdftex, answers]{exam} \documentclass[12pt,a4paper,pdftex]{exam}
\usepackage[german]{babel} \usepackage[german]{babel}
\usepackage{natbib} \usepackage{natbib}

View File

@ -3,7 +3,7 @@
In this chapter we will cover the basics of programming in In this chapter we will cover the basics of programming in
\matlab{}. Starting with the concept of simple variables and data \matlab{}. Starting with the concept of simple variables and data
types, we introduce basic data structures, such as vecotrs and types, we introduce basic data structures, such as vectors and
matrices and show how one can work with them. Then we will address the matrices and show how one can work with them. Then we will address the
structures used to control the flow of the program and how to write structures used to control the flow of the program and how to write
scripts and functions. Only a few of the concepts discussed in this scripts and functions. Only a few of the concepts discussed in this
@ -14,13 +14,13 @@ simple.
\section{Variables and data types} \section{Variables and data types}
The ultimate goals of scientific computing is to analyze gathered The ultimate goal of scientific computing is to analyze gathered data,
data, correlate it with e.g. stimulus conditions and infer rules and correlate it with e.g. the stimulus conditions and infer rules and
dependencies. These may be used to constrain model that allow us to dependencies. These may be used to constrain model that allow us to
understand and predict a system's behavior. In order to work with data understand and predict a system's behavior. In order to work with data
we need to store it somehow. For this purpose we use \emph{variables} we need to store it somehow. For this purpose we use containers called
that allow us to store the data and give it a name for easy recognition \emph{variables}. Variables store the data and are named for easier
and to provide semantic meaning. recognition and to provide semantic meaning.
\subsection{Variables} \subsection{Variables}
@ -89,6 +89,7 @@ information but it is not suited to be used in programs (see
also the \code{who} function that returns a list of all defined also the \code{who} function that returns a list of all defined
variables, listing~\ref{varListing2}). variables, listing~\ref{varListing2}).
\newpage
\begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}] \begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
>>class(x) >>class(x)
ans = ans =
@ -115,6 +116,7 @@ x y z
variable names. variable names.
\end{important} \end{important}
\pagebreak[4]
\subsection{Working with variables} \subsection{Working with variables}
We can certainly work, i.e. do calculations, with variables. \matlab{} We can certainly work, i.e. do calculations, with variables. \matlab{}
knows all basic \codeterm[Operator!arithmetic]{arithmetic operators} knows all basic \codeterm[Operator!arithmetic]{arithmetic operators}
@ -125,7 +127,6 @@ such as \code[Operator!arithmetic!1add@+]{+},
\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3} \code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
shows their use. shows their use.
\pagebreak[4]
\begin{lstlisting}[label=varListing3, caption={Working with variables.}] \begin{lstlisting}[label=varListing3, caption={Working with variables.}]
>> x = 1; >> x = 1;
>> x + 10 >> x + 10
@ -465,7 +466,7 @@ Error using +
Matrix dimensions must agree. Matrix dimensions must agree.
\end{lstlisting} \end{lstlisting}
Element-wise multiplication and division to raise a vector to a given power requires a Element-wise multiplication, division, or raising a vector to a given power requires a
different operator with a preceding '.'. \matlab{} defines the different operator with a preceding '.'. \matlab{} defines the
following operators for element-wise operations on vectors following operators for element-wise operations on vectors
\code[Operator!arithmetic!3mule@.*]{.*}, \code[Operator!arithmetic!3mule@.*]{.*},
@ -683,7 +684,7 @@ straight-forward or efficient solution to the problem. Consider for
example that you have a 3-D matrix and you want the minimal number in example that you have a 3-D matrix and you want the minimal number in
that matrix. One could try to first find the minimum in each column, that matrix. One could try to first find the minimum in each column,
then compare it to the elements on each page, and so on. An then compare it to the elements on each page, and so on. An
alternative way is to make use of the so called \emph{linar indexing} alternative way is to make use of the so called \emph{linear indexing}
in which each element of the matrix is addressed by a single in which each element of the matrix is addressed by a single
number. The linear index thus ranges from 1 to number. The linear index thus ranges from 1 to
\code{numel(matrix)}. The linear index increases first along the 1st, \code{numel(matrix)}. The linear index increases first along the 1st,
@ -828,11 +829,11 @@ indexing, Section~\ref{logicalindexingsec}).
Truth tables (\ref{logicalandor}) are used to visualize the results of Truth tables (\ref{logicalandor}) are used to visualize the results of
Boolean expressions. A and B are statements that can be evaluated to Boolean expressions. A and B are statements that can be evaluated to
True or False. When they are combined with a logical AND the whole true or false. When they are combined with a logical AND the whole
expression is True only if both statements are True. The logical OR, expression is true only if both statements are true. The logical OR,
on the other hand, requires that at least one of the statements is on the other hand, requires that at least one of the statements is
True. The exclusive OR (XOR) is True only for cases in which one of true. The exclusive OR (XOR) is true only for cases in which one of
the statements but not both are True. There is no operator for XOR in the statements but not both are true. There is no operator for XOR in
\matlab{} it is realized via the function \code[xor()]{xor(A, B)}. \matlab{} it is realized via the function \code[xor()]{xor(A, B)}.
\begin{table}[tp] \begin{table}[tp]
@ -860,7 +861,7 @@ the statements but not both are True. There is no operator for XOR in
\end{table} \end{table}
Tables~\ref{logicalrelationaloperators} show the logical and Table~\ref{logicalrelationaloperators} shows the logical and
relational operators available in \matlab{}. The additional relational operators available in \matlab{}. The additional
\code[Operator!logical!and2@\&\&]{\&\&} and \code[Operator!logical!and2@\&\&]{\&\&} and
\code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so \code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so
@ -1360,11 +1361,11 @@ end
\begin{exercise}{simplerandomwalk.m}{} \begin{exercise}{simplerandomwalk.m}{}
Implement a 1-D random walk: Starting from the initial position $0$ Implement a 1-D random walk: Starting from the initial position $0$
the agent takes a step in a random direction. the agent takes a step in a random direction with each iteration of the simulation.
\begin{itemize} \begin{itemize}
\item The program should do 10 random walks with 1000 steps each. \item Implement the random walk. With each step decide randomly whether the position is changed
\item With each step decide randomly whether the position is changed by $+1$ or $-1$. The walk should have 1000 steps.
by $+1$ or $-1$. \item Extend the program to do 10 random walks with 1000 steps each.
\item Store all positions. \item Store all positions.
\item Create a figure in which you plot the position as a function \item Create a figure in which you plot the position as a function
of the steps. of the steps.
@ -1388,7 +1389,7 @@ and executed line-by-line from top to bottom.
Programs are stored in so called \codeterm{m-files} Programs are stored in so called \codeterm{m-files}
(e.g. \file{myProgram.m}). To use them they have to be \emph{called} (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 from the command line or from within another program. Storing your code in
programs increases the re-usability. So far we have used programs increases the re-usability. So far we have used
\emph{scripts} to store the solutions of the exercises. Any variable \emph{scripts} to store the solutions of the exercises. Any variable
that was created appeared in the \codeterm{workspace} and existed even that was created appeared in the \codeterm{workspace} and existed even