fixed many index entries

This commit is contained in:
2019-12-09 20:01:27 +01:00
parent f24c14e6f5
commit bf52536b7b
12 changed files with 332 additions and 306 deletions

View File

@@ -49,13 +49,14 @@ obscure logical errors! Take care when using the \codeterm{try-catch
\end{important}
\subsection{\codeterm{Syntax errors}}\label{syntax_error}
The most common and easiest to fix type of error. A syntax error
violates the rules (spelling and grammar) of the programming
language. For example every opening parenthesis must be matched by a
closing one or every \code{for} loop has to be closed by an
\code{end}. Usually, the respective error messages are clear and
the editor will point out and highlight most \codeterm{syntax error}s.
\subsection{Syntax errors}\label{syntax_error}
The most common and easiest to fix type of error. A
\entermde[error!syntax]{Fehler!Syntax\~}{syntax error} violates the
rules (spelling and grammar) of the programming language. For example
every opening parenthesis must be matched by a closing one or every
\code{for} loop has to be closed by an \code{end}. Usually, the
respective error messages are clear and the editor will point out and
highlight most syntax errors.
\begin{lstlisting}[label=syntaxerror, caption={Unbalanced parenthesis error.}]
>> mean(random_numbers
@@ -66,8 +67,9 @@ Did you mean:
>> mean(random_numbers)
\end{lstlisting}
\subsection{\codeterm{Indexing error}}\label{index_error}
Second on the list of common errors are the indexing errors. Usually
\subsection{Indexing error}\label{index_error}
Second on the list of common errors are the
\entermde[error!indexing]{Fehler!Index\~}{indexing errors}. Usually
\matlab{} gives rather precise infromation about the cause, once you
know what they mean. Consider the following code.
@@ -111,14 +113,16 @@ to a number and uses this number to address the element in
\varcode{my\_array}. The \codeterm{char} has the ASCII code 65 and
thus the 65th element of \varcode{my\_array} is returned.
\subsection{\codeterm{Assignment error}}
Related to the Indexing error, an assignment error occurs when we want
to write data into a variable, that does not fit into it. Listing
\ref{assignmenterror} shows the simple case for 1-d data but, of
course, it extents to n-dimensional data. The data that is to be
filled into a matrix hat to fit in all 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.
\subsection{Assignment error}
Related to the indexing error, an
\entermde[error!assignment]{Fehler!Zuweisungs\~}{assignment error}
occurs when we want to write data into a variable, that does not fit
into it. Listing \ref{assignmenterror} shows the simple case for 1-d
data but, of course, it extents to n-dimensional data. The data that
is to be filled into a matrix hat to fit in all 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.}]
>> a = zeros(1, 100);
@@ -133,21 +137,20 @@ ans =
110 1
\end{lstlisting}
\subsection{\codeterm{Dimension mismatch error}}
\subsection{Dimension mismatch error}
Similarly, some arithmetic operations are only valid if the variables
fulfill some size constraints. Consider the following commands
(listing\,\ref{dimensionmismatch}). The first one (line 3) fails
because we are trying to do al elementwise add on two vectors that
have different lengths, respectively sizes. The matrix multiplication
in line 6 also fails since for this operations to succeed the inner
matrix dimensions must agree (for more information on the
matrixmultiplication see box\,\ref{matrixmultiplication} in
chapter\,\ref{programming}). The elementwise multiplication issued in
line 10 fails for the same reason as the addition we tried
earlier. Sometimes, however, things apparently work but the result may
be surprising. The last operation in listing\,\ref{dimensionmismatch}
does not throw an error but the result is something else than the
expected elementwise multiplication.
because we are trying to add two vectors of different lengths
elementwise. The matrix multiplication in line 6 also fails since for
this operations to succeed the inner matrix dimensions must agree (for
more information on the matrixmultiplication see
box\,\ref{matrixmultiplication} in chapter\,\ref{programming}). The
elementwise multiplication issued in line 10 fails for the same reason
as the addition we tried earlier. Sometimes, however, things
apparently work but the result may be surprising. The last operation
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.}]
@@ -174,7 +177,8 @@ expected elementwise multiplication.
\section{Logical error}
Sometimes a program runs smoothly and terminates without any
complaint. This, however, does not necessarily mean that the program
is correct. We may have made a \codeterm{logical error}. Logical
is correct. We may have made a
\entermde[error!logical]{Fehler!logischer}{logical error}. Logical
errors are hard to find, \matlab{} has no chance to detect such errors
since they do not violate the syntax or cause the throwing of an
error. Thus, we are on our own to find and fix the bug. There are a
@@ -283,7 +287,7 @@ validity.
Matlab offers a unit testing framework in which small scripts are
written that test the features of the program. We will follow the
example given in the \matlab{} help and assume that there is a
function \code{rightTriangle} (listing\,\ref{trianglelisting}).
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.}]
@@ -308,7 +312,7 @@ folder that follows the following rules.
\item The name of the script file must start or end with the word
'test', which is case-insensitive.
\item Each unit test should be placed in a separate section/cell of the script.
\item After the \code{\%\%} that defines the cell, a name for the
\item After the \mcode{\%\%} that defines the cell, a name for the
particular unit test may be given.
\end{enumerate}
@@ -328,11 +332,11 @@ Further there are a few things that are different in tests compared to normal sc
tests.
\end{enumerate}
The test script for the \code{rightTrianlge} function
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 \code{rightTriangle} function stored in an m-file testRightTriangle.m}]
\begin{lstlisting}[label=testscript, caption={Unit test for the \varcode{rightTriangle()} function stored in an m-file testRightTriangle.m}]
tolerance = 1e-10;
% preconditions
@@ -372,7 +376,7 @@ assert(abs(approx - smallAngle) <= tolerance, 'Problem with small angle approxim
In a test script we can execute any code. The actual test whether or
not the results match our predictions is done using the
\code{assert()}{assert} function. This function basically expects a
\code{assert()} function. This function basically expects a
boolean value and if this is not true, it raises an error that, in the
context of the test does not lead to a termination of the program. In
the tests above, the argument to assert is always a boolean expression
@@ -392,7 +396,7 @@ result = runtests('testRightTriangle')
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(result)}
\code[table()]{table(result)}.
\begin{lstlisting}[label=testresults, caption={The test results.}, basicstyle=\ttfamily\scriptsize]
table(result)
@@ -431,7 +435,7 @@ that help to solve the problem.
\item No idea what the error message is trying to say? Google it!
\item Read the program line by line and understand what each line is
doing.
\item Use \code{disp} to print out relevant information on the command
\item Use \code{disp()} to print out relevant information on the command
line and compare the output with your expectations. Do this step by
step and start at the beginning.
\item Use the \matlab{} debugger to stop execution of the code at a