Task: Find and correct the errors in the script from this link.
It is all too easy to make errors with computer programs, such as the ones you will create for this class. The hardest part of learning to use a computer program is to find and correct these errors. Sometimes the errors are due to a misunderstanding of the commands and the command structure, which is a common source of errors for beginning users. Most often, the errors are clerical; that is, mis-spelling a variable or command name, forgetting to put a needed character somewhere, or having a stray character in the script.
These errors produce two types of problems. In many cases, the error produces an incorrect syntax for the program and the runtime system stops the program because it does not understand what you want done. These errors are typically easy to find because the MATLAB runtime system will print a message in the command window identifying where the script has a problem.
The second type of error does not cause the program to stop with an error, but produces an incorrect value for a calculation. This type of error is very hard to detect and fix. Look at the results of your programs and ask if they make sense or seem plausible.
MATLAB checks for errors in two ways. It first scans the script to see that all of the commands are complete (checks that the command syntax is correct). These errors could be
You should be aware, that the syntax checker stops when it finds a problem. There could be another statement with a problem. If you fix one syntax error, another might appear when you run the script again.
If the script passes the syntax check, then the procedure is run starting at the first line. MATLAB is a sequential calculator; it executes the script one line at a time in the order that they are provided (unless the program causes lines to be skipped). New kinds of errors can appear at this point. These errors might be
Even though the line for the error is provided, the reason for the error could be in another, earlier, line.
Finding errors is not easy. It requires that you become a detective and evaluate in detail what the script is trying to do. It is sometimes useful to use disp() to print variable values while the program runs to make sure the values are correct/appropriate. Using whos VAR in the script to provide details about a variable can help direct you to the line/value causing the error.
Recent versions of MATLAB sometimes make suggestions for how to correct an error. They are mostly useful, but sometimes they send you in the wrong direction to find the error.
With practice, you can develop the skill of debugging (finding errors in) programs. This is the hardest programming skill to develop and it is the most frustrating.
Solution to the task:
(I strongly recommend that you run the original script and see how MATLAB responds to each of these errors)
% script26.m % find the errors in this script % original script % script26.m % find the errors in this script % A=[ 1 2 3 4; dsp(num2str(A)); b=a^2; disp('the values of b are ' b) c=2 a; x=2*y+10; disp(num2str(A(5))); % corrected script % A=[ 1 2 3 4; % missing closing bracket %(corrected) A=[ 1 2 3 4]; dsp(num2str(A)); % disp is mis-spelled %(corrected) disp(num2str(A)); b=a^2; % variable a is not defined. MATLAB is case sensitive %(corrected) b = A^2; disp('the values of b are ' b) % disp() needs a single string, use concatenation % b is not defined; needs to be a string %(corrected) disp(['the values of b are ' num2str(b)]) c=2 a; % missing arithmetic operator %(corrected) c=2-a; x=2*y+10; % variable y is not defined %(corrected) x=2*A+10; (or some other expression with a known variable; or define y disp(num2str(A(5))); % variable A has only 4 values, A(5) is not defined %(corrected) disp(num2str(A(3)));