Previous: Algebraic forms, Up: Examples   [Contents][Index]


3.2 Recursion

Following is an example of error propagation in a recursive function. The factorial of x is written as a recursive function f(x). Its derivative is given by The term in the parenthesis is also written as a recursive function df(x). It is shown that the propagated error in f(x) is equal to f(x)df(x)\delta x.

   >f(x) {if (x==1) return x; else return x*f(--x);}
   >df(x){if (x==1) return x; else return 1/x+df(--x);}
   >f(x=10pm1)
           3628800.00000 +/- 10628640.00000
   >(f(x)*df(x)*x.rms).val
           10628640.00000

Similarly, the recurrence relations for the Laguerre polynomial of order n and its derivative evaluated at x can be written as recursive functions. These are written in GNU fussy as l(n,x) and dl(n,x) and it is shown that the propagated error in L_n(x) is equal to L^\prime_n(x)\delta x.

   >l(n,x){
      if (n<=0) return 1;
      if (n==1) return 1-x;
      return ((2*n-1-x)*l(n-1,x)-(n-1)*l(n-2,x))/n;
    }
   >dl(n,x){return (n/x)*(l(n,x)-l(n-1,x));}
   >l(4,x=3pm1)
               1.37500 +/-    0.50000
   >(dl(4,x)*x.rms).val
           0.50000