///Numerical Analysis
// 
//  
// Secant method with one root estimation 
// quadratic difference formula 
// 

import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

//============= function definition ================

class fb extends f_x
{
   public double func (double x)
   { return x*x*x-2;}
}
//===========================================================

public class NA9
{

public static double df(f_x f,double x,double h)
{ // derivative formula
  double hh=1.0/h; 
  return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh;
}
	
public static double secant(f_x f,double x)
{
int nmax=500;
double tolerance=1.0e-15;
double fx,dfx;
for(int i=0;i<nmax;i++)
{
  fx=f.func(x);
  dfx=df(f,x,0.00001);
  x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return x;
}

     public static void main (String args[])  
     {  
	 fb f=new fb(); 
     double x0;
     x0=Double.parseDouble(JOptionPane.showInputDialog(" First estimation for the root : "));
     double r= secant(f,x0);
     JOptionPane.showMessageDialog(null," the root : "+r+"\nFunction value : "+f.func(r),
     "root of the function by Secant method : ",JOptionPane.PLAIN_MESSAGE);
     } 
}