fixed page breaking of code and exercises

This commit is contained in:
Jan Benda 2020-12-11 23:22:40 +01:00
parent 0380b7a96f
commit 0e30a01a45
13 changed files with 360 additions and 536 deletions

View File

@ -204,7 +204,7 @@ random walk\footnote{A random walk is a simple simulation of Brownian
(listing \ref{chaoticcode}) then in cleaner way (listing
\ref{cleancode})
\begin{lstlisting}[label=chaoticcode, caption={Chaotic implementation of the random-walk.}]
\begin{pagelisting}[label=chaoticcode, caption={Chaotic implementation of the random-walk.}]
num_runs = 10; max_steps = 1000;
positions = zeros(max_steps, num_runs);
@ -218,17 +218,14 @@ x = randn(1);
if x<0
positions(step, run)= positions(step-1, run)+1;
elseif x>0
positions(step,run)=positions(step-1,run)-1;
end
end
end
\end{lstlisting}
\end{pagelisting}
\pagebreak[4]
\begin{lstlisting}[label=cleancode, caption={Clean implementation of the random-walk.}]
\begin{pagelisting}[label=cleancode, caption={Clean implementation of the random-walk.}]
num_runs = 10;
max_steps = 1000;
positions = zeros(max_steps, num_runs);
@ -243,7 +240,7 @@ for run = 1:num_runs
end
end
end
\end{lstlisting}
\end{pagelisting}
\section{Using comments}
@ -275,7 +272,6 @@ avoided:\\ \varcode{ x = x + 2; \% add two to x}\\
make it even clearer.}{Steve McConnell}
\end{important}
\pagebreak[4]
\section{Documenting functions}
All pre-defined \matlab{} functions begin with a comment block that
describes the purpose of the function, the required and optional
@ -335,8 +331,7 @@ used within the context of another function \matlab{} allows to define
within the same file. Listing \ref{localfunctions} shows an example of
a local function definition.
\pagebreak[3]
\lstinputlisting[label=localfunctions, caption={Example for local
\pageinputlisting[label=localfunctions, caption={Example for local
functions.}]{calculateSines.m}
\emph{Local function} live in the same \entermde{m-File}{m-file} as

View File

@ -59,14 +59,14 @@ every opening parenthesis must be matched by a closing one or every
respective error messages are clear and the editor will point out and
highlight most syntax errors.
\begin{lstlisting}[label=syntaxerror, caption={Unbalanced parenthesis error.}]
\begin{pagelisting}[label=syntaxerror, caption={Unbalanced parenthesis error.}]
>> mean(random_numbers
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Did you mean:
>> mean(random_numbers)
\end{lstlisting}
\end{pagelisting}
\subsection{Indexing error}\label{index_error}
Second on the list of common errors are the
@ -125,7 +125,7 @@ dimensions. The command in line 7 works due to the fact, that matlab
automatically extends the matrix, if you assign values to a range
outside its bounds.
\begin{lstlisting}[label=assignmenterror, caption={Assignment errors.}]
\begin{pagelisting}[label=assignmenterror, caption={Assignment errors.}]
>> a = zeros(1, 100);
>> b = 0:10;
@ -136,7 +136,7 @@ outside its bounds.
>> size(a)
ans =
110 1
\end{lstlisting}
\end{pagelisting}
\subsection{Dimension mismatch error}
Similarly, some arithmetic operations are only valid if the variables
@ -154,7 +154,7 @@ in listing\,\ref{dimensionmismatch} does not throw an error but the
result is something else than the expected elementwise multiplication.
% XXX Some arithmetic operations make size constraints, violating them leads to dimension mismatch errors.
\begin{lstlisting}[label=dimensionmismatch, caption={Dimension mismatch errors.}]
\begin{pagelisting}[label=dimensionmismatch, caption={Dimension mismatch errors.}]
>> a = randn(100, 1);
>> b = randn(10, 1);
>> a + b
@ -171,7 +171,7 @@ result is something else than the expected elementwise multiplication.
>> size(c)
ans =
100 10
\end{lstlisting}
\end{pagelisting}
@ -239,7 +239,7 @@ but it requires a deep understanding of the applied functions and also
the task at hand.
% XXX Converting a series of spike times into the firing rate as a function of time. Many tasks can be solved with a single line of code. But is this readable?
\begin{lstlisting}[label=easyvscomplicated, caption={One-liner versus readable code.}]
\begin{pagelisting}[label=easyvscomplicated, caption={One-liner versus readable code.}]
% the one-liner
rate = conv(full(sparse(1, round(spike_times/dt), 1, 1, length(time))), kernel, 'same');
@ -248,7 +248,7 @@ rate = zeros(size(time));
spike_indices = round(spike_times/dt);
rate(spike_indices) = 1;
rate = conv(rate, kernel, 'same');
\end{lstlisting}
\end{pagelisting}
The preferred way depends on several considerations. (i) How deep is
your personal understanding of the programming language? (ii) What
@ -291,7 +291,7 @@ example given in the \matlab{} help and assume that there is a
function \varcode{rightTriangle()} (listing\,\ref{trianglelisting}).
% XXX Slightly more readable version of the example given in the \matlab{} help system. Note: The variable name for the angles have been capitalized in order to not override the matlab defined functions \code{alpha, beta,} and \code{gamma}.
\begin{lstlisting}[label=trianglelisting, caption={Example function for unit testing.}]
\begin{pagelisting}[label=trianglelisting, caption={Example function for unit testing.}]
function angles = rightTriangle(length_a, length_b)
ALPHA = atand(length_a / length_b);
BETA = atand(length_a / length_b);
@ -300,7 +300,7 @@ function angles = rightTriangle(length_a, length_b)
angles = [ALPHA BETA GAMMA];
end
\end{lstlisting}
\end{pagelisting}
This function expects two input arguments that are the length of the
sides $a$ and $b$ and assumes a right angle between them. From this
@ -337,7 +337,7 @@ The test script for the \varcode{rightTriangle()} function
(listing\,\ref{trianglelisting}) may look like in
listing\,\ref{testscript}.
\begin{lstlisting}[label=testscript, caption={Unit test for the \varcode{rightTriangle()} function stored in an m-file testRightTriangle.m}]
\begin{pagelisting}[label=testscript, caption={Unit test for the \varcode{rightTriangle()} function stored in an m-file testRightTriangle.m}]
tolerance = 1e-10;
% preconditions
@ -373,7 +373,7 @@ angles = rightTriangle(1, 1500);
smallAngle = (pi / 180) * angles(1); % radians
approx = sin(smallAngle);
assert(abs(approx - smallAngle) <= tolerance, 'Problem with small angle approximation')
\end{lstlisting}
\end{pagelisting}
In a test script we can execute any code. The actual test whether or
not the results match our predictions is done using the
@ -390,16 +390,16 @@ executed. We further define a \varcode{tolerance} variable that is
used when comparing double values (Why might the test on equality of
double values be tricky?).
\begin{lstlisting}[label=runtestlisting, caption={Run the test!}]
\begin{pagelisting}[label=runtestlisting, caption={Run the test!}]
result = runtests('testRightTriangle')
\end{lstlisting}
\end{pagelisting}
During the run, \matlab{} will put out error messages onto the command
line and a summary of the test results is then stored within the
\varcode{result} variable. These can be displayed using the function
\code[table()]{table(result)}.
\begin{lstlisting}[label=testresults, caption={The test results.}, basicstyle=\ttfamily\scriptsize]
\begin{pagelisting}[label=testresults, caption={The test results.}, basicstyle=\ttfamily\scriptsize]
table(result)
ans =
4x6 table
@ -411,7 +411,7 @@ _________________________________ ______ ______ ___________ ________ _____
'testR.../Test_IsoscelesTriangles' true false false 0.004893 [1x1 struct]
'testR.../Test_30_60_90Triangle' true false false 0.005057 [1x1 struct]
'testR.../Test_SmallAngleApprox' true false false 0.0049 [1x1 struct]
\end{lstlisting}
\end{pagelisting}
So far so good, all tests pass and our function appears to do what it
is supposed to do. But tests are only as good as the programmer who
@ -479,11 +479,11 @@ stopped in debug mode (listing\,\ref{debuggerlisting}).
\end{figure}
\begin{lstlisting}[label=debuggerlisting, caption={Command line when the program execution was stopped in the debugger.}]
\begin{pagelisting}[label=debuggerlisting, caption={Command line when the program execution was stopped in the debugger.}]
>> simplerandomwalk
6 for run = 1:num_runs
K>>
\end{lstlisting}
\end{pagelisting}
When stopped in the debugger we can view and change the state of the
program at this point, we can also issue commands to try the next

View File

@ -10,7 +10,8 @@ pattern]{design pattern}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Looping over vector elements}
Iterating over vector elements by means of a \mcode{for}-loop is a very commen pattern:
\begin{lstlisting}[caption={\varcode{for}-loop for accessing vector elements by indices}]
\begin{pagelisting}[caption={\varcode{for}-loop for accessing vector elements by indices}]
x = [2:3:20]; % Some vector.
for i=1:length(x) % For loop over the indices of the vector.
i % This is the index (an integer number)
@ -22,12 +23,14 @@ for i=1:length(x) % For loop over the indices of the vector.
% as an argument to a function:
do_something(x(i));
end
\end{lstlisting}
\end{pagelisting}
\noindent
If the result of the computation within the loop are single numbers
that are to be stored in a vector, you should create this vector with
the right size before the loop:
\begin{lstlisting}[caption={\varcode{for}-loop for writing a vector}]
\begin{pagelisting}[caption={\varcode{for}-loop for writing a vector}]
x = [1.2 2.3 2.6 3.1]; % Some vector.
% Create a vector for the results, as long as the number of loops:
y = zeros(length(x),1);
@ -38,13 +41,15 @@ for i=1:length(x)
end
% Now the result vector can be further processed:
mean(y);
\end{lstlisting}
\end{pagelisting}
\noindent
The computation within the loop could also result in a vector of some
length and not just a single number. If the length of this vector
(here 10) is known beforehand, then you should create a matrix of
appropriate size for storing the results:
\begin{lstlisting}[caption={\varcode{for}-loop for writing rows of a matrix}]
\begin{pagelisting}[caption={\varcode{for}-loop for writing rows of a matrix}]
x = [2:3:20]; % Some vector.
% Create space for results -
% as many rows as loops, as many columns as needed:
@ -56,11 +61,13 @@ for i=1:length(x)
end
% Process the results stored in matrix y:
mean(y, 1)
\end{lstlisting}
\end{pagelisting}
\noindent
Another possibility is that the result vectors (here of unknown size)
need to be combined into a single large vector:
\begin{lstlisting}[caption={\varcode{for}-loop for appending vectors}]
\begin{pagelisting}[caption={\varcode{for}-loop for appending vectors}]
x = [2:3:20]; % Some vector.
y = []; % Empty vector for storing the results.
for i=1:length(x)
@ -72,14 +79,15 @@ for i=1:length(x)
end
% Process the results stored in the vector z:
mean(y)
\end{lstlisting}
\end{pagelisting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Scaling and shifting random numbers, zeros, and ones}
Random number generators usually return random numbers of a given mean
and standard deviation. Multiply those numbers by a factor to change their standard deviation and add a number to shift the mean.
\begin{lstlisting}[caption={Scaling and shifting of random numbers}]
\begin{pagelisting}[caption={Scaling and shifting of random numbers}]
% 100 random numbers drawn from a normal distribution
% with mean 0 and standard deviation 1:
x = randn(100, 1);
@ -89,18 +97,20 @@ x = randn(100, 1);
mu = 4.8;
sigma = 2.3;
y = randn(100, 1)*sigma + mu;
\end{lstlisting}
\end{pagelisting}
\noindent
The same principle can be useful for in the context of the functions
\mcode{zeros()} or \mcode{ones()}:
\begin{lstlisting}[caption={Scaling and shifting of \varcode{zeros()} and \varcode{ones()}}]
\begin{pagelisting}[caption={Scaling and shifting of \varcode{zeros()} and \varcode{ones()}}]
x = -1:0.01:2; % Vector of x-values for plotting
plot(x, exp(-x.*x));
% Plot for the same x-values a horizontal line with y=0.8:
plot(x, zeros(size(x))+0.8);
% ... or a line with y=0.5:
plot(x, ones(size(x))*0.5);
\end{lstlisting}
\end{pagelisting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -119,25 +129,26 @@ can plot the values of the $y$ vector against the ones of the $x$
vector.
The following scripts compute and plot the function $f(x)=e^{-x^2}$:
\begin{lstlisting}[caption={Plotting a mathematical function --- very detailed}]
\begin{pagelisting}[caption={Plotting a mathematical function --- very detailed}]
xmin = -1.0;
xmax = 2.0;
dx = 0.01; % Step size
x = xmin:dx:xmax; % Vector with x-values.
y = exp(-x.*x); % No for loop! '.*' for multiplying the vector elements.
plot(x, y);
\end{lstlisting}
\end{pagelisting}
\begin{lstlisting}[caption={Plotting a mathematical function --- shorter}]
\begin{pagelisting}[caption={Plotting a mathematical function --- shorter}]
x = -1:0.01:2;
y = exp(-x.*x);
plot(x, y);
\end{lstlisting}
\end{pagelisting}
\begin{lstlisting}[caption={Plotting a mathematical function --- compact}]
\begin{pagelisting}[caption={Plotting a mathematical function --- compact}]
x = -1:0.01:2;
plot(x, exp(-x.*x));
\end{lstlisting}
\end{pagelisting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -146,28 +157,34 @@ For estimating probabilities or probability densities from histograms
we need to normalize them appropriately.
The \mcode{histogram()} function does this automatically with the appropriate arguments:
\begin{lstlisting}[caption={Probability density with the \varcode{histogram()}-function}]
\begin{pagelisting}[caption={Probability density with the \varcode{histogram()}-function}]
x = randn(100, 1); % Some real-valued data.
histogram(x, 'Normalization', 'pdf');
\end{lstlisting}
\begin{lstlisting}[caption={Probability with the \varcode{histogram()}-function}]
\end{pagelisting}
\begin{pagelisting}[caption={Probability with the \varcode{histogram()}-function}]
x = randi(6, 100, 1); % Some integer-valued data.
histogram(x, 'Normalization', 'probability');
\end{lstlisting}
\end{pagelisting}
\noindent
Alternatively one can normalize the histogram data as returned by the
\code{hist()}-function manually:
\begin{lstlisting}[caption={Probability density with the \varcode{hist()}- and \varcode{bar()}-function}]
\begin{pagelisting}[caption={Probability density with the \varcode{hist()}- and \varcode{bar()}-function}]
x = randn(100, 1); % Some real-valued data.
[h, b] = hist(x); % Compute histogram.
h = h/sum(h)/(b(2)-b(1)); % Normalization to a probability density.
bar(b, h); % Plot the probability density.
\end{lstlisting}
\begin{lstlisting}[caption={Probability with the \varcode{hist()}- and \varcode{bar()}-function}]
\end{pagelisting}
\begin{pagelisting}[caption={Probability with the \varcode{hist()}- and \varcode{bar()}-function}]
x = randi(6, 100, 1); % Some integer-valued data.
[h, b] = hist(x); % Compute histogram.
h = h/sum(h); % Normalize to probability.
bar(b, h); % Plot the probabilities.
\end{lstlisting}
\end{pagelisting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -2,7 +2,7 @@
\title{\textbf{\huge\sffamily\tr{Introduction to\\[1ex] Scientific Computing}%
{Einf\"uhrung in die\\[1ex] wissenschaftliche Datenverarbeitung}}}
\author{{\LARGE Jan Grewe \& Jan Benda}\\[5ex]Abteilung Neuroethologie\\[2ex]%
\author{{\LARGE Jan Grewe \& Jan Benda}\\[5ex]Neuroethology Lab\\[2ex]%
\includegraphics[width=0.3\textwidth]{UT_WBMW_Rot_RGB}\vspace{3ex}}
\date{WS 2020/2021\\\vfill%
@ -77,7 +77,7 @@
\setcounter{totalnumber}{2}
% float placement fractions:
\renewcommand{\textfraction}{0.2}
\renewcommand{\textfraction}{0.1}
\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.0}
\renewcommand{\floatpagefraction}{0.7}
@ -209,6 +209,21 @@
\let\l@lstlisting\l@figure
\makeatother
% \lstinputlisting wrapped in a minipage to avoid page breaks:
\newcommand{\pageinputlisting}[2][]{\vspace{-2ex}\noindent\begin{minipage}[t]{1\linewidth}\lstinputlisting[#1]{#2}\end{minipage}}
%%%%% listing environment: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usage:
%
% \begin{pagelisting}[label=listing1, caption={A script.}]
% >> x = 6
% \end{pagelisting}
%
% This is the lstlisting environment but wrapped in a minipage to avoid page breaks.
\lstnewenvironment{pagelisting}[1][]%
{\vspace{-2ex}\lstset{#1}\noindent\minipage[t]{1\linewidth}}%
{\endminipage}
%%%%% english, german, code and file terms: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{ifthen}
@ -304,7 +319,7 @@
%
\newboolean{showexercisesolutions}
\setboolean{showexercisesolutions}{true}
\newcommand{\exercisesolutions}{here} % 0: here, 1: chapter, 2: end
\newcommand{\exercisesolutions}{chapter} % 0: here, 1: chapter, 2: end
% we need this also as numbers:
\ifthenelse{\equal{\exercisesolutions}{end}}{\newcommand{\exercisesolutionsnum}{2}}{%
\ifthenelse{\equal{\exercisesolutions}{chapter}}{\newcommand{\exercisesolutionsnum}{1}}{%
@ -349,11 +364,12 @@
}{%
\immediate\write\solutions{\unexpanded{\subsection}{Exercise \thechapter.\arabic{exercisef}}}%
\immediate\write\solutions{\unexpanded{\label}{solution\arabic{chapter}-\arabic{exercisef}}}%
\immediate\write\solutions{\unexpanded{\lstinputlisting[belowskip=0ex,aboveskip=0ex,%
nolol=true, title={\textbf{Source code:} \protect\StrSubstitute{#1}{_}{\_}}]}{\codepath#1}}%
\immediate\write\solutions{\unexpanded{\begin{minipage}{1\linewidth}\lstinputlisting[belowskip=0ex,aboveskip=0ex,%
nolol=true, title={\textbf{Source code:} \protect\StrSubstitute{#1}{_}{\_}}]}{\codepath#1}\unexpanded{\end{minipage}}}%
\ifthenelse{\equal{#2}{}}{}%
{\immediate\write\solutions{\unexpanded{\lstinputlisting[language={},%
nolol=true, title={\textbf{Output:}}, belowskip=0ex, aboveskip=1ex]}{\codepath#2}}}%
{\immediate\write\solutions{\unexpanded{\begin{minipage}{1\linewidth}\lstinputlisting[language={},%
nolol=true, title={\textbf{Output:}}, belowskip=0ex, aboveskip=1ex]}{\codepath#2}\unexpanded{\end{minipage}}}}%
\immediate\write\solutions{\unexpanded{\vspace*{\fill}}}%
\immediate\write\solutions{}%
}%
}%
@ -362,14 +378,18 @@
{ \hypersetup{hypertexnames=false}%
\ifthenelse{\equal{\exercisesource}{}}{}%
{ \addtocounter{lstlisting}{-1}%
\lstinputlisting[belowskip=0pt,aboveskip=1ex,nolol=true,%
\par\noindent\begin{minipage}[t]{1\linewidth}%
\lstinputlisting[belowskip=1ex,aboveskip=-1ex,nolol=true,%
title={\textbf{Solution:} \exercisefile}]%
{\exercisesource}%
\end{minipage}%
\ifthenelse{\equal{\exerciseoutput}{}}{}%
{ \addtocounter{lstlisting}{-1}%
\par\noindent\begin{minipage}[t]{1\linewidth}%
\lstinputlisting[language={},title={\textbf{Output:}},%
nolol=true,belowskip=0pt]%
nolol=true,belowskip=0pt,aboveskip=-0.5ex]%
{\exerciseoutput}%
\end{minipage}%
}%
}%
\hypersetup{hypertexnames=true}%
@ -452,12 +472,12 @@
{ \captionsetup{singlelinecheck=off,hypcap=false,labelformat={empty},%
labelfont={large,sf,it,bf},font={large,sf,it,bf}}
\ifthenelse{\equal{#1}{}}%
{ \begin{mdframed}[linecolor=importantline,linewidth=1ex,%
{ \begin{mdframed}[nobreak=true,linecolor=importantline,linewidth=1ex,%
backgroundcolor=importantback,font={\sffamily}]%
\setlength{\parindent}{0pt}%
\setlength{\parskip}{1ex}%
}%
{ \begin{mdframed}[linecolor=importantline,linewidth=1ex,%
{ \begin{mdframed}[nobreak=true,linecolor=importantline,linewidth=1ex,%
backgroundcolor=importantback,font={\sffamily},%
frametitle={\captionof{iboxf}{#1}},frametitleaboveskip=-1ex,%
frametitlebackgroundcolor=importantline]%

View File

@ -3,7 +3,7 @@
\input{../../header}
\lstset{inputpath=../code}
\graphicspath{{images/}}
\graphicspath{{figures/}}
\typein[\pagenumber]{Number of first page}
\typein[\chapternumber]{Chapter number}

View File

@ -148,7 +148,7 @@ or the color. For additional options consult the help.
The following listing shows a simple line plot with axis labeling and a title
\lstinputlisting[caption={A simple plot showing a sinewave.},
\pageinputlisting[caption={A simple plot showing a sinewave.},
label=simpleplotlisting]{simple_plot.m}
@ -162,10 +162,10 @@ chosen, and star marker symbols is used. Finally, the name of the
curve is set to \emph{plot 1} which will be displayed in a legend, if
chosen.
\begin{lstlisting}[label=settinglineprops, caption={Setting line properties when calling \varcode{plot}.}]
\begin{pagelisting}[label=settinglineprops, caption={Setting line properties when calling \varcode{plot}.}]
x = 0:0.1:2*pi; y = sin(x); plot( x, y, 'color', 'r', 'linestyle',
':', 'marker', '*', 'linewidth', 1.5, 'displayname', 'plot 1')
\end{lstlisting}
\end{pagelisting}
\begin{important}[Choosing the right color.]
Choosing the perfect color goes a little bit beyond personal
@ -277,7 +277,7 @@ the last one defines the output format (box\,\ref{graphicsformatbox}).
listing\,\ref{niceplotlisting}.}\label{spikedetectionfig}
\end{figure}
\begin{ibox}[t]{\label{graphicsformatbox}File formats for digital artwork.}
\begin{ibox}[tp]{\label{graphicsformatbox}File formats for digital artwork.}
There are two fundamentally different types of formats for digital artwork:
\begin{enumerate}
\item \enterm[bitmap]{Bitmaps} (\determ{Rastergrafik})
@ -322,7 +322,7 @@ the last one defines the output format (box\,\ref{graphicsformatbox}).
efficient.
\end{ibox}
\lstinputlisting[caption={Script for creating the plot shown in
\pageinputlisting[caption={Script for creating the plot shown in
\figref{spikedetectionfig}.},
label=niceplotlisting]{automatic_plot.m}
@ -380,7 +380,7 @@ draw the data. In the example we also provide further arguments to set
the size, color of the dots and specify that they are filled
(listing\,\ref{scatterlisting1}).
\lstinputlisting[caption={Creating a scatter plot with red filled dots.},
\pageinputlisting[caption={Creating a scatter plot with red filled dots.},
label=scatterlisting1, firstline=9, lastline=9]{scatterplot.m}
We could have used plot for this purpose and set the marker to
@ -395,8 +395,7 @@ manipulate the color we need to specify a length(x)-by-3 matrix. For
each dot we provide an individual color (i.e. the RGB triplet in each
row of the color matrix, lines 2-4 in listing\,\ref{scatterlisting2})
\lstinputlisting[caption={Creating a scatter plot with size and color
\pageinputlisting[caption={Creating a scatter plot with size and color
variations. The RGB triplets define the respective color intensity
in a range 0:1. Here, we modify only the red color channel.},
label=scatterlisting2, linerange={15-15, 21-23}]{scatterplot.m}
@ -431,7 +430,7 @@ figures\,\ref{regularsubplotsfig}, \ref{irregularsubplotsfig}).
also below).}\label{regularsubplotsfig}
\end{figure}
\lstinputlisting[caption={Script for creating subplots in a regular
\pageinputlisting[caption={Script for creating subplots in a regular
grid \figref{regularsubplotsfig}.}, label=regularsubplotlisting,
basicstyle=\ttfamily\scriptsize]{regular_subplot.m}
@ -458,7 +457,7 @@ create a grid with larger numbers of columns and rows, and specify the
used cells of the grid by passing a vector as the third argument to
\code{subplot()}.
\lstinputlisting[caption={Script for creating subplots of different
\pageinputlisting[caption={Script for creating subplots of different
sizes \figref{irregularsubplotsfig}.},
label=irregularsubplotslisting,
basicstyle=\ttfamily\scriptsize]{irregular_subplot.m}
@ -516,7 +515,7 @@ its properties. See the \matlab{} help for more information.
listing\,\ref{errorbarlisting} for A and C and listing\,\ref{errorbarlisting2} }\label{errorbarplot}
\end{figure}
\lstinputlisting[caption={Illustrating estimation errors using error bars. Script that
\pageinputlisting[caption={Illustrating estimation errors using error bars. Script that
creates \figref{errorbarplot}. A, B},
label=errorbarlisting, firstline=13, lastline=31,
basicstyle=\ttfamily\scriptsize]{errorbarplot.m}
@ -550,7 +549,7 @@ leading to invisibility and a value of one to complete
opaqueness. Finally, we use the normal plot command to draw a line
connecting the average values (line 12).
\lstinputlisting[caption={Illustrating estimation errors using a shaded area. Script that
\pageinputlisting[caption={Illustrating estimation errors using a shaded area. Script that
creates \figref{errorbarplot} C.}, label=errorbarlisting2,
firstline=33,
basicstyle=\ttfamily\scriptsize]{errorbarplot.m}
@ -575,7 +574,7 @@ listing\,\ref{annotationsplotlisting}. For more options consult the
listing\,\ref{annotationsplotlisting}}\label{annotationsplot}
\end{figure}
\lstinputlisting[caption={Adding annotations to figures. Script that
\pageinputlisting[caption={Adding annotations to figures. Script that
creates \figref{annotationsplot}.},
label=annotationsplotlisting,
basicstyle=\ttfamily\scriptsize]{annotations.m}
@ -632,7 +631,7 @@ Lissajous figure. The basic steps are:
\item Finally, close the file (line 31).
\end{enumerate}
\lstinputlisting[caption={Making animations and saving them as a
\pageinputlisting[caption={Making animations and saving them as a
movie.}, label=animationlisting, firstline=16, lastline=36,
basicstyle=\ttfamily\scriptsize]{movie_example.m}

View File

@ -1,7 +1,7 @@
%!PS-Adobe-2.0 EPSF-2.0
%%Title: pointprocessscetchA.tex
%%Creator: gnuplot 5.2 patchlevel 8
%%CreationDate: Mon Dec 7 16:09:58 2020
%%Creator: gnuplot 4.6 patchlevel 4
%%CreationDate: Fri Dec 11 21:08:22 2020
%%DocumentFonts:
%%BoundingBox: 50 50 373 135
%%EndComments
@ -18,7 +18,6 @@ gnudict begin
/Dashlength 1 def
/Landscape false def
/Level1 false def
/Level3 false def
/Rounded true def
/ClipToBoundingBox false def
/SuppressPDFMark false def
@ -30,11 +29,11 @@ gnudict begin
%
/vshift -73 def
/dl1 {
10.0 Dashlength userlinewidth gnulinewidth div mul mul mul
10.0 Dashlength mul mul
Rounded { currentlinewidth 0.75 mul sub dup 0 le { pop 0.01 } if } if
} def
/dl2 {
10.0 Dashlength userlinewidth gnulinewidth div mul mul mul
10.0 Dashlength mul mul
Rounded { currentlinewidth 0.75 mul add } if
} def
/hpt_ 31.5 def
@ -48,7 +47,7 @@ gnudict begin
} if
} def
%
% Gnuplot Prolog Version 5.2 (Dec 2017)
% Gnuplot Prolog Version 4.6 (September 2012)
%
%/SuppressPDFMark true def
%
@ -65,11 +64,11 @@ gnudict begin
/vpt2 vpt 2 mul def
/hpt2 hpt 2 mul def
/Lshow {currentpoint stroke M 0 vshift R
Blacktext {gsave 0 setgray textshow grestore} {textshow} ifelse} def
Blacktext {gsave 0 setgray show grestore} {show} ifelse} def
/Rshow {currentpoint stroke M dup stringwidth pop neg vshift R
Blacktext {gsave 0 setgray textshow grestore} {textshow} ifelse} def
Blacktext {gsave 0 setgray show grestore} {show} ifelse} def
/Cshow {currentpoint stroke M dup stringwidth pop -2 div vshift R
Blacktext {gsave 0 setgray textshow grestore} {textshow} ifelse} def
Blacktext {gsave 0 setgray show grestore} {show} ifelse} def
/UP {dup vpt_ mul /vpt exch def hpt_ mul /hpt exch def
/hpt2 hpt 2 mul def /vpt2 vpt 2 mul def} def
/DL {Color {setrgbcolor Solid {pop []} if 0 setdash}
@ -83,7 +82,7 @@ gnudict begin
/PL {stroke userlinewidth setlinewidth
Rounded {1 setlinejoin 1 setlinecap} if} def
3.8 setmiterlimit
% Classic Line colors (version 5.0)
% Default Line colors
/LCw {1 1 1} def
/LCb {0 0 0} def
/LCa {0 0 0} def
@ -96,21 +95,19 @@ gnudict begin
/LC6 {0 0 0} def
/LC7 {1 0.3 0} def
/LC8 {0.5 0.5 0.5} def
% Default dash patterns (version 5.0)
/LTB {BL [] LCb DL} def
% Default Line Types
/LTw {PL [] 1 setgray} def
/LTb {PL [] LCb DL} def
/LTb {BL [] LCb DL} def
/LTa {AL [1 udl mul 2 udl mul] 0 setdash LCa setrgbcolor} def
/LT0 {PL [] LC0 DL} def
/LT1 {PL [2 dl1 3 dl2] LC1 DL} def
/LT2 {PL [1 dl1 1.5 dl2] LC2 DL} def
/LT3 {PL [6 dl1 2 dl2 1 dl1 2 dl2] LC3 DL} def
/LT4 {PL [1 dl1 2 dl2 6 dl1 2 dl2 1 dl1 2 dl2] LC4 DL} def
/LT5 {PL [4 dl1 2 dl2] LC5 DL} def
/LT6 {PL [1.5 dl1 1.5 dl2 1.5 dl1 1.5 dl2 1.5 dl1 6 dl2] LC6 DL} def
/LT7 {PL [3 dl1 3 dl2 1 dl1 3 dl2] LC7 DL} def
/LT8 {PL [2 dl1 2 dl2 2 dl1 6 dl2] LC8 DL} def
/SL {[] 0 setdash} def
/LT1 {PL [4 dl1 2 dl2] LC1 DL} def
/LT2 {PL [2 dl1 3 dl2] LC2 DL} def
/LT3 {PL [1 dl1 1.5 dl2] LC3 DL} def
/LT4 {PL [6 dl1 2 dl2 1 dl1 2 dl2] LC4 DL} def
/LT5 {PL [3 dl1 3 dl2 1 dl1 3 dl2] LC5 DL} def
/LT6 {PL [2 dl1 2 dl2 2 dl1 6 dl2] LC6 DL} def
/LT7 {PL [1 dl1 2 dl2 6 dl1 2 dl2 1 dl1 2 dl2] LC7 DL} def
/LT8 {PL [2 dl1 2 dl2 2 dl1 2 dl2 2 dl1 2 dl2 2 dl1 4 dl2] LC8 DL} def
/Pnt {stroke [] 0 setdash gsave 1 setlinecap M 0 0 V stroke grestore} def
/Dia {stroke [] 0 setdash 2 copy vpt add M
hpt neg vpt neg V hpt vpt neg V
@ -305,7 +302,7 @@ gnudict begin
ColR ColG ColB setrgbcolor} def
/BoxColFill {gsave Rec PolyFill} def
/PolyFill {gsave Density fill grestore grestore} def
/h {rlineto rlineto rlineto closepath gsave fill grestore stroke} bind def
/h {rlineto rlineto rlineto gsave closepath fill grestore} bind def
%
% PostScript Level 1 Pattern Fill routine for rectangles
% Usage: x y w h s a XX PatternFill
@ -332,14 +329,9 @@ gnudict begin
%
/languagelevel where
{pop languagelevel} {1} ifelse
dup 2 lt
{/InterpretLevel1 true def
/InterpretLevel3 false def}
{/InterpretLevel1 Level1 def
2 gt
{/InterpretLevel3 Level3 def}
{/InterpretLevel3 false def}
ifelse }
2 lt
{/InterpretLevel1 true def}
{/InterpretLevel1 Level1 def}
ifelse
%
% PostScript level 2 pattern fill definitions
@ -428,7 +420,6 @@ Level1 {Level1PatternFill} {Level2PatternFill} ifelse
/Symbol-Oblique /Symbol findfont [1 0 .167 1 0 0] makefont
dup length dict begin {1 index /FID eq {pop pop} {def} ifelse} forall
currentdict end definefont pop
%
Level1 SuppressPDFMark or
{} {
/SDict 10 dict def
@ -438,39 +429,14 @@ systemdict /pdfmark known not {
SDict begin [
/Title (pointprocessscetchA.tex)
/Subject (gnuplot plot)
/Creator (gnuplot 5.2 patchlevel 8)
/Creator (gnuplot 4.6 patchlevel 4)
/Author (jan)
% /Producer (gnuplot)
% /Keywords ()
/CreationDate (Mon Dec 7 16:09:58 2020)
/CreationDate (Fri Dec 11 21:08:22 2020)
/DOCINFO pdfmark
end
} ifelse
%
% Support for boxed text - Ethan A Merritt Sep 2016
%
/InitTextBox { userdict /TBy2 3 -1 roll put userdict /TBx2 3 -1 roll put
userdict /TBy1 3 -1 roll put userdict /TBx1 3 -1 roll put
/Boxing true def } def
/ExtendTextBox { dup type /stringtype eq
{ Boxing { gsave dup false charpath pathbbox
dup TBy2 gt {userdict /TBy2 3 -1 roll put} {pop} ifelse
dup TBx2 gt {userdict /TBx2 3 -1 roll put} {pop} ifelse
dup TBy1 lt {userdict /TBy1 3 -1 roll put} {pop} ifelse
dup TBx1 lt {userdict /TBx1 3 -1 roll put} {pop} ifelse
grestore } if }
{} ifelse} def
/PopTextBox { newpath TBx1 TBxmargin sub TBy1 TBymargin sub M
TBx1 TBxmargin sub TBy2 TBymargin add L
TBx2 TBxmargin add TBy2 TBymargin add L
TBx2 TBxmargin add TBy1 TBymargin sub L closepath } def
/DrawTextBox { PopTextBox stroke /Boxing false def} def
/FillTextBox { gsave PopTextBox fill grestore /Boxing false def} def
0 0 0 0 InitTextBox
/TBxmargin 20 def
/TBymargin 20 def
/Boxing false def
/textshow { ExtendTextBox Gshow } def
%
end
%%EndProlog
%%Page: 1 1
@ -484,33 +450,27 @@ newpath
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
1.000 UP
0.00 0.00 0.00 C 1.000 UP
LTb
LCb setrgbcolor
LTb
1.000 UL
[] 0 setdash
LTb
gsave 6208 824 N 0 -32 V 121 32 V -121 32 V 0 -32 V 1 PolyFill
6208 824 M
stroke
6208 824 N
0 -32 V
121 32 V
-121 32 V
0 -32 V
Z stroke
528 824 M
5680 0 V
stroke
2.000 UL
LTb
0.00 0.00 0.00 C
% Begin plot #1
10.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
910 573 M
0.00 0.00 0.00 C 10.000 UL
LT0
LC0 setrgbcolor
0.00 0.00 0.00 C 910 573 M
0 503 V
1412 573 M
0 503 V
@ -528,18 +488,12 @@ LCb setrgbcolor
0 503 V
5685 573 M
0 503 V
1.000 UP
stroke
LTw
% End plot #1
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
1.000 UP
2.000 UL
LTb
0.00 0.00 0.00 C
stroke
0.00 0.00 0.00 C stroke
grestore
end
showpage

View File

@ -1,7 +1,7 @@
%!PS-Adobe-2.0 EPSF-2.0
%%Title: pointprocessscetchB.tex
%%Creator: gnuplot 5.2 patchlevel 8
%%CreationDate: Mon Dec 7 16:09:59 2020
%%Creator: gnuplot 4.6 patchlevel 4
%%CreationDate: Fri Dec 11 21:08:22 2020
%%DocumentFonts:
%%BoundingBox: 50 50 373 237
%%EndComments
@ -18,7 +18,6 @@ gnudict begin
/Dashlength 1 def
/Landscape false def
/Level1 false def
/Level3 false def
/Rounded true def
/ClipToBoundingBox false def
/SuppressPDFMark false def
@ -30,11 +29,11 @@ gnudict begin
%
/vshift -73 def
/dl1 {
10.0 Dashlength userlinewidth gnulinewidth div mul mul mul
10.0 Dashlength mul mul
Rounded { currentlinewidth 0.75 mul sub dup 0 le { pop 0.01 } if } if
} def
/dl2 {
10.0 Dashlength userlinewidth gnulinewidth div mul mul mul
10.0 Dashlength mul mul
Rounded { currentlinewidth 0.75 mul add } if
} def
/hpt_ 31.5 def
@ -48,7 +47,7 @@ gnudict begin
} if
} def
%
% Gnuplot Prolog Version 5.2 (Dec 2017)
% Gnuplot Prolog Version 4.6 (September 2012)
%
%/SuppressPDFMark true def
%
@ -65,11 +64,11 @@ gnudict begin
/vpt2 vpt 2 mul def
/hpt2 hpt 2 mul def
/Lshow {currentpoint stroke M 0 vshift R
Blacktext {gsave 0 setgray textshow grestore} {textshow} ifelse} def
Blacktext {gsave 0 setgray show grestore} {show} ifelse} def
/Rshow {currentpoint stroke M dup stringwidth pop neg vshift R
Blacktext {gsave 0 setgray textshow grestore} {textshow} ifelse} def
Blacktext {gsave 0 setgray show grestore} {show} ifelse} def
/Cshow {currentpoint stroke M dup stringwidth pop -2 div vshift R
Blacktext {gsave 0 setgray textshow grestore} {textshow} ifelse} def
Blacktext {gsave 0 setgray show grestore} {show} ifelse} def
/UP {dup vpt_ mul /vpt exch def hpt_ mul /hpt exch def
/hpt2 hpt 2 mul def /vpt2 vpt 2 mul def} def
/DL {Color {setrgbcolor Solid {pop []} if 0 setdash}
@ -83,7 +82,7 @@ gnudict begin
/PL {stroke userlinewidth setlinewidth
Rounded {1 setlinejoin 1 setlinecap} if} def
3.8 setmiterlimit
% Classic Line colors (version 5.0)
% Default Line colors
/LCw {1 1 1} def
/LCb {0 0 0} def
/LCa {0 0 0} def
@ -96,21 +95,19 @@ gnudict begin
/LC6 {0 0 0} def
/LC7 {1 0.3 0} def
/LC8 {0.5 0.5 0.5} def
% Default dash patterns (version 5.0)
/LTB {BL [] LCb DL} def
% Default Line Types
/LTw {PL [] 1 setgray} def
/LTb {PL [] LCb DL} def
/LTb {BL [] LCb DL} def
/LTa {AL [1 udl mul 2 udl mul] 0 setdash LCa setrgbcolor} def
/LT0 {PL [] LC0 DL} def
/LT1 {PL [2 dl1 3 dl2] LC1 DL} def
/LT2 {PL [1 dl1 1.5 dl2] LC2 DL} def
/LT3 {PL [6 dl1 2 dl2 1 dl1 2 dl2] LC3 DL} def
/LT4 {PL [1 dl1 2 dl2 6 dl1 2 dl2 1 dl1 2 dl2] LC4 DL} def
/LT5 {PL [4 dl1 2 dl2] LC5 DL} def
/LT6 {PL [1.5 dl1 1.5 dl2 1.5 dl1 1.5 dl2 1.5 dl1 6 dl2] LC6 DL} def
/LT7 {PL [3 dl1 3 dl2 1 dl1 3 dl2] LC7 DL} def
/LT8 {PL [2 dl1 2 dl2 2 dl1 6 dl2] LC8 DL} def
/SL {[] 0 setdash} def
/LT1 {PL [4 dl1 2 dl2] LC1 DL} def
/LT2 {PL [2 dl1 3 dl2] LC2 DL} def
/LT3 {PL [1 dl1 1.5 dl2] LC3 DL} def
/LT4 {PL [6 dl1 2 dl2 1 dl1 2 dl2] LC4 DL} def
/LT5 {PL [3 dl1 3 dl2 1 dl1 3 dl2] LC5 DL} def
/LT6 {PL [2 dl1 2 dl2 2 dl1 6 dl2] LC6 DL} def
/LT7 {PL [1 dl1 2 dl2 6 dl1 2 dl2 1 dl1 2 dl2] LC7 DL} def
/LT8 {PL [2 dl1 2 dl2 2 dl1 2 dl2 2 dl1 2 dl2 2 dl1 4 dl2] LC8 DL} def
/Pnt {stroke [] 0 setdash gsave 1 setlinecap M 0 0 V stroke grestore} def
/Dia {stroke [] 0 setdash 2 copy vpt add M
hpt neg vpt neg V hpt vpt neg V
@ -305,7 +302,7 @@ gnudict begin
ColR ColG ColB setrgbcolor} def
/BoxColFill {gsave Rec PolyFill} def
/PolyFill {gsave Density fill grestore grestore} def
/h {rlineto rlineto rlineto closepath gsave fill grestore stroke} bind def
/h {rlineto rlineto rlineto gsave closepath fill grestore} bind def
%
% PostScript Level 1 Pattern Fill routine for rectangles
% Usage: x y w h s a XX PatternFill
@ -332,14 +329,9 @@ gnudict begin
%
/languagelevel where
{pop languagelevel} {1} ifelse
dup 2 lt
{/InterpretLevel1 true def
/InterpretLevel3 false def}
{/InterpretLevel1 Level1 def
2 gt
{/InterpretLevel3 Level3 def}
{/InterpretLevel3 false def}
ifelse }
2 lt
{/InterpretLevel1 true def}
{/InterpretLevel1 Level1 def}
ifelse
%
% PostScript level 2 pattern fill definitions
@ -428,7 +420,6 @@ Level1 {Level1PatternFill} {Level2PatternFill} ifelse
/Symbol-Oblique /Symbol findfont [1 0 .167 1 0 0] makefont
dup length dict begin {1 index /FID eq {pop pop} {def} ifelse} forall
currentdict end definefont pop
%
Level1 SuppressPDFMark or
{} {
/SDict 10 dict def
@ -438,39 +429,14 @@ systemdict /pdfmark known not {
SDict begin [
/Title (pointprocessscetchB.tex)
/Subject (gnuplot plot)
/Creator (gnuplot 5.2 patchlevel 8)
/Creator (gnuplot 4.6 patchlevel 4)
/Author (jan)
% /Producer (gnuplot)
% /Keywords ()
/CreationDate (Mon Dec 7 16:09:59 2020)
/CreationDate (Fri Dec 11 21:08:22 2020)
/DOCINFO pdfmark
end
} ifelse
%
% Support for boxed text - Ethan A Merritt Sep 2016
%
/InitTextBox { userdict /TBy2 3 -1 roll put userdict /TBx2 3 -1 roll put
userdict /TBy1 3 -1 roll put userdict /TBx1 3 -1 roll put
/Boxing true def } def
/ExtendTextBox { dup type /stringtype eq
{ Boxing { gsave dup false charpath pathbbox
dup TBy2 gt {userdict /TBy2 3 -1 roll put} {pop} ifelse
dup TBx2 gt {userdict /TBx2 3 -1 roll put} {pop} ifelse
dup TBy1 lt {userdict /TBy1 3 -1 roll put} {pop} ifelse
dup TBx1 lt {userdict /TBx1 3 -1 roll put} {pop} ifelse
grestore } if }
{} ifelse} def
/PopTextBox { newpath TBx1 TBxmargin sub TBy1 TBymargin sub M
TBx1 TBxmargin sub TBy2 TBymargin add L
TBx2 TBxmargin add TBy2 TBymargin add L
TBx2 TBxmargin add TBy1 TBymargin sub L closepath } def
/DrawTextBox { PopTextBox stroke /Boxing false def} def
/FillTextBox { gsave PopTextBox fill grestore /Boxing false def} def
0 0 0 0 InitTextBox
/TBxmargin 20 def
/TBymargin 20 def
/Boxing false def
/textshow { ExtendTextBox Gshow } def
%
end
%%EndProlog
%%Page: 1 1
@ -484,33 +450,27 @@ newpath
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
1.000 UP
0.00 0.00 0.00 C 1.000 UP
LTb
LCb setrgbcolor
LTb
1.000 UL
[] 0 setdash
LTb
gsave 6208 3165 N 0 -32 V 121 32 V -121 32 V 0 -32 V 1 PolyFill
6208 3165 M
stroke
6208 3165 N
0 -32 V
121 32 V
-121 32 V
0 -32 V
Z stroke
528 3165 M
-5680 0 R
5680 0 V
stroke
2.000 UL
LTb
0.00 0.00 0.00 C
% Begin plot #1
10.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
910 3029 M
0.00 0.00 0.00 C 10.000 UL
LT0
LC0 setrgbcolor
0.00 0.00 0.00 C 910 3029 M
0 272 V
502 -272 R
0 272 V
@ -528,167 +488,99 @@ LCb setrgbcolor
0 272 V
5685 3029 M
0 272 V
1.000 UP
stroke
LTw
% End plot #1
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
1.000 UP
2.000 UL
LTb
0.00 0.00 0.00 C
2.000 UL
0.00 0.00 0.00 C 2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
1.000 UP
0.00 0.00 0.00 C 1.000 UP
LTb
LCb setrgbcolor
LTb
1.000 UL
[] 0 setdash
LTb
gsave 6208 2043 N 0 -32 V 121 32 V -121 32 V 0 -32 V 1 PolyFill
6208 2043 M
stroke
6208 2043 N
0 -32 V
121 32 V
-121 32 V
0 -32 V
Z stroke
528 2043 M
-5680 0 R
5680 0 V
stroke
1.000 UL
[] 0 setdash
1291 1929 M
stroke
1291 1929 N
121 32 V
-121 32 V
-260 0 R
stroke
1031 1993 N
910 1961 L
121 -32 V
-121 32 R
-260 -64 R
-121 32 V
121 32 V
910 1961 M
502 0 V
stroke
1.000 UL
[] 0 setdash
2002 1929 M
stroke
2002 1929 N
590 -32 R
121 32 V
-121 32 V
-469 0 R
stroke
1533 1993 N
-121 -32 V
121 -32 V
-121 32 R
-469 -64 R
-121 32 V
121 32 V
-121 -32 R
711 0 V
stroke
1.000 UL
[] 0 setdash
2329 1939 M
stroke
2329 1939 N
206 -22 R
84 22 V
-84 22 V
-122 0 R
stroke
2207 1983 N
-84 -22 V
84 -22 V
-84 22 R
-122 -44 R
-84 22 V
84 22 V
-84 -22 R
290 0 V
stroke
1.000 UL
[] 0 setdash
2629 1938 M
stroke
2629 1938 N
216 -23 R
88 23 V
-88 23 V
-128 0 R
stroke
2501 1984 N
-88 -23 V
88 -23 V
-88 23 R
-128 -46 R
-88 23 V
88 23 V
-88 -23 R
304 0 V
stroke
1.000 UL
[] 0 setdash
3046 1929 M
stroke
3046 1929 N
329 -32 R
121 32 V
-121 32 V
-208 0 R
stroke
2838 1993 N
-121 -32 V
121 -32 V
-121 32 R
-208 -64 R
-121 32 V
121 32 V
-121 -32 R
450 0 V
stroke
1.000 UL
[] 0 setdash
3912 1929 M
stroke
3912 1929 N
745 -32 R
121 32 V
-121 32 V
-624 0 R
stroke
3288 1993 N
-121 -32 V
121 -32 V
-121 32 R
-624 -64 R
-121 32 V
121 32 V
-121 -32 R
866 0 V
stroke
1.000 UL
[] 0 setdash
4529 1929 M
stroke
4529 1929 N
496 -32 R
121 32 V
-121 32 V
-375 0 R
stroke
4154 1993 N
-121 -32 V
121 -32 V
-121 32 R
-375 -64 R
-121 32 V
121 32 V
-121 -32 R
617 0 V
stroke
1.000 UL
[] 0 setdash
5564 1929 M
stroke
5564 1929 N
914 -32 R
121 32 V
-121 32 V
-793 0 R
stroke
4771 1993 N
-121 -32 V
121 -32 V
-121 32 R
-793 -64 R
-121 32 V
121 32 V
-121 -32 R
1035 0 V
stroke
2.000 UL
LTb
0.00 0.00 0.00 C
% Begin plot #1
10.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
910 1907 M
0.00 0.00 0.00 C 10.000 UL
LT0
LC0 setrgbcolor
0.00 0.00 0.00 C 910 1907 M
0 272 V
502 -272 R
0 272 V
@ -706,100 +598,74 @@ LCb setrgbcolor
0 272 V
5685 1907 M
0 272 V
1.000 UP
stroke
LTw
% End plot #1
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
1.000 UP
2.000 UL
LTb
0.00 0.00 0.00 C
2.000 UL
0.00 0.00 0.00 C 2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 268 M
0.00 0.00 0.00 C 528 268 M
-63 0 V
stroke
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 460 M
0.00 0.00 0.00 C 528 460 M
-63 0 V
stroke
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 652 M
0.00 0.00 0.00 C 528 652 M
-63 0 V
stroke
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 844 M
0.00 0.00 0.00 C 528 844 M
-63 0 V
stroke
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 1036 M
0.00 0.00 0.00 C 528 1036 M
-63 0 V
stroke
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 1228 M
0.00 0.00 0.00 C 528 1228 M
-63 0 V
stroke
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
2.000 UL
0.00 0.00 0.00 C 2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 1276 M
0.00 0.00 0.00 C 528 1276 M
528 220 L
5801 0 R
0 1056 R
-5801 0 R
1.000 UP
stroke
LTb
LCb setrgbcolor
LTb
1.000 UL
[] 0 setdash
LTb
gsave 6208 268 N 0 -32 V 121 32 V -121 32 V 0 -32 V 1 PolyFill
6208 268 M
stroke
6208 268 N
0 -32 V
121 32 V
-121 32 V
0 -32 V
Z stroke
528 268 M
5680 0 V
stroke
2.000 UL
LTb
0.00 0.00 0.00 C
% Begin plot #1
3.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
528 268 M
0.00 0.00 0.00 C 3.000 UL
LT0
LC0 setrgbcolor
0.00 0.00 0.00 C 528 268 M
382 0 V
0 96 R
502 0 V
@ -819,17 +685,12 @@ LCb setrgbcolor
1035 0 V
0 96 R
533 0 V
stroke
LTw
% End plot #1
% Begin plot #2
1.500 UP
stroke
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
910 268 CircleF
LT0
LC0 setrgbcolor
0.00 0.00 0.00 C 910 268 CircleF
1412 364 CircleF
2123 460 CircleF
2413 556 CircleF
@ -838,16 +699,11 @@ LCb setrgbcolor
4033 844 CircleF
4650 940 CircleF
5685 1036 CircleF
LTw
% End plot #2
% Begin plot #3
1.000 UP
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
1.00 1.00 1.00 C
910 268 CircleF
LT0
LC0 setrgbcolor
1.00 1.00 1.00 C 910 268 CircleF
1412 364 CircleF
2123 460 CircleF
2413 556 CircleF
@ -856,16 +712,11 @@ LCb setrgbcolor
4033 844 CircleF
4650 940 CircleF
5685 1036 CircleF
LTw
% End plot #3
% Begin plot #4
1.500 UP
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
0.00 0.00 0.00 C
910 364 CircleF
LT0
LC0 setrgbcolor
0.00 0.00 0.00 C 910 364 CircleF
1412 460 CircleF
2123 556 CircleF
2413 652 CircleF
@ -874,17 +725,11 @@ LCb setrgbcolor
4033 940 CircleF
4650 1036 CircleF
5685 1132 CircleF
LTw
% End plot #4
2.000 UL
LTb
LCb setrgbcolor
[] 0 setdash
1.000 UP
2.000 UL
LTb
0.00 0.00 0.00 C
stroke
LCb setrgbcolor
0.00 0.00 0.00 C stroke
grestore
end
showpage

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
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

@ -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
@ -271,8 +269,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
@ -284,7 +282,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()}
@ -292,14 +290,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
@ -310,7 +308,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
@ -333,7 +331,7 @@ b =
>> size(b)
ans =
1 10
\end{lstlisting}
\end{pagelisting}
\subsubsection{Accessing elements of a vector}
@ -354,7 +352,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}
@ -365,8 +363,8 @@ individual values by providing a single index or use the
\code[Operator!Matrix!:]{:} notation to access multiple values with a
single command.
\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
@ -376,14 +374,14 @@ ans = 11
ans = 15
>> 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
@ -394,7 +392,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
@ -403,13 +401,13 @@ ans =
\end{exercise}
\begin{important}[The : (colon) operator]
The colon \code[Operator!colon@:]{:} operator is often used when working with vectors. It has multiple purposes.
The colon \code[Operator!colon@:]{:} operator is often used when working with vectors. It has multiple purposes.\vspace{-1ex}
\begin{enumerate}
\item In the simplest form, \code{x = a:b} with \code{a} and \code{b} being two numbers, it creates
a vector \code{x} containing the numbers \code{a} to \code{b} in integer steps. In \matlab{} the borders $a$ and $b$ are included $[a, b]$ or $a\leq x \leq b$.
\item In the form \code{x = a:c:b} the vector \code{x} uses a \emph{stepsize} of \code{c} to create the range of numbers.
\item When used in the context of indexing such as \code{x(:)} all elements of the vector x are accessed.
\item In the simplest form, \code{x = a:b} with \code{a} and \code{b} being two numbers, it generates
a vector \code{x} containing the numbers \code{a} to \code{b} inclusively in integer steps.
\item In the form \code{x = a:c:b} the vector \code{x} uses a \emph{stepsize} of \code{c} to generate the range of numbers.
\item As vectors are often used for indexing in other vectors one use the colon operator to create such vectors implicitely, e.g. \varcode{x(1:2:end)} to access every seond element of \code{x}.
\item Indexing with a single colon, e.g. \code{x(:)}, returns all elements of the vector \code{x} as a row vector.
\end{enumerate}
\end{important}
@ -423,7 +421,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
@ -447,7 +445,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
@ -460,7 +458,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
@ -479,7 +477,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
@ -489,7 +487,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 =
@ -509,7 +507,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
@ -519,7 +517,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.
@ -536,14 +534,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
@ -556,7 +553,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
@ -718,7 +715,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 =
@ -735,17 +732,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
@ -795,8 +792,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
@ -822,7 +818,7 @@ ans =
10 15 20
24 23 35
16 17 25
\end{lstlisting}
\end{pagelisting}
\section{Boolean expressions}
@ -854,7 +850,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
@ -871,7 +867,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
@ -884,7 +880,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
@ -922,8 +918,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)
@ -941,7 +937,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
@ -1038,7 +1034,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
@ -1049,7 +1045,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}
@ -1064,7 +1060,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
@ -1093,7 +1088,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
@ -1110,7 +1105,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.
@ -1119,8 +1114,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]
@ -1142,7 +1136,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;}.
@ -1150,9 +1143,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}
@ -1230,7 +1223,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;
@ -1239,7 +1232,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
@ -1279,7 +1272,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
@ -1288,7 +1281,7 @@ for-loop.
1
2
3
\end{lstlisting}
\end{pagelisting}
\begin{exercise}{factorialLoop.m}{factorialLoop.out}
@ -1308,11 +1301,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]
\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.
@ -1367,7 +1360,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
@ -1375,7 +1368,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}
@ -1403,7 +1396,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
@ -1413,7 +1406,7 @@ switch mynumber
otherwise
disp('something else');
end
\end{lstlisting}
\end{pagelisting}
\subsubsection{Comparison \varcode{if} and \varcode{switch} -- statements}
@ -1437,7 +1430,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)
@ -1450,9 +1443,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;
@ -1463,7 +1456,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
@ -1535,11 +1528,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
@ -1572,7 +1565,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;
@ -1583,7 +1576,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
@ -1644,7 +1637,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.
@ -1662,7 +1655,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}
@ -1689,7 +1682,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.
%
@ -1701,7 +1694,7 @@ function plotFunction(x_data, y_data, name)
% name : the displayname
plot(x_data, y_data, 'displayname', name)
end
\end{lstlisting}
\end{pagelisting}
\paragraph{III. One script to rule them all}
@ -1728,7 +1721,7 @@ Again, we need to specify what needs to be done:
The implementation is shown in listing~\ref{sinesskriptlisting}.
\begin{lstlisting}[caption={Control script for the plotting of sine waves.},label=sinesskriptlisting]
\begin{pagelisting}[caption={Control script for the plotting of sine waves.},label=sinesskriptlisting]
amplitudes = 0.25:0.25:1.25;
frequency = 2.0;
duration = 10.0; % seconds
@ -1744,11 +1737,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}