//  High order methods Halley, Abbasbandy hiele1 and Thiele2  
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
//============= function to be defined ================

class fb extends f_x
{
   public double func (double x)
   { return x*x*x+3.6*x-36.4;}
}

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

public class NA5B1
{
public static double[] newton(f_x f,double x)
{
double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-8;
double fx,dfx;
for(int i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { a[0]=x;a[1]=fx;a[2]=i;return a;}
}
JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded in Newton Method\n"+
   " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE);
a[0]=x;a[1]=fx;a[2]=i;return a;
}
		
public static double[] halley(f_x f,double x)
{
int nmax=500;
double tolerance=1.0e-8;
double fx,dfx,fxm,dfxm,d2fx;
for(int i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   d2fx=f.dfunc(x,2);
   x-=2.0*fx*dfx/(2.0*dfx*dfx-fx*d2fx);
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded in Halley method\n"+
   " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE);
a[0]=x;a[1]=fx;a[2]=i;return a;
}

public static double[] abbasbandy(f_x f,double x)
{
int nmax=500;
double tolerance=1.0e-8;
double fx,dfx,d2fx,d3fx;
for(int i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   d2fx=f.dfunc(x,2);
   d3fx=f.dfunc(x,3);
   x-=(fx/dfx+fx*fx*d2fx/(2.0*dfx*dfx*dfx)+fx*fx*fx*d3fx/(6.0*dfx*dfx*dfx*dfx));
  if(Math.abs(fx)<tolerance) { a[0]=x;a[1]=fx;a[2]=i;return a;}
}
  JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded in Abbasbandy\n"+
   " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE);
  a[0]=x;a[1]=fx;a[2]=i;return a;
}   

public static double[] TM1(f_x f,double x)
{ //Thiele 1 method
int nmax=500;
double tolerance=1.0e-8;
double fx,dfx,d2fx,d3fx;
double P1,Q1;
for(int i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   d2fx=f.dfunc(x,2);
   d3fx=f.dfunc(x,3);
   P1=4.0*fx*dfx*(3.0*dfx*dfx*d2fx-3.0*fx*d2fx*d2fx+fx*dfx*d3fx);
   Q1=12.0*dfx*dfx*dfx*dfx*d2fx-18.0*fx*dfx*dfx*d2fx*d2fx+3.0*fx*fx*d2fx*d2fx*d2fx+4.0*fx*dfx*dfx*dfx*d3fx;
   x-=P1/Q1;
  if(Math.abs(fx)<tolerance) { a[0]=x;a[1]=fx;a[2]=i;return a;}
}
JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded in Thiele 1\n"+
   " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE);
{ a[0]=x;a[1]=fx;a[2]=i;return a;}
}

public static double[] TM2(f_x f,double x)
{ //Thiele 2 method
int nmax=500;
double tolerance=1.0e-8;
double fx,dfx,d2fx,d3fx;
double P2,Q2;
for(int i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   d2fx=f.dfunc(x,2);
   d3fx=f.dfunc(x,3);
   P2=fx*(6.0*dfx*dfx-3.0*fx*d2fx*d2fx+2*fx*dfx*d3fx);
   Q2=2.0*dfx*(3.0*dfx*dfx*d2fx-3.0*fx*d2fx*d2fx+fx*dfx*d3fx);
   x-=P2/Q2;
  if(Math.abs(fx)<tolerance) { a[0]=x;a[1]=fx;a[2]=i;return a;}
}
JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded in Thiele 2 method\n"+
   " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE);
 a[0]=x;a[1]=fx;a[2]=i;return a;
}

     public static void main (String args[])  
     { 
	 fb f=new fb();
	 dfb df=new dfb();   
     double x0;
     x0=Double.parseDouble(JOptionPane.showInputDialog(" first guess for x : "));
     double r[]=new double[5];
     String s[]={"Newton","Halley","Abbasbandy","Thiele 1","Thiele 2"};
     r[0]=newton(f,x0);
     r[1]= halley(f,x0);
     r[2]= abbasbandy(f,x0);
     r[3]= TM1(f,x0);
     r[4]= TM2(f,x0);
     Text.print(r,s,"2.11 HIGHER DERIVATIVE ORDER METHODS");
     } 
}
