Skip to main content
Engineering LibreTexts

1.5: Errors

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Every error is a learning opportunity. Whenever you learn a new feature, you should try to make as many errors as possible, as soon as possible. When you make deliberate errors, you see what the error messages are. Later, when you make accidental errors, you’ll know what the messages mean.

    Let’s look at some common errors. A big one for beginners is leaving out the * for multiplication, as in this example:

    area = pi r^2

    This code should produce the following error message:

    area = pi r^2
    Error: Invalid expression. Check for missing multiplication 
    operator, missing or unbalanced delimiters, or other syntax 
    error. To construct matrices, use brackets instead of parentheses.

    The message indicates that the expression is invalid and suggests several things that might be wrong. In this case, one of its guesses is right: we’re missing a multiplication operator.

    Another common error is to leave out the parentheses around the arguments of a function. For example, in math notation it’s common to write something like \(\sin \pi\), but in MATLAB if you write

    sin pi

    you should get the following error message:

    Undefined function ’sin’ for input arguments of type 'char'.

    The problem is that when you leave out the parentheses, MATLAB treats the argument as a string of characters (which have type 'char'). In this case the error message is helpful, but in other cases the results can be baffling. For example, if you call abs, which computes absolute values, and forget the parentheses, you get a surprising result:

    >> abs pi
    ans =  112   105

    I won’t explain this result; for now, I’ll just suggest that you should put parentheses around arguments.

    Here’s another common error. If you were translating the mathematical expression \[\frac{1}{2 \sqrt \pi}\] into MATLAB, you might be tempted to write this:

    1 / 2 * sqrt(pi)

    But that would be wrong because of the order of operations. Division and multiplication are evaluated from left to right, so this expression would multiply 1/2 by sqrt(pi).

    To keep sqrt(pi) in the denominator, you could use parentheses,

    1 / (2 * sqrt(pi))

    or make the division explicit,

    1 / 2 / sqrt(pi)

    The last two examples bring us to the Second Theorem of Debugging:

    The only thing worse than getting an error message is not getting an error message.

    Beginning programmers often hate error messages and do everything they can to make the messages go away. Experienced programmers know that error messages are your friend. They can be hard to understand, and even misleading, but it’s worth the effort to understand them.

    This page titled 1.5: Errors is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.