2. The assert function is a built-in function that generates an error when a condition is violated.
For example:
assert(isequal(myprod,myprod_test),'Check your expression for myprod')
Here, myprod is the student’s answer and myprod_test is the correct answer to check it against.
This statement checks to see if the variables myprod and myprod_test hold the same value. If
they do (so the condition is true), nothing happens. If the values are different (so the condition
is false), it asserts an error and provides a feedback statement of “Check your expression for
myprod”.
In addition to the assert function, you will need to use some string functions. Please refer to
your book, notes, and online resources to find helpful functions.
Additional Guidance
To understand the nature of the problem even better, you should play around with this
problem on Edge (see: Week 3, WS3 Edge Submit Problems, the 4th
problem titled
“Difference”). Try to make intentional mistakes and see what happens! For instance, what
happens if you use the diff function? What if the word “diff” is used in a comment? Playing
around with it on Edge should help you out with writing your solution.
Some sample M-files have been provided as well. These should help you test your solution (you
can find these on Edge):
1. correct_answer.m - A perfect, vectorized solution
2. uses_diff.m - A correct solution that uses the diff function
3. incorrect_answer.m - An incorrect solution that attempts to use a for loop
4. assert_sample.m - A quick example of how the assert function is used
Note: Both examples 1 and 2 contain “diff” in a comment.
You may also find the following useful:
1. fileread() - a function useful for reading the entire file as a string
2. char(10) - the newline character that separates each line in the file. Comments will
always be on separate lines from actual code to making parsing out/ignoring comments
easier.