next up previous
Next: Finite Sums and Other Up: More Advanced Uses of Previous: More Advanced Uses of

Piecewise-Defined Functions

The syntax for defining a function such as $ f=\max(\sin,\cos)$ is:


double f(double t)
{
  if (cos(t) <= sin(t))
    return sin(t);
  else 
    return cos(t);
}
If there are more than two formulas in the definition, use an else if construction, as in

double max_10_f(double x)
{
  if ( fabs(f(x)) < 10 ) // |f(x)| < 10
    return f(x);
  else if ( f(x)>0 )
    return 10;
  else
    return -10;
}
This truncates $ f$ at the lines $ y=\pm10$. (The function $ f$ must be defined separately.)



hwang
2002-06-06