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.

200 • Chapter 5: Numerical <strong>Programming</strong> in Maple> else> evalf(expr);> end if;> end proc:> evaluate( Int(exp(x^3), x=0..1) );The evaluate procedure provides a model of how to write proceduresthat use hardware floating-point arithmetic whenever possible.Newton’s MethodThis section illustrates how to take advantage of hardware floating-pointarithmetic to calculate successive approximations using Newton’s method.You can use Newton’s method to find numerical solutions to equations.As section 2.2 describes, if x n is an approximate solution to the equationf(x) = 0, then x n+1 , given by the following formula, is typically a betterapproximation.x n+1 = x n − f(x n)f ′ (x n )Example The iterate procedure takes a function, f, its derivative, df,and an initial approximate solution, x0, as input to the equation f(x) = 0.The iteration procedure calculates at most N successive Newton iterationsuntil the difference between the new approximation and the previousone is small. The iterate procedure prints the sequence of approximationsto show successive approximations.> iterate := proc( f::procedure, df::procedure,> x0::numeric, N::posint )> local xold, xnew;> xold := x0;> xnew := evalf( xold - f(xold)/df(xold) );> to N-1 while abs(xnew-xold) > 10^(1-Digits) do> xold := xnew;> print(xold);> xnew := evalf( xold - f(xold)/df(xold) );> end do;> xnew;> end proc:The following procedure calculates the derivative of f and passes allthe necessary information to iterate.> Newton := proc( f::procedure, x0::numeric, N::posint )> local df;> df := D(f);> print(x0);

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

Saved successfully!

Ooh no, something went wrong!