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:
MATLAB provides three main methods of adding comments to your code:
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
Hello, margreet docter. Congratulations on solving the Problem. I hope you won't mind if I make a few observations. . . . (1) The function you have used on line 12 [the first line inside your loop] can also take two inputs. Have a look at the documentation for the function to see another syntax that would achieve the same thing. . . . (2) If you care about Cody "size", you could delete lines 14 & 19 in a resubmission, as they are only echoing values to the screen to assist in debugging. . . . (3) In this sort of problem, where a focus is on precision and decimal places, you might sometimes find it helpful to use the fprintf command to produce debugging output, with the syntax fprintf(formatSpec,A1,...,An), as this gives you more power to customise the format [e.g. scientific, or integer, or decimal; number of decimal places; etc.]. —DIV
Nice one :-)
4978 Solvers
202 Solvers
Getting the indices from a vector
1608 Solvers
640 Solvers
Matrix with different incremental runs
202 Solvers
Solution 1671361
If you describe a problem as simple, William, you'll invite close inspection.... Maybe you'll find a benefit in commenting the code in your next submission.
Touche! Yes, the new examples make the more extensive comments worthwhile.