{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw2_solutions - Due CS 257(Luke Olson Homework#2 Solutions...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Due: September 11, 2008 CS 257 (Luke Olson): Homework #2 Solutions Problem 1 [Range Reduction] We are developing code for a financial firm and are required to compute tan (10 n ) for various n as a component of our financial model. Use range reduction to show how to compute tan (10 n ) for small n ( n < 5). What happens for large n ( n = 20, 50, 100)? Hint: (Think of the role of the less significant digits in a number, when that number is multiplied by a large number). Hand in pseudocode or matlab code to compute tan (10 4 ) and discuss in a couple of sentences the problem of computing tan (10 20 ). Solution The tan function is π periodic. To use range reduction, we have to subtract off integer multiples of π , until the value lies in the range 0 to π . Using this strategy to compute tan (10 n ), we find an integer k such that 0 (10 n - ) < π . Assume we want to calculate (10 n - ) up to two decimal places of accuracy , for n < 5. Since k 10 4 , the digit of π that is six places to the right of the decimal point may be two places to the right of the decimal point in . Therefore we need to use a value of π which is accurate up to 6 decimal places.(Using matlab value of pi is fine too. Thats accurate upto 15 decimal digits). Listing 1: Code to compute tan (10 4 ) 1 2 %Value of \pi accurate upto 6 decimal places 3 mypi = 3.141593; 4 x = 10ˆ4; 5 %Subtract off integer multiples 6 while (x > mypi) 7 x = x - mypi; 8 end 9 %calcute the tan now 10 tan_x = tan (x); 11 fprintf ( ’tan(10ˆ4) = tan(%g) = %g\n’ , x,tan_x); As n gets large, we need more and more digits of accuracy of π to compute (10 n - ) up to desired accuracy. In double precision, we can have 52 bits for representation of
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}