11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

212 • Chapter 5: Numerical <strong>Programming</strong> in MapleThe following procedure increments Digits by three and uses evalfto round the result to the proper number of digits before returning.> ‘evalf/MyFcn‘ := proc(xx::algebraic)> local x, term, s, old_s, xsqr, i;> x := evalf(xx);> Digits := Digits+3;> if type(x, numeric) and abs(x) xsqr := x^2;> term := evalf(1/6);> s := term;> old_s := 0;> for i from 1 while s old_s do> term := -term * xsqr / ((2*i+2)*(2*i+3));> old_s := s;> s := s + term;> end do;> else> s := evalf( (x-sin(x))/x^3 );> end if;> eval[Digits-3](s);> end proc:When you invoke evalf on an expression containing an unevaluatedcall to MyFcn, Maple invokes ‘evalf/MyFcn‘.> evalf( ’MyFcn’(0.000195) );0.1666666663498Recode the symbolic version of MyFcn so that it takes advantage of‘evalf/MyFcn‘ if the argument is a floating-point number.> MyFcn := proc(x::algebraic)> if type(x, float) then> evalf(’MyFcn’(x));> else> (x - sin(x)) / x^3;> end if;> end proc:The evalf command automatically looks for ‘evalf/MyFcn‘ whenused in the evalf(‘MyFcn‘) syntax.> MyFcn(0) := limit( MyFcn(x), x=0 );MyFcn(0) := 1 6

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

Saved successfully!

Ooh no, something went wrong!