University of Waterloo
Faculty of Engineering
Department of Electrical and Computer Engineering
ECE 204A
Pre-Laboratory 5
Prepared by
Surname/Last Name, Legal Given/First Name(s)
UW Student ID Number: 2NNNNNNN
UW User ID: uwuserid @uwaterloo.ca
2A Electrical/Computer Engineering
18 September 2015
5.1.1a For each row in Table 1, convert the given numbers to the other two formats.
Table 1. Binary, decimal, and hexadecimal numbers.
Binary / Decimal / Hexadecimal10001 / ? / ?
? / 23 / ?
? / ? / 2B
101010 / ? / ?
? / 65 / ?
? / ? / AA
5.1.1b Add the following hexadecimal numbers without converting them to decimal first.
A3825
+ 1F539
?
38925
+ 93295
?
ACFEB
+ CDAEF
?
5.1.2a If the first hexadecimal digit of a double-precision floating-point number is A, is that number positive or negative?
5.1.2.1a The sign bit indicating a positive number is
0 or 1.
5.1.2.2a If the first three hexadecimal digits of a double are 3E7, what is the corresponding power of 2?
Your answer.
5.1.2.2a If the first three hexadecimal digits of a double are C03, what is the corresponding power of 2?
Your answer.
5.1.2.3a Why is it unnecessary to store the leading 1?
Your explanation here.
5.1.2.4a One justification for denormalized numbers is that for two doubles, x and y, the difference x – y is never zero unless x = y. Confirm this by seeing the output of the following code:
> format hex
> 5e-300
> 5e-300 - 5.00000000001e-300
Copy and paste your Matlab commands and the output here.
5.1.2.4b Another justification for denormalized numbers is that it would be absurd to have a double x on the order of 2-1023 to be stored with 53 bits of precision, but to then have x/2 equal zero. The cost, however, is that real numbers less than 2-1022 are no longer stored with 53 bits of precision. For example, how many bits of precision are used to store 10-320 (be sure to use format hex)?
Your answer here.
5.1.2.5a Fill in the second row of Table 2 with NaN or Inf.
Table 2. Various Matalb calculations with infinity.
Caclulation / Inf - Inf / Inf + Inf / Inf/0 / Inf/Inf / Inf^2Result / ? / ? / ? / ? / ?
5.1.2.5b Is it ever possible to perform a calculation with Inf and get a value that is neither infinity nor not-a-number? If yes, give an example.
Your answer here.
5.1.2.6a Which of the following doubles (represented using hexadecimal digits) is larger?
52832e8d36bf5eed or 52832e8d86bf5eed
Your answer here.
5.1.2.7a Consider the following:
> M = randn( 3 )
M =
2.7694 0.7254 -0.2050
-1.3499 -0.0631 -0.1241
3.0349 0.7147 1.4897
> M >= 0
ans =
1 1 0
0 0 0
1 1 1
Use this to devise a test on a matrix M to determine
1. whether or not all entries in the matrix are positive, and
2. whether or not there is at least one negative entry (< 0) in the matrix.
Copy and paste your Matlab commands here.
5.1.2.8a Create a while loop that finds the first integer k such that 1/k < p/1000.
Copy and paste your Matlab commands and the output here.
5.2.1a The exact value of x2 + 1574359x – 1.912846 when x = 0.000001215 is 0.000000185001476225. What is the relative error of Matlab’s approximation when you calculate
> x = 0.000001215;
> x^2 + x*1574359 - 1.912846
Copy and paste your Matlab commands and the output here.
5.2.2a What is the Newton polynomial that passes through the three points (3.2, 0.5),
(5.4, 2.7) and (8.3, 3.6)?
? + ?(x – 3.2) + ?(x – 3.2)(x – 5.4)
5.2.2b Why does the Newton polynomial in Question 5.2.2a not have a term of the form
(x – 3.2)(x – 5.4)(x – 8.3)?
Your answer here.
5.2.3a Save the program newtonpoly and use it to find the Newton polynomial passing through:
1. The point (3.2, 4.9);
2. The points (3.2, 4.9) and (5.3, 8.1);
3. The points (3.2, 4.9), (5.3, 8.1) and (6.4, 7.0); and
4. The points (3.2, 4.9), (5.3, 8.1), (6.4, 7.0) and (10.5, 2.8).
Copy and paste your Matlab commands and the output here.
5.2.4a Write a Matlab function that implements Horner’s rule as described in Section 5.2.4.
function [y] = horners( a, x )
% enter your implementation here
end
Your implementation must, in addition to evaluating the polynomial defined by a at the point x, allow the user to evaluate the polynomial at each point of a vector or matrix.
Your function must have the following output:
> format long
> horners( [1 2 3], 3.2 )
ans =
19.640000000000001
> horners( [1 2 3], 3.5 )
ans =
22.250000000000000
Your function should also be able to perform the following:
> format
> horners( [1 2 3], 0:0.1:1 )
ans =
3.00 3.21 3.44 3.69 3.96 4.25 4.56 4.89 5.24 5.61 6.00
If we define xs to be 100 equally spaced points on the interval [ –3, 3], we can also plot the polynomial on that interval:
xs = linspace( -3, 3, 100 );
ys = horners( [1 2 3], xs );
plot( xs, ys );
ylim( [min( ys ) - 1, max( ys ) + 1] );
title('The polynomial x^2 + 2x + 3 plotted on the interval [-3, 3]');
The output of this sequence of commands should be a plot similar to Figure 1.
Figure 1. The output of Matlab when plotting a polynomial.
You will modify this function in the laboratory.
5.2.4b What is the purpose of the statement
ylim([min(ys)-1,max(ys)+1]);
?
Your answer here.
6