8.1: Number formats
- Page ID
- 87355
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 long
a
format bank
a
format short e
a
format long e
a
format short eng
a
format long eng
a
format short g
a
format long g
a
format rat % Ratio
a
format short % Reset to the default format.
- Answer
-
The effect of 'format long'
aResult: -0.333333333333333 0 0.333333333333333 0.666666666666667
A more complete explanation is at: Mathworks format help [www.mathworks.com]
.
Fibonnaci_2
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:
format long
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.
- Answer
-
Add texts here. Do not delete this text first.
.