//
//#####################################################################
// Igor Yanovsky, UCLA                               February 25, 2006
//#####################################################################
//
// Created for Math 151A class
//
// This is an example of numerical differentiation being used.
// The function is f(x) = sin(x). We know that the derivative
// of this function is f'(x) = cos(x). By looking at the output,
// the derivative that we calculated using numerical differentiation
// is what we expected.


#include <iostream>
using namespace std;

#include <math.h>

#define PI 4.0*atan2(1.0,1.0)

int main( void )
{
	double h = 0.1;
	int sizeX = int(4*PI/0.1) + 1;

	double *x;
	double *f;
	double *fprime;

	x = new double[sizeX];
	f = new double[sizeX];
	fprime = new double[sizeX];

	int i;
	for( i = 0; i < sizeX; i++ )
	{
		x[i] = -2*PI + i*h;
		f[i] = sin(x[i]);
	}

	for( i = 0; i < sizeX; i++ )
	{
		fprime[i] = (f[i+1]-f[i])/h;
	}

	for( i = 0; i < sizeX; i++ )
	{
		cout << x[i] << "  " << f[i] << "  " << fprime[i] << endl;
	}
}
