#include "chapter3.cpp"

typedef double runtype;

// function 1 is x^3 - 3x + 1
inline runtype f(runtype x) { return (pow(x,3) - 3*x + 1); }
inline runtype deriv_f(runtype x) { return (2*x*x - 3); }
// function 2 is x^3 - 2sin(x)
inline runtype g(runtype x) { return (pow(x,3) - 2*sin(x)); }
inline runtype deriv_g(runtype x) { return (2*x*x - 2*cos(x)); }

void main()
{
	ofstream outfile("ch3.txt");
	const int n_max = 25;
	const runtype epsilon	= 0.0000005;
	const runtype delta		= 0.0000001;
	runtype a, b;


// Test several different methods with the same function.
// Shows how many iterations are necessary to get an answer
	a = 0.0;
	b = 1.0;
	outfile << "First Function: f" << endl;
	outfile << "Bisection 1" << endl;
	bisection1(outfile,f,a,b,n_max);

	outfile << endl << "Bisection with check for convergence" << endl;
	bisection2(outfile,f,a,b,n_max,epsilon);

	outfile << endl << "Recursive Bisection" << endl;
	outfile << setw(5) << "n" << setw(15) << "c" << setw(15) << "f(c)";
	outfile << setw(15) << "error" << endl;
	bisection3(outfile,f,a,b,f(a),f(b),n_max,epsilon,0);

	outfile << endl << "Newton's method" << endl;
	newton(outfile,f,deriv_f,a,n_max,epsilon,delta);

	outfile << endl << "Secant Method" << endl;
	secant(outfile,f,a,b,n_max,epsilon);

	outfile << endl;


// Test several different methods with the same function.
// Shows how many iterations are necessary to get an answer
	a = 0.5;
	b = 2.0;
	outfile << endl << "Second Function: g" << endl;
	outfile << "Bisection 1" << endl;
	bisection1(outfile,g,a,b,n_max);

	outfile << endl << "Bisection with check for convergence" << endl;
	bisection2(outfile,g,a,b,n_max,epsilon);


	outfile << endl << "Recursive Bisection" << endl;
	outfile << setw(5) << "n" << setw(15) << "c" << setw(15) << "f(c)";
	outfile << setw(15) << "error" << endl;
	bisection3(outfile,g,a,b,f(a),f(b),n_max,epsilon,0);

	outfile << endl << "Newton's Method" << endl;
	newton(outfile,g,deriv_g,a,n_max,epsilon,delta);

	outfile << endl << "Secant Method" << endl;
	secant(outfile,g,a,b,n_max,epsilon);
}