/*  ATK IV NUMERICAL PROGRAMMING (2006)  */
/* Excercise 2.4 - Cosine power series   */

#include<stdio.h>
#include<math.h>
#define Pi 3.141592654

double cosine(double x, int n); /* The actual function is defined in cosine.c */ 

/* The e0204.c and cosine.c codes are compiled and linked with command    */
/* cc e0204.c cosine.c -lm -o e0204.out                                   */

int main(void){
  double x,y,step;
  int i,n;

  n=20; /* Order of the cosine expansion */
  x=0.0;
  step = Pi/5.0;

  /* The series expansion and the "real" cosine values are printed when x=[0:2Pi]. */
  /* The step size for x is Pi/5.                                                  */

  for(i=0;i<=500;i++){
    y=cosine(x,n);
    printf("%f %f %f\n",x,y,cos(x));
    x+=step;
  }

  /* Try different values for n to see the effects of the order of the expansion. */
  /* Try also in cosine.c to calculate the series expansion with x instead of     */
  /* x(mod 2Pi). Note the effect on the convergence with large x.                 */ 

  return 0;
}