/*
 *   Newton-Raphson algorithme:
 *
 *      find the root x : f(x)==0
 *
 *   Givet x: f(z) z "tæt" på x ved
 *
 *    y(z) = f(x)+f'(x)*(z-x)
 *
 *   f(x) == 0,  y(z)==0.
 *
 *    z == x -f(x)/f'(x)
 *
 *   start gæt: x[0]
 *
 *    x[n+1]= x[n]-f(x[n])/f'(x[n])
 *
 *
 *  Note: A function can have many roots, which one we find depend on the starting position.
 *        Strange things can happen if during the iteration you hit a point where f'(x) is very small, since you divide by the derivative.
 */

#include <cmath>
#include <iostream>
using namespace std;

double f(double x)
{
   return sin(2*M_PI*x)*sin(2*M_PI*x);
}

double fprime(double x)
{
// Implement code providing an approximation for df/dx evaluated at x.
}

int main()
{
   double x=0.25;         // Starting point
   double err=1e-5;      // Threshold for finding root
   int nmax=100;

   int iteration=0;
   while ( fabs(f(x)) > err  && iteration<nmax)
     {
        x+=-f(x)/fprime(x);
        cout << iteration << " " << x << " " << f(x) << "\n";
        iteration++;
     }

   if (iteration==nmax) cout << "No solution found!" << endl;
                   else cout << "Solution f("<<x<<")="<<f(x)<<"\n";

   return 0;
}

