11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

iterate(f, df, x0, N);> end proc:Use Newton to solve the equation x 2 − 2 = 0.> f := x -> x^2 - 2;5.2 Hardware Floating-Point Numbers • 201f := x → x 2 − 2> Newton(f, 1.5, 15);1.51.4166666671.4142156861.4142135621.414213562Example This version of Newton uses hardware floating-point arithmeticif possible. Since iterate only tries to find a solution to an accuracy of10^(1-Digits), Newton uses evalf to round the result of the hardwarefloating-point computation to an appropriate number of digits.> Newton := proc( f::procedure, x0::numeric, N::posint )> local df, result;> df := D(f);> print(x0);> if Digits < evalhf(Digits) then> try> return evalf(evalhf(iterate(f, df, x0, N)));> catch:> end try;> else> iterate(f, df, x0, N);> end if;> end proc:Newton uses hardware floating-point arithmetic for the iterations androunds the result to software precision. Hardware floating-point numbershave more digits than the software floating-point numbers, giventhe present setting of Digits.> Newton(f, 1.5, 15);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!