A straight-forward way to learn MATLAB's number formats is to try them and see their effects.
Create a MATLAB .m or .mlx file with the following.
Assign this vector:
a = [-1/3, 0, 1/3, 2/3]
Enter each of the formats below, followed by the variable 'a', in order to see the effect of the format on a number.
format short e
format long e
format short eng
format long eng
format short g
format long g
format rat % Ratio
format short % Reset to the default format.
The effect of 'format long'
Result: -0.333333333333333 0 0.333333333333333 0.666666666666667
A more complete explanation is at: https://www.mathworks.com/help/matlab/ref/format.html
A. Create an m-file script for the Fibonnaci example in this section.
Call your file Fibonnaci_2_KennedyJ.m, but replace KennedyJ with your last name and 1st initial.
B. Start with Fibonnaci code of section 2.6.
Add this documentation to the top of the script file:
% Set n = the index of particular Fibonocci number to be computed (precondition)
C. The division in last line of the code can result in a number that not an exact integer.
This can be demonstrated changing the the format to display about 15 decimal places with this line of code:
Set n = 10 and run the script. The result will be
ans = 55.000000000000014
Fix this by changing the last line to be:
ans = round(diff / s5)
D. Test your script with n = 4, n = 5, and n =10
Note: n is a precondition for this script, so n needs to be in the command window, before running the the script.
This allows n to be set to various different value without changing the script.
Add texts here. Do not delete this text first.