This commit is contained in:
2020-12-20 13:52:08 +01:00
137 changed files with 2213 additions and 2412 deletions

View File

@@ -1,4 +1,5 @@
a = [2 4 6 8 10]; % row vector with five elements
s = size(a) % store the return value of size() in a new variable
s(2) % get the second element of s, i.e. the length along the 2nd dimension
size(a,2) % the shortcut
a = [2 4 6 8 10]; % row vector with five elements
s = size(a) % store the return value of size() in a new variable
s(2) % get the second element of s,
% i.e. the length along the 2nd dimension
size(a, 2) % the shortcut

View File

@@ -1,3 +1,3 @@
s = 1 10
ans = 10
ans = 10
s = 1 5
ans = 5
ans = 5

View File

@@ -0,0 +1,11 @@
TEXFILES= \
boolean_logical_indexing.tex \
control_flow.tex \
matrices.tex \
scripts_functions.tex \
structs_cells.tex \
variables_types.tex \
vectors_matrices.tex \
vectors.tex
include ../../exercises.mk

View File

@@ -1,39 +1,17 @@
\documentclass[12pt, a4paper, pdftex]{exam}
\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}
\newcommand{\exercisetopic}{Logical indexing}
\newcommand{\exercisenum}{4}
\newcommand{\exercisedate}{17. November, 2020}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot} \header{{\bfseries\large Exercise 4
}}{{\bfseries\large Boolean expressions \& logical indexing}}{{\bfseries\large 17. November, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de} \runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\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]
Abteilung Neuroethologie \hfill --- \hfill Institut f\"ur Neurobiologie \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. Your solution

View File

@@ -1,40 +1,17 @@
\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}
\newcommand{\exercisetopic}{Control flow}
\newcommand{\exercisenum}{5}
\newcommand{\exercisedate}{24. November, 2020}
%%%%% 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 24. November, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\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}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. Your solution

View File

@@ -1,40 +1,17 @@
\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}
\newcommand{\exercisetopic}{Matrices}
\newcommand{\exercisenum}{3}
\newcommand{\exercisedate}{22. October, 2019}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large Exercise 3}}{{\bfseries\large Matrices}}{{\bfseries\large 10. Oktober, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\newcommand{\code}[1]{\texttt{#1}}
\renewcommand{\solutiontitle}{\noindent\textbf{Solutions:}\par\noindent}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Introduction to Scientific Computing}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex]
Neuroethology \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. Your solution

View File

@@ -1,62 +1,17 @@
\documentclass[12pt,a4paper,pdftex]{exam}
%\documentclass[answers,12pt,a4paper,pdftex]{exam}
\usepackage[german]{babel}
\usepackage{natbib}
\usepackage{xcolor}
\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}
\newcommand{\exercisetopic}{Scripts and Functions}
\newcommand{\exercisenum}{6}
\newcommand{\exercisedate}{1. December, 2020}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large Exercise 6}}{{\bfseries\large Scripts and functions}}{{\bfseries\large 01. December, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74 588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}
\lstset{
language=Matlab,
basicstyle=\ttfamily\footnotesize,
numbers=left,
numberstyle=\tiny,
title=\lstname,
showstringspaces=false,
commentstyle=\itshape\color{darkgray},
breaklines=true,
breakautoindent=true,
columns=flexible,
frame=single,
xleftmargin=1em,
xrightmargin=1em,
aboveskip=10pt
}
\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]
Neuroethology \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. In contrast

View File

@@ -1,40 +1,17 @@
\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}
\newcommand{\exercisetopic}{Structs and Cell Arrays}
\newcommand{\exercisenum}{6}
\newcommand{\exercisedate}{16. October, 2015}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large \"Ubung 6}}{{\bfseries\large Strukturen und Cell Arrays}}{{\bfseries\large 16. Oktober, 2015}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\newcommand{\code}[1]{\texttt{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Einf\"uhrung in die wissenschaftliche Datenverarbeitung}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex]
Abteilung Neuroethologie \hfill --- \hfill Institut f\"ur Neurobiologie \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
Die folgenden Aufgaben dienen der Wiederholung, \"Ubung und
Selbstkontrolle und sollten eigenst\"andig bearbeitet und gel\"ost

View File

@@ -1,41 +1,17 @@
\documentclass[12pt,a4paper,pdftex]{exam}
\usepackage[english]{babel}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage[small]{caption}
\usepackage{sidecap}
\usepackage{pslatex}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{lipsum}
\setlength{\marginparwidth}{2cm}
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}
\newcommand{\exercisetopic}{Variables und Datatypes}
\newcommand{\exercisenum}{1}
\newcommand{\exercisedate}{3. November, 2020}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large Exercise 1}}{{\bfseries\large Variables und Datatypes}}{{\bfseries\large 03. November, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\newcommand{\code}[1]{\texttt{#1}}
\renewcommand{\solutiontitle}{\noindent\textbf{Solutions:}\par\noindent}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Introduction to Scientific Computing}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex]
Neuroethology \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. Your solution

View File

@@ -1,40 +1,17 @@
\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}
\newcommand{\exercisetopic}{Vectors}
\newcommand{\exercisenum}{2}
\newcommand{\exercisedate}{3. November, 2020}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large Exercise 2}}{{\bfseries\large Vectors}}{{\bfseries\large 03. November, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\newcommand{\code}[1]{\texttt{#1}}
\renewcommand{\solutiontitle}{\noindent\textbf{Solutions:}\par\noindent}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Introduction to Scientific Computing}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex]
Neuroethology \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the
lecture. You should try to solve them on your own. Your solution

View File

@@ -1,40 +1,17 @@
\documentclass[12pt,a4paper,pdftex, answers]{exam}
\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}
\newcommand{\exercisetopic}{Vectors}
\newcommand{\exercisenum}{2}
\newcommand{\exercisedate}{3. November, 2020}
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\header{{\bfseries\large Exercise 2}}{{\bfseries\large Vectors}}{{\bfseries\large 03. November, 2020}}
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
jan.grewe@uni-tuebingen.de}
\runningfooter{}{\thepage}{}
\input{../../exercisesheader}
\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}
\firstpagefooter{Dr. Jan Grewe}{}{jan.grewe@uni-tuebingen.de}
\newcommand{\code}[1]{\texttt{#1}}
\renewcommand{\solutiontitle}{\noindent\textbf{Solutions:}\par\noindent}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\vspace*{-6.5ex}
\begin{center}
\textbf{\Large Introduction to Scientific Computing}\\[1ex]
{\large Jan Grewe, Jan Benda}\\[-3ex]
Neuroethology \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\
\end{center}
\input{../../exercisestitle}
The exercises are meant for self-monitoring and revision of the lecture
topic. You should try to solve them on your own. Your solution should

View File

@@ -60,7 +60,8 @@ variable.
In \matlab{} variables can be created at any time on the command line
or any place in a script or function. Listing~\ref{varListing1} shows
three different ways of creating a variable:
\begin{lstlisting}[label=varListing1, caption={Creating variables.}]
\begin{pagelisting}[label=varListing1, caption={Creating variables.}]
>> x = 38
x =
38
@@ -72,7 +73,7 @@ y =
>> z = 'A'
z =
A
\end{lstlisting}
\end{pagelisting}
Line 1 can be read like: ``Create a variable with the name \varcode{x}
and assign the value 38''. The equality sign is the so called
@@ -93,8 +94,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.}]
\begin{pagelisting}[label=varListing2, caption={Requesting information about defined variables and their types.}]
>>class(x)
ans =
double
@@ -110,7 +110,7 @@ x y z
x 1x1 8 double
y 0x0 0 double
z 1x1 2 char
\end{lstlisting}
\end{pagelisting}
\begin{important}[Naming conventions]
There are a few rules regarding variable names. \matlab{} is
@@ -120,7 +120,6 @@ 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 \entermde[Operator!arithmetic]{Operator!arithmetischer}{arithmetic operators}
@@ -131,7 +130,7 @@ such as \code[Operator!arithmetic!1add@+]{+},
\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3}
shows their use.
\begin{lstlisting}[label=varListing3, caption={Working with variables.}]
\begin{pagelisting}[label=varListing3, caption={Working with variables.}]
>> x = 1;
>> x + 10
ans =
@@ -149,13 +148,12 @@ ans =
z =
3
>> z = z * 5;
>> z
>> z = z * 5
z =
15
>> clear z % deleting a variable
\end{lstlisting}
\end{pagelisting}
Note: in lines 2 and 10 the variables have been used without changing
their values. Whenever the value of a variable should change, the
@@ -272,8 +270,8 @@ step-sizes unequal to 1. Line 5 can be read like: ``Create a variable
\varcode{b} and assign the values from 0 to 9 in increasing steps of
1.''. Line 9 reads: ``Create a variable \varcode{c} and assign the
values from 0 to 10 in steps of 2''.
\pagebreak
\begin{lstlisting}[label=generatevectorslisting, caption={Creating simple row-vectors.}]
\begin{pagelisting}[label=generatevectorslisting, caption={Creating simple row-vectors.}]
>> a = [0 1 2 3 4 5 6 7 8 9] % Creating a row-vector
a =
0 1 2 3 4 5 6 7 8 9
@@ -285,7 +283,7 @@ b =
>> c = (0:2:10)
c =
0 2 4 6 8 10
\end{lstlisting}
\end{pagelisting}
The length of a vector, that is the number of elements, can be
requested using the \code{length()} or \code{numel()}
@@ -293,14 +291,14 @@ functions. \code{size()} provides the same information in a slightly,
yet more powerful way (listing~\ref{vectorsizeslisting}). The above
used vector \varcode{a} has the following size:
\begin{lstlisting}[label=vectorsizeslisting, caption={Size of a vector.}]
\begin{pagelisting}[label=vectorsizeslisting, caption={Size of a vector.}]
>> length(a)
ans =
10
>> size(a)
ans =
1 10
\end{lstlisting}
\end{pagelisting}
The answer provided by the \code{size()} function demonstrates that
vectors are nothing else but 2-dimensional matrices in which one
@@ -311,7 +309,7 @@ create a column-vector and how the \code[Operator!Matrix!']{'} ---
operator is used to transpose the column-vector into a row-vector
(lines 14 and following).
\begin{lstlisting}[label=columnvectorlisting, caption={Column-vectors.}]
\begin{pagelisting}[label=columnvectorlisting, caption={Column-vectors.}]
>> b = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] % Creating a column-vector
b =
1
@@ -334,7 +332,7 @@ b =
>> size(b)
ans =
1 10
\end{lstlisting}
\end{pagelisting}
\subsubsection{Accessing elements of a vector}
@@ -355,7 +353,7 @@ number of elements irrespective of the type of vector.
Elements of a vector are accessed via their index. This process is
called \entermde{Indizierung}{indexing}.
In \matlab{} the first element has the index one.
In \matlab{} the first element in a vector has the index one.
The last element's index equals the length of the vector.
\end{important}
@@ -366,25 +364,25 @@ individual values by providing a single index or use the
\code[Operator!Matrix!:]{:} operator to access multiple values with a
single command (see also the info box below \ref{important:colon_operator}).
\begin{lstlisting}[label=vectorelementslisting, caption={Access to individual elements of a vector.}]
>> a = (11:20)
\begin{pagelisting}[label=vectorelementslisting, caption={Access to individual elements of a vector.}]
>> a = (11:20) % generate a vector
a =
11 12 13 14 15 16 17 18 19 20
>> a(1) % the 1. element
>> a(1) % the 1. element
ans = 11
>> a(5) % the 5. element
>> a(5) % the 5. element
ans = 15
>> a(end) % the last element
>> a(end) % the last element
ans = 20
\end{lstlisting}
\end{pagelisting}
\begin{lstlisting}[caption={Access to multiple elements.}, label=vectorrangelisting]
\begin{pagelisting}[caption={Access to multiple elements.}, label=vectorrangelisting]
>> a([1 3 5]) % 1., 3. and 5. element
ans =
11 13 15
>> a(2:4) % all elements with the indices 2 to 4
>> a(2:4) % elements at indices 2 to 4
ans =
12 13 14
@@ -395,7 +393,7 @@ ans =
>> a(:) % all elements as row-vector
ans =
11 12 13 14 15 16 17 18 19 20
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{vectorsize.m}{vectorsize.out}
Create a row-vector \varcode{a} with 5 elements. The return value of
@@ -425,7 +423,7 @@ how vectors and scalars can be combined with the operators \code[Operator!arithm
\code[Operator!arithmetic!4div@/]{/}
\code[Operator!arithmetic!5powe@.\^{}]{.\^}.
\begin{lstlisting}[caption={Calculating with vectors and scalars.},label=vectorscalarlisting]
\begin{pagelisting}[caption={Calculations with vectors and scalars.},label=vectorscalarlisting]
>> a = (0:2:8)
a =
0 2 4 6 8
@@ -449,7 +447,7 @@ ans =
>> a .^ 2 % exponentiation
ans =
0 4 16 36 64
\end{lstlisting}
\end{pagelisting}
When doing calculations with scalars and vectors the same mathematical
operation is done to each element of the vector. In case of, e.g. an
@@ -462,7 +460,7 @@ element-wise operations of two vectors, e.g. each element of vector
layout (row- or column vectors). Addition and subtraction are always
element-wise (listing~\ref{vectoradditionlisting}).
\begin{lstlisting}[caption={Element-wise addition and subtraction of two vectors.},label=vectoradditionlisting]
\begin{pagelisting}[caption={Element-wise addition and subtraction of two vectors.},label=vectoradditionlisting]
>> a = [4 9 12];
>> b = [4 3 2];
>> a + b % addition
@@ -481,7 +479,7 @@ Matrix dimensions must agree.
>> a + d % both vectors must have the same layout!
Error using +
Matrix dimensions must agree.
\end{lstlisting}
\end{pagelisting}
Element-wise multiplication, division, or raising a vector to a given power requires a
different operator with a preceding '.'. \matlab{} defines the
@@ -491,7 +489,7 @@ following operators for element-wise operations on vectors
\code[Operator!arithmetic!5powe@.\^{}]{.\^{}}
(listing~\ref{vectorelemmultiplicationlisting}).
\begin{lstlisting}[caption={Element-wise multiplication, division and
\begin{pagelisting}[caption={Element-wise multiplication, division and
exponentiation of two vectors.},label=vectorelemmultiplicationlisting]
>> a .* b % element-wise multiplication
ans =
@@ -511,7 +509,7 @@ Matrix dimensions must agree.
>> a .* d % Both vectors must have the same layout!
Error using .*
Matrix dimensions must agree.
\end{lstlisting}
\end{pagelisting}
The simple operators \code[Operator!arithmetic!3mul@*]{*},
\code[Operator!arithmetic!4div@/]{/} and
@@ -521,7 +519,7 @@ matrix-operations known from linear algebra (Box~
of a row-vectors $\vec a$ with a column-vector $\vec b$ the
scalar-poduct (or dot-product) $\sum_i = a_i b_i$.
\begin{lstlisting}[caption={Multiplication of vectors.},label=vectormultiplicationlisting]
\begin{pagelisting}[caption={Multiplication of vectors.},label=vectormultiplicationlisting]
>> a * b % multiplication of two vectors
Error using *
Inner matrix dimensions must agree.
@@ -538,14 +536,13 @@ ans =
16 12 8
36 27 18
48 36 24
\end{lstlisting}
\pagebreak[4]
\end{pagelisting}
To remove elements from a vector an empty value
(\code[Operator!Matrix!{[]}]{[]}) is assigned to the respective
elements:
\begin{lstlisting}[label=vectoreraselisting, caption={Deleting elements of a vector.}]
\begin{pagelisting}[label=vectoreraselisting, caption={Deleting elements of a vector.}]
>> a = (0:2:8);
>> length(a)
ans = 5
@@ -558,7 +555,7 @@ a = 4 8
>> length(a)
ans = 2
\end{lstlisting}
\end{pagelisting}
In addition to deleting of vector elements one also add new elements
or concatenate two vectors. When performing a concatenation the two
@@ -720,7 +717,7 @@ remapping, but can be really helpful
rows in each column and so on.}\label{matrixlinearindexingfig}
\end{figure}
\begin{lstlisting}[label=matrixLinearIndexing, caption={Lineares indexing in matrices.}]
\begin{pagelisting}[label=matrixLinearIndexing, caption={Lineares indexing in matrices.}]
>> x = randi(100, [3, 4, 5]); % 3-D matrix filled with random numbers
>> size(x)
ans =
@@ -737,17 +734,17 @@ ans =
>> min(x(:)) % or even simpler
ans =
4
\end{lstlisting}
\end{pagelisting}
\matlab{} defines functions that convert subscript indices to linear indices and back (\code{sub2ind()} and \code{ind2sub()}).
\begin{ibox}[tp]{\label{matrixmultiplication} The matrix--multiplication.}
\begin{ibox}[t]{\label{matrixmultiplication} The matrix--multiplication.}
The matrix--multiplication from linear algebra is \textbf{not} an
element--wise multiplication of each element in a matrix \varcode{A}
and the respective element of matrix \varcode{B}. It is something
completely different. Confusing element--wise and
matrix--multiplication is one of the most common mistakes in
\matlab{}. \linebreak
\matlab{}.
The matrix--multiplication of two 2-D matrices is only possible if
the number of columns in the first matrix agrees with the number of
@@ -797,8 +794,7 @@ box~\ref{matrixmultiplication}). To do a matrix-multiplication the
inner dimensions of the matrices must agree
(box~\ref{matrixmultiplication}).
\pagebreak[4]
\begin{lstlisting}[label=matrixOperations, caption={Two kinds of multiplications of matrices.}]
\begin{pagelisting}[label=matrixOperations, caption={Two kinds of multiplications of matrices.}]
>> A = randi(5, [2, 3]) % 2-D matrix
A =
1 5 3
@@ -824,7 +820,7 @@ ans =
10 15 20
24 23 35
16 17 25
\end{lstlisting}
\end{pagelisting}
\section{Boolean expressions}
@@ -856,7 +852,7 @@ synonymous for the logical values 1 and
0. Listing~\ref{logicaldatatype} exemplifies the use of the logical
data type.
\begin{lstlisting}[caption={The logical data type. Please note that the actual \matlab{} output looks a little different.}, label=logicaldatatype]
\begin{pagelisting}[caption={The logical data type. Please note that the actual \matlab{} output looks a little different.}, label=logicaldatatype]
>> true
ans = 1
>> false
@@ -873,7 +869,7 @@ ans = 0
ans = 1 1 1 1
>> logical([1 2 3 4 0 0 10])
ans = 1 1 1 1 0 0 1
\end{lstlisting}
\end{pagelisting}
\varcode{true} and \varcode{false} are reserved keywords that evaluate
to the logical values 1 and 0, respectively. If you want to create a
@@ -886,7 +882,7 @@ code of each character in ``test'' is non-zero value, thus, the result
of casting it to logical is a vector of logicals. A similar thing
happens upon casting a vector (or matrix) of numbers to logical. Each
value is converted to logical and the result is true for all non-zero
values (line 21).
values (line 15).
Knowing how to represent true and false values in \matlab{} using the
logical data type allows us to take a step towards more complex
@@ -924,8 +920,8 @@ stored in variable \varcode{b}?''.
The result of such questions is then given as a logical
value. Listing~\ref{relationaloperationslisting} shows examples using relational operators.
\pagebreak
\begin{lstlisting}[caption={Relational Boolean expressions.}, label=relationaloperationslisting]
\begin{pagelisting}[caption={Relational Boolean expressions.}, label=relationaloperationslisting]
>> true == logical(1)
ans = 1
>> false ~= logical(1)
@@ -943,7 +939,7 @@ ans = 0 0 1 0 0
ans = 0 1 0 0 1
>> [2 0 0 5 0] >= [1 0 3 2 0]
ans = 1 1 0 1 1
\end{lstlisting}
\end{pagelisting}
Testing the relations between numbers and scalar variables is straight
forward. When comparing vectors, the relational operator will be
@@ -1040,7 +1036,7 @@ for implementing such
expressions. Listing~\ref{logicaloperatorlisting} shows a few
examples and respective illustrations are shown in figure~\ref{logicaloperationsfig}.
\begin{lstlisting}[caption={Boolean expressions.}, label=logicaloperatorlisting]
\begin{pagelisting}[caption={Boolean expressions.}, label=logicaloperatorlisting]
>> x = rand(1) % create a single random number in the range [0, 1]
x = 0.3452
>> x > 0.25 & x < 0.75
@@ -1051,7 +1047,7 @@ x = 0.4920, 0.9106, 0.7218, 0.8749, 0.1574, 0.0201, 0.9107, 0.8357, 0.0357, 0.47
ans = 1 0 1 0 0 0 0 0 0 1
>> x < 0.25 | x > 0.75
ans = 0 1 0 1 1 1 1 1 1 0
\end{lstlisting}
\end{pagelisting}
\begin{figure}[ht]
\includegraphics[]{logical_operations}
@@ -1066,7 +1062,6 @@ ans = 0 1 0 1 1 1 1 1 1 0
data.}\label{logicaloperationsfig}
\end{figure}
\pagebreak
\begin{important}[Assignment and equality operators]
The assignment operator \code[Operator!Assignment!=]{=} and the
relational equality operator \code[Operator!relational!==]{==} are
@@ -1095,7 +1090,7 @@ elements of \varcode{x} where the Boolean expression \varcode{x < 0}
evaluates to true and store the result in the variable
\varcode{x\_smaller\_zero}''.
\begin{lstlisting}[caption={Logical indexing.}, label=logicalindexing1]
\begin{pagelisting}[caption={Logical indexing.}, label=logicalindexing1]
>> x = randn(1, 6) % a vector with 6 random numbers
x =
-1.4023 -1.4224 0.4882 -0.1774 -0.1961 1.4193
@@ -1112,7 +1107,7 @@ elements_smaller_zero =
>> elements_smaller_zero = x(x < 0)
elements_smaller_zero =
-1.4023 -1.4224 -0.1774 -0.1961
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{logicalVector.m}{logicalVector.out}
Create a vector \varcode{x} containing the values 0--10.
@@ -1121,8 +1116,7 @@ elements_smaller_zero =
\item Display the content of \varcode{y} in the command window.
\item What is the data type of \varcode{y}?
\item Return only those elements \varcode{x} that are less than 5.
\end{enumerate}
\pagebreak[4]
\end{enumerate}\vspace{-1ex}
\end{exercise}
\begin{figure}[t]
@@ -1144,7 +1138,6 @@ segment of data of a certain time span (the stimulus was on,
\begin{exercise}{logicalIndexingTime.m}{}
Assume that measurements have been made for a certain time. Usually
measured values and the time are stored in two vectors.
\begin{itemize}
\item Create a vector that represents the recording time \varcode{t
= 0:0.001:10;}.
@@ -1152,9 +1145,9 @@ segment of data of a certain time span (the stimulus was on,
that has the same length as \varcode{t}. The values stored in
\varcode{x} represent the measured data at the times in
\varcode{t}.
\item Use logical indexing to select those values that have been
recorded in the time span from 5--6\,s.
\end{itemize}
\item Use logical indexing to select values that have been
recorded in the time span 5--6\,s.
\end{itemize}\vspace{-1ex}
\end{exercise}
\begin{ibox}[!ht]{\label{advancedtypesbox}Advanced data types}
@@ -1276,7 +1269,7 @@ As the name already suggests loops are used to execute the same parts
of the code repeatedly. In one of the earlier exercises the factorial of
five has been calculated as depicted in listing~\ref{facultylisting}.
\begin{lstlisting}[caption={Calculation of the factorial of 5 in five steps}, label=facultylisting]
\begin{pagelisting}[caption={Calculation of the factorial of 5 in five steps}, label=facultylisting]
>> x = 1;
>> x = x * 2;
>> x = x * 3;
@@ -1285,7 +1278,7 @@ five has been calculated as depicted in listing~\ref{facultylisting}.
>> x
x =
120
\end{lstlisting}
\end{pagelisting}
This kind of program solves the taks but it is rather repetitive. The only
thing that changes is the increasing factor. The repetition of such
@@ -1325,7 +1318,7 @@ a certain purpose. The \varcode{for}-loop is closed with the keyword
\code{end}. Listing~\ref{looplisting} shows a simple version of such a
for-loop.
\begin{lstlisting}[caption={Example of a \varcode{for}-loop.}, label=looplisting]
\begin{pagelisting}[caption={Example of a \varcode{for}-loop.}, label=looplisting]
>> for x = 1:3 % head
disp(x) % body
end
@@ -1334,7 +1327,7 @@ for-loop.
1
2
3
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{factorialLoop.m}{factorialLoop.out}
@@ -1354,11 +1347,11 @@ keyword \code{while} that is followed by a Boolean expression. If this
can be evaluated to true, the code in the body is executed. The loop
is closed with an \code{end}.
\begin{lstlisting}[caption={Basic structure of a \varcode{while} loop.}, label=whileloop]
while x == true % head with a Boolean expression
\begin{pagelisting}[caption={Basic structure of a \varcode{while} loop.}, label=whileloop]
while x == true % head with a Boolean expression
% execute this code if the expression yields true
end
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{factorialWhileLoop.m}{}
Implement the factorial of a number \varcode{n} using a \varcode{while}-loop.
@@ -1413,7 +1406,7 @@ expression to provide a default case. The last body of the
\varcode{if} - \varcode{elseif} - \varcode{else} statement has to be
finished with the \code{end} (listing~\ref{ifelselisting}).
\begin{lstlisting}[label=ifelselisting, caption={Structure of an \varcode{if} statement.}]
\begin{pagelisting}[label=ifelselisting, caption={Structure of an \varcode{if} statement.}]
if x < y % head
% body I, executed only if x < y
elseif x > y
@@ -1421,7 +1414,7 @@ elseif x > y
else
% body III, executed only if the previous conditions did not match
end
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{ifelse.m}{}
Draw a random number and check with an appropriate \varcode{if}
@@ -1449,7 +1442,7 @@ that were not explicitly stated above (listing~\ref{switchlisting}).
As usual the \code{switch} statement needs to be closed with an
\code{end}.
\begin{lstlisting}[label=switchlisting, caption={Structure of a \varcode{switch} statement.}]
\begin{pagelisting}[label=switchlisting, caption={Structure of a \varcode{switch} statement.}]
mynumber = input('Enter a number:');
switch mynumber
case -1
@@ -1459,7 +1452,7 @@ switch mynumber
otherwise
disp('something else');
end
\end{lstlisting}
\end{pagelisting}
\subsubsection{Comparison \varcode{if} and \varcode{switch} -- statements}
@@ -1483,7 +1476,7 @@ skip the execution of the body under certain circumstances, one can
use the keywords \code{break} and \code{continue}
(listings~\ref{continuelisting} and \ref{continuelisting}).
\begin{lstlisting}[caption={Stop the execution of a loop using \varcode{break}.}, label=breaklisting]
\begin{pagelisting}[caption={Stop the execution of a loop using \varcode{break}.}, label=breaklisting]
>> x = 1;
while true
if (x > 3)
@@ -1496,9 +1489,9 @@ use the keywords \code{break} and \code{continue}
1
2
3
\end{lstlisting}
\end{pagelisting}
\begin{lstlisting}[caption={Skipping iterations using \varcode{continue}.}, label=continuelisting]
\begin{pagelisting}[caption={Skipping iterations using \varcode{continue}.}, label=continuelisting]
for x = 1:5
if(x > 2 & x < 5)
continue;
@@ -1509,7 +1502,7 @@ end
1
2
5
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{logicalIndexingBenchmark.m}{logicalIndexingBenchmark.out}
Above we claimed that logical indexing is faster and much more
@@ -1581,11 +1574,11 @@ has one \entermde{Argument}{argument} $x$ that is transformed into the
function's output value $y$. In \matlab{} the syntax of a function
declaration is very similar (listing~\ref{functiondefinitionlisting}).
\begin{lstlisting}[caption={Declaration of a function in \matlab{}}, label=functiondefinitionlisting]
\begin{pagelisting}[caption={Declaration of a function in \matlab{}}, label=functiondefinitionlisting]
function [y] = functionName(arg_1, arg_2)
% ^ ^ ^
% return value argument_1, argument_2
\end{lstlisting}
\end{pagelisting}
The keyword \code{function} is followed by the return value(s) (it can
be a list \varcode{[]} of values), the function name and the
@@ -1618,7 +1611,7 @@ The following listing (\ref{badsinewavelisting}) shows a function that
calculates and displays a bunch of sine waves with different amplitudes.
\begin{lstlisting}[caption={Bad example of a function that displays a series of sine waves.},label=badsinewavelisting]
\begin{pagelisting}[caption={Bad example of a function that displays a series of sine waves.},label=badsinewavelisting]
function myFirstFunction() % function head
t = (0:0.01:2);
frequency = 1.0;
@@ -1629,7 +1622,7 @@ function myFirstFunction() % function head
hold on;
end
end
\end{lstlisting}
\end{pagelisting}
\varcode{myFirstFunction} (listing~\ref{badsinewavelisting}) is a
prime-example of a bad function. There are several issues with it's
@@ -1690,7 +1683,7 @@ define (i) how to name the function, (ii) which information it needs
Having defined this we can start coding
(listing~\ref{sinefunctionlisting}).
\begin{lstlisting}[caption={Function that calculates a sine wave.}, label=sinefunctionlisting]
\begin{pagelisting}[caption={Function that calculates a sine wave.}, label=sinefunctionlisting]
function [time, sine] = sinewave(frequency, amplitude, t_max, t_step)
% Calculate a sinewave of a given frequency, amplitude,
% duration and temporal resolution.
@@ -1708,7 +1701,7 @@ function [time, sine] = sinewave(frequency, amplitude, t_max, t_step)
time = (0:t_step:t_max);
sine = sin(frequency .* time .* 2 .* pi) .* amplitude;
end
\end{lstlisting}
\end{pagelisting}
\paragraph{II. Plotting a single sine wave}
@@ -1735,7 +1728,7 @@ specification of the function:
With this specification we can start to implement the function
(listing~\ref{sineplotfunctionlisting}).
\begin{lstlisting}[caption={Function for the graphical display of data.}, label=sineplotfunctionlisting]
\begin{pagelisting}[caption={Function for the graphical display of data.}, label=sineplotfunctionlisting]
function plotFunction(x_data, y_data, name)
% Plots x-data against y-data and sets the display name.
%
@@ -1747,7 +1740,7 @@ function plotFunction(x_data, y_data, name)
% name : the displayname
plot(x_data, y_data, 'displayname', name)
end
\end{lstlisting}
\end{pagelisting}
\begin{ibox}[!ht]{\label{box:sprintf}\enterm[Format strings]{Formatted strings} with \mcode{sprintf}.}
We can use \varcode{disp} to dispaly the content of a variable on the command line. But how can we embed variable contents nicely into a text and store it in a variable or show it on the command line? \varcode{sprintf} is the standard function to achieve this. We introduce only a fraction of its real might here. For a more complete description visit the \matlab{} help or e.g. \url{https://en.wikipedia.org/wiki/Printf_format_string}.
@@ -1820,11 +1813,11 @@ for i = 1:length(amplitudes)
end
hold off
legend('show')
\end{lstlisting}
\end{pagelisting}
\pagebreak[4]
\begin{exercise}{plotMultipleSinewaves.m}{}
Extend the program to plot also a range of frequencies.
\pagebreak[4]
\end{exercise}