A) Write a MATLAB function that computes precision for a given reference value.
ID: 3783844 • Letter: A
Question
A) Write a MATLAB function that computes precision for a given reference value.
A prototype of the function is given here:
function [precision] = PrecisionVersusBase( base ) % returns precision
The algorithm for computing the precision is given in the pseudo code below
precision = base
WHILE( base + precision > base )
precision = precision / 2
precision = precision * 2
B)
Using the function from part A, compute precision for a reference value equal to 1.0 to
1e-30, reducing it by a factor of 10 each step. ( base = base/10.0; ) Write out the results of each case to a Comma Separated Values (CSV) file*. Write out theprecision, and the base, along with the ratio of precision and base, in other words writeout base, precision and precision/base. Document these results in your report.
First, plot the base versus precision and then plot base versus the ratio. Consider theform and style of plot you are using, especially the x-axis. Due to the geometricnature in the progression of base, base/10 at each step, a log scale might be helpful.The MATLAB function loglog is the same as plot, except each axis (x and y) will use a log scale.
Explanation / Answer
Use double-precision to store values greater than approximately 3.4 x 1038 or less than approximately -3.4 x 1038. For numbers that lie between these two limits, you can use either double- or single-precision, but single requires less memory.
Creating Double-Precision Data
Because the default numeric type for MATLAB is double, you can create a double with a simple assignment statement:
The whos function shows that MATLAB has created a 1-by-1 array of type double for the value you just stored in x:
Use isfloat if you just want to verify that x is a floating-point number. This function returns logical 1 (true) if the input is a floating-point number, and logical 0 (false) otherwise:
You can convert other numeric data, characters or strings, and logical data to double precision using the MATLAB function, double. This example converts a signed integer to double-precision floating point:
Creating Single-Precision Data
Because MATLAB stores numeric data as a double by default, you need to use the single conversion function to create a single-precision number:
The whos function returns the attributes of variable x in a structure. The bytes field of this structure shows that when x is stored as a single, it requires just 4 bytes compared with the 8 bytes to store it as a double:
You can convert other numeric data, characters or strings, and logical data to single precision using the single function. This example converts a signed integer to single-precision floating point:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.