There are two main types of errors that can be introduced while programming in any language:
1. Syntax Errors
2. Algorithmic Errors
A syntax error occurs when the calling syntax you use for a function is incorrect, or when you provide the function with inputs that are of the wrong shape, size, and/or type, or are otherwise not valid for the function in question. A typo, or typographic error, could be considered as a syntax error. If it occurs while you are typing a function name, it can be easy to find. For example, typing
when you meant to type
will return an error message that is easy to understand:
??? Undefined function or variable 'cod'.
However, if you mistype a variable name, it can lead to unexpected results that can be extremely difficult to track down and eliminate. For example, if you type
when you really meant to type
and "t" is of different size than "y", you will likely receive an error message about the size of the array when you try to manipulate it later in your code, long after the assignment statement has been executed.
An algorithmic error occurs when the program executes perfectly, but the result you receive is not what you expect. For instance, if you wrote a program to add two numbers, passed it 2 and 3, and received 6 as a result (with no error or warning messages) that would be an algorithmic error.
The main technique involved in debugging these types of bugs is to compute the answer to the problem you expect by some means other than MATLAB (or by using an example worked in a text on the subject) and stepping through the code. You can do so with the help of breakpoints, which are used to pause a program while it is executing. When the program is paused at a breakpoint, it is possible to check the state of the program where the breakpoint is set. By making sure whether the program computes the correct results at each step, one can eventually locate the error precisely.
For more information on debugging MATLAB files, please refer to the following documentation.