Merge branch 'master' of https://whale.am28.uni-tuebingen.de/git/teaching/scientificComputing
This commit is contained in:
commit
55b3451152
@ -1,4 +1,4 @@
|
||||
\documentclass[12pt,a4paper,pdftex, answers]{exam}
|
||||
\documentclass[12pt,a4paper,pdftex]{exam}
|
||||
|
||||
\usepackage[german]{babel}
|
||||
\usepackage{natbib}
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
In this chapter we will cover the basics of programming in
|
||||
\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
|
||||
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
|
||||
@ -14,13 +14,13 @@ simple.
|
||||
|
||||
\section{Variables and data types}
|
||||
|
||||
The ultimate goals of scientific computing is to analyze gathered
|
||||
data, correlate it with e.g. stimulus conditions and infer rules and
|
||||
The ultimate goal of scientific computing is to analyze gathered data,
|
||||
correlate it with e.g. the stimulus conditions and infer rules and
|
||||
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
|
||||
we need to store it somehow. For this purpose we use \emph{variables}
|
||||
that allow us to store the data and give it a name for easy recognition
|
||||
and to provide semantic meaning.
|
||||
we need to store it somehow. For this purpose we use containers called
|
||||
\emph{variables}. Variables store the data and are named for easier
|
||||
recognition and to provide semantic meaning.
|
||||
|
||||
\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
|
||||
variables, listing~\ref{varListing2}).
|
||||
|
||||
\newpage
|
||||
\begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
|
||||
>>class(x)
|
||||
ans =
|
||||
@ -115,6 +116,7 @@ x y z
|
||||
variable names.
|
||||
\end{important}
|
||||
|
||||
\pagebreak[4]
|
||||
\subsection{Working with variables}
|
||||
We can certainly work, i.e. do calculations, with variables. \matlab{}
|
||||
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}
|
||||
shows their use.
|
||||
|
||||
\pagebreak[4]
|
||||
\begin{lstlisting}[label=varListing3, caption={Working with variables.}]
|
||||
>> x = 1;
|
||||
>> x + 10
|
||||
@ -465,7 +466,7 @@ Error using +
|
||||
Matrix dimensions must agree.
|
||||
\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
|
||||
following operators for element-wise operations on vectors
|
||||
\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
|
||||
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
|
||||
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
|
||||
number. The linear index thus ranges from 1 to
|
||||
\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
|
||||
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
|
||||
expression is True only if both statements are True. The logical OR,
|
||||
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,
|
||||
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
|
||||
the statements but not both are True. There is no operator for XOR in
|
||||
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
|
||||
\matlab{} it is realized via the function \code[xor()]{xor(A, B)}.
|
||||
|
||||
\begin{table}[tp]
|
||||
@ -860,7 +861,7 @@ the statements but not both are True. There is no operator for XOR in
|
||||
\end{table}
|
||||
|
||||
|
||||
Tables~\ref{logicalrelationaloperators} show the logical and
|
||||
Table~\ref{logicalrelationaloperators} shows the logical and
|
||||
relational operators available in \matlab{}. The additional
|
||||
\code[Operator!logical!and2@\&\&]{\&\&} and
|
||||
\code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so
|
||||
@ -1360,11 +1361,11 @@ end
|
||||
|
||||
\begin{exercise}{simplerandomwalk.m}{}
|
||||
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}
|
||||
\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 Implement the random walk. With each step decide randomly whether the position is changed
|
||||
by $+1$ or $-1$. The walk should have 1000 steps.
|
||||
\item Extend the program to do 10 random walks with 1000 steps each.
|
||||
\item Store all positions.
|
||||
\item Create a figure in which you plot the position as a function
|
||||
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}
|
||||
(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
|
||||
\emph{scripts} to store the solutions of the exercises. Any variable
|
||||
that was created appeared in the \codeterm{workspace} and existed even
|
||||
|
Reference in New Issue
Block a user