Docsity

Computing Techniques - Test 1 Solutions | ME 2016, Exams of Computer Science

Toggle sidebar
bg1
ME2016A, Spring 2004, Dr. Ferri Name: _____Solution________
Test 1, February 4
This is a closed-book, closed-notes test. There are 4 problems for a total of 40 points.
Honor Pledge:
On my honor, I pledge that I have neither given nor received any inappropriate aid in the
preparation of this test.
_______________________________
Signature
Problem 1 (10) _________
Problem 2 ( 5) __________
Problem 3 (10) _________
Problem 4 (15) _________
Total (40) _____________
pf3
pf4
pf5

Partial preview of the text

Download Computing Techniques - Test 1 Solutions | ME 2016 and more Exams Computer Science in PDF only on Docsity!

ME2016A, Spring 2004, Dr. Ferri Name: _____ Solution ________ Test 1, February 4 This is a closed-book, closed-notes test. There are 4 problems for a total of 40 points.

Honor Pledge: On my honor, I pledge that I have neither given nor received any inappropriate aid in the preparation of this test.

_______________________________

Signature

Problem 1 (10) _________

Problem 2 ( 5) __________

Problem 3 (10) _________

Problem 4 (15) _________

Total (40) _____________

Consider a floating point machine that allocates 3 bits for the magnitude of the exponent, 3 bits for the magnitude of the mantissa, and 2 "sign bits." (A sign bit of 0 indicates a positive quantity, while a value of 1 indicates a negative quantity.) (a) (5 points) What is the spacing between the smallest positive number and the next smallest positive number on this computer? (b) (5 points) What is machine epsilon for this computer; i.e., what is the smallest number ε such that 1+ε > 1?

Solution The situation is as shown below

exponent

mantissa

sign bit for exponent

sign bit for mantissa

(a) The smallest number would have an exponent of e= –(1 1 1) 2 = -(7) 10 ; the smallest mantissa

would be m=(0.100) 2 , or (0.5) 10. (Recall that the first digit to the right of the decimal point in the mantissa must be equal to 1.) Therefore, the smallest positive number is 2-8^ = (1/256) 10.

The next smallest number would have m = (0.101) 2 , which is 2-10^ larger than the smallest positive number.

(b) If the mantissa has p digits, it can be shown that machine epsilon is given by ε = 2(1-p). In this case, p = 3, and ε = 0.

Consider the five points with connecting straight lines in the figure below. We want to calculate the sum of the lengths of the lines connecting the 5 points. (a) Write a Matlab function ‘ dist ’ that calculates the distance between a “left point” (xL, yL) and a “right point” (xR, yR). (i.e., give all of the lines of code needed within a text file called dist.m) The format of the call to dist must take the following form:

» d=dist(xL,yL,xR,yR);

(b) Write a Matlab script (program) that calls the function dist from part (a) and calculates the total length of the line segments connecting the 5 points. Your code must include a loop, so that it can easily accommodate any number of points. Assume that the x and y-coordinates are contained in 1x5 vectors x_vec and y_vec , respectively.

(x 1 , y 1 )

(x 2 , y 2 )

(x 3 , y 3 )

(x 4 , y 4 )

(x 5 , y 5 )

X

Y

(a) One possible version of the text file dist.m is

% program returns the distance between the % points (xL,yL) and (xR,yR) function d = dist(xL,yL,xR,yR); rise = yR-yL; run = xR-xL; d = sqrt( rise^2 + run^2);

(b) There are many possibilities, but one example is as follows:

% Program calculates the total length of the % straight lines connecting a series of points % The x and y-coords are contained in vectors % x_vec and y_vec, respectively n=length(x_vec); sum = 0; for k = 1:(n-1); xL = x_vec(k); xR = x_vec(k+1); yL = y_vec(k); yR = y_vec(k+1); d = dist(xL,yL,xR,yR); sum = sum + d; end

A 10 Newton weight is supported beneath a nonlinear spring. The tension in the spring in Newtons

is given by 2x + 3x2, where x is the spring’s elongation in meters. Setting the force in the spring equal to the weight gives the following equation that governs x:

f ( x )= 2 x + 3 x^2 − 10 = 0

It is easily seen that the root that we are seeking is between 1 and 2. (a) If a bisection algorithm is applied with initial values xL=1 and xU=2, what will be the maximum error between xr and the actual root at the end of 5 iterations? (b) Do you think that a fixed-point algorithm will be effective in determining the solution for x? Why or why not? Will the algorithm converge or diverge? (c) Starting with an initial guess of x=1, perform 2 iterations of Newton-Raphson. Show all of your work.

Solution

(a) The initial interval is of length ∆x = xU – xL = 2 – 1 = 1. After 1 iteration, xr = 1.5, and the

maximum error is ∆x/2. After 2 iterations, the maximum error is halved again to ∆x/22. Similarly, after n iterations, the maximum error will be ∆x/2n. Therefore, the maximum error after 5 iterations of the bisection method is ∆x/25 = 1/25 = 1/32 = 0.03125.

(b) For the fixed-point method, define g ( x )= f ( x )+ x = 3 x + 3 x^2 − 10. Convergence depends on the derivative of g(x)

( x x ) x

dx

d g dx

dg = ′= 3 + 3 2 − 10 = 3 + 6

Since x is somewhere between 1 and 2, the derivative is in the range of 9 < g′ < 15. Since g ′> 1 , the fixed-point method will not converge.

(c) The Newton-Raphson algorithm is

1 i

i i i f x

f x x x ′

  • = − ,^ where^ f^ ′( x^ )=^2 +^6 x

The result after 2 iterations is x 2 = 1.5253. The complete set of results for the first 4 iterations is given in the table below. Note the rapid convergence!

iteration x f df/dx x new 1 1.0000 -5.0000 8.0000 1. 2 1.6250 1.1719 11.7500 1. 3 1.5253 0.0298 11.1516 1. 4 1.5226 0.0000 11.1355 1.