next up previous
Next: Parametrized Figures and Animation Up: More Advanced Uses of Previous: Piecewise-Defined Functions

Finite Sums and Other Algorithms

Function definitions in  C may be as simple as an algebraic formula, or as complicated as an arbitrary finite ``rule''. Sums of finite series are achieved with the following sort of definition, which defines $ \mathrm{fourier9}(t)=\sum\limits_{k=1}^9\frac{1}{k}\sin kt$:


double fourier9(double t)
{
  int k; // summation index
  double y=0; // running total of the sum

  /* Run from k=1 to 9; increment k at each step */
  for (k=1; k < 10; ++k)

    // Add (1/k)sin(kt) to the running total
    y += (1.0/k)*sin(k*t); 

  return y;
}
The Weierstrass non-differentiable function sample file is an example.

Generally, a function definition may be specified by an arbitrary algorithm; see the definitions of gcd, sup, and inf in functions.cc for more examples. Of course, numerical error can become an issue if the algorithm is complicated.



hwang
2002-06-06