import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
//============= Tanımı gereken fonksiyon ================

class fc extends f_x
{
public double func(double x)
{return x*x*x-2.0;}

}


//===========================================================

public class NA12
{
public static double inverseinter2Droot(f_x f,double x1,double x3)
{double x2=(x1+x3)/2.0;
return inverseinter2Droot(f,x1,x2,x3);
}
	
public static double inverseinter2Droot(f_x f,double x1,double x2,double x3)
{
// Inverse quadratic Lagrange interpolation
double test;
double p=0;
double es,ea;
double f1,f2,f3,fp;
int maxit=100,iter=0;
double tol=1.0e-10;
if(x1==0) x1=1.0e-10;
if(x2==0) x2=1.0e-10;
if(x3==0) x3=1.0e-10;
es=0.00000001;
ea=0.001;
f1=f.func(x1);
f2=f.func(x2);
f3=f.func(x3);
if(f1==0) return x1;
else if(f2==0) return x2;
else if(f3==0) return x3; 
if(f1*f3>0) System.out.println("Root is not existed in the given region");
while((ea>es)&&(iter<maxit))
{	p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1));
    fp=f.func(p);
    ea=Math.abs(x3-p);
	if(Math.abs(f3)<tol) return x3;        
    if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;}
    else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;}
    iter=iter+1;
    System.out.println("iter="+iter+"p="+p+"fp="+fp);
} 
if(iter>=maxit) JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+
   " solution might not be valid","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return p;
}
     public static void main (String args[])  
     { 
	 fc f=new fc();
     double x1,x2,x3;
     x1=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x1 : "));
     x3=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x2 : "));
     double r;
     r= inverseinter2Droot(f,x1,x3);
     JOptionPane.showMessageDialog(null," root value  : "+r+"\nFunction value : "+f.func(r),
     "Quadratic inverse Lagrange Interpolation method : ",JOptionPane.PLAIN_MESSAGE);
     fa f1=new fa();
     double r1= inverseinter2Droot(f1,x1,x3);
          JOptionPane.showMessageDialog(null," root value  : "+r1+"\nFunction value : "+f1.func(r1),
     "Quadratic inverse Lagrange Interpolation method : ",JOptionPane.PLAIN_MESSAGE);
     } 
}