#include "chapter4.cpp"

typedef double runtype;
typedef std::vector<runtype> Array;
typedef std::vector<Array> Matrix;

inline runtype f(runtype x) { return (sin(x)); }

ofstream outfile1("polynomial.txt");

void main()
{
	int n = 9;
	Array x(n+1, 0);
	Array y(n+1, 0);
	Array a(n+1, 0);
	int j, k;
	runtype e, h, p, t;

	h = 1.6875 / n;

	for (k=0; k<=n; k++)
	{
		x[k] = k*h;
		y[k] = f(x[k]);
	}

	coef(n, x, y, a);

	outfile1 << setw(3) << "j" << setw(15) << "t" << setw(15) << "p";
	outfile1 << setw(15) << "|sin(t) - p|" << endl;
	for (j = 0; j <= 4.0*n; j++)
	{
		t = j*h/4.0;
		p = eval(n, x, a, t);
		e = fabs(sin(t) - p);
		outfile1 << setw(3) << j << setw(15) << t << setw(15) << p << setw(15) << e << endl;
	}

ofstream outfile2("deriv.txt");

	n = 10;
	h = 1.0;
	runtype x2 = 1.2309594154;

	Matrix d(n+1, Array(n+1, 0.0));

	deriv(f, x2, n, h, d);
	outfile2.precision(10);
	outfile2 << "Estimating derivatives using Richardson's Extrapolation" << endl;
	for (int i = 0; i <= n; i++)
	{
		for (int j = 0; j <= n; j++)
			outfile2 << setw(15) << d[i][j];
		outfile2 << endl;
	}
}