//  High order methods Olver,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-10;
double fx=0,dfx;
int i;
for(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)
{
	double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-10;
double fx=0,dfx,fxm,dfxm,d2fx;
int i;
for(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) { a[0]=x;a[1]=fx;a[2]=i;return a;}
}
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[] olver(f_x f,double x)
{ double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-8;
double fx=0,dfx,d2fx,d3fx;
int i;
for(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));
  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[] abbasbandy(f_x f,double x)
{
	double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-10;
double fx=0,dfx,d2fx,d3fx;
int i;
for(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
double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-10;
double fx=0,dfx,d2fx,d3fx;
double P1,Q1;
int i;
for(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
double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-10;
double fx=0,dfx,d2fx,d3fx;
double P2,Q2;
int i;
for(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*d2fx-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[6][3];
     String s[]={"Newton","Halley","olver","Abbasbandy","Thiele 1","Thiele 2"};
     r[0]=newton(f,x0);
     r[1]= halley(f,x0);
     r[2]=olver(f,x0);
     r[3]= abbasbandy(f,x0);
     r[4]= TM1(f,x0);
     r[5]= TM2(f,x0);
     Text.printT(r,s,"2.11 HIGHER DERIVATIVE ORDER METHODS");
     } 
}
