Problem 44405. Back to basics: adding comments to your code
One of the most useful skills a programmer can develop is the skill — or habit — of writing descriptive comments within/above the code that they work on.
Top reasons for including comments in one's code:
- Helpful while you are initially developing the code.
- Helpful if you are collaborating on the development, so others need to be able to use/amend/revise it.
- Helpful if you need to revisit the code later on (could be months or years later), such as to extend it or amend it.
- Helpful if someone else may work on the code in future, such as to extend it or amend it.
- Helpful on Cody for other players who can learn from inspecting your code.
MATLAB provides three main methods of adding comments to your code:
- comments;
- comments also serving as 'code section' delimiters & titles (these may be more appropriate in scripts than within functions, but it is still possible to practice using them here);
- block comments.
Note: some other features of MATLAB that are related to comments are illustrated at the top of the Test Suite, but are outside the present scope. Specifically, ellipsis (...) is deemed here a technique primarily for line continuation, not primarily for adding descriptions to the code.
Your PRIMARY task is to utilize all three comment styles in your submission. Your comments will have to be of 'meaningful' length (i.e. not so short as to have no clear meaning).
However, to make it slightly more interesting — and to have something specific to comment upon — as a SECONDARY task you will also have to solve the equation
√(x³ − 3/2) = A
for x (find x), in which A is a constant that will be provided as an input. "√" represents the square-root radical sign. Note that generally x will not be an integer.
Moreover, your result should be returned rounded (in the usual way) to the minimum number of decimal places so that the left-hand side of the equation will be within the span defined by A ± 0.0001.
EXAMPLE
A = 8 x = 4.031
The exact value of x is more accurately approximated as 4.031008989457290. However, this is more than the minimum required precision. By setting x = 4.031, the left-hand side of the equation yields 7.999973, which is only 0.000027 less than the target value of A, and therefore acceptable. Note that more aggressive rounding of x to x = 4.03 would yield an unacceptably poor estimate for A, namely 7.996926 (an error of 0.003074). See also the table below:
x √(x³ − 3/2) A Error Error size 4.0310089895 8.00000000013 8 +1.3E-10 OK 4.031008990 8.00000000165 8 +1.7E-09 OK 4.03100899 8.00000000165 8 +1.7E-09 OK 4.0310090 8.00000003212 8 +3.2E-08 OK 4.031009 8.00000003212 8 +3.2E-08 OK 4.03101 8.00000307881 8 +3.1E-06 OK 4.0310 7.99997261189 8 -2.7E-05 OK 4.031 7.99997261189 8 -2.7E-05 OK <—— Most rounding yielding "OK" 4.03 7.99692609695 8 -3.1E-03 Too big 4.0 7.90569415042 8 -9.4E-02 Too big 4. 7.90569415042 8 -9.4E-02 Too big
Solution Stats
Solution Comments
Show commentsProblem Recent Solvers10
Suggested Problems
-
Program an exclusive OR operation with logical operators
711 Solvers
-
176 Solvers
-
109 Solvers
-
273 Solvers
-
Check that number is whole number
4201 Solvers
More from this Author32
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!