//  High order methods proposed by Hasan
// 50.	Hasan, Mohammed A., On the Derivation of Higher Order Root-finding Methods, 
// Proceedings of the 2007 American Control Conference, ThA07.3, 
// New York City, USA, July 11-13 2007 
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 NA5B3
{

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[] hasan47a(f_x f,double x)
{ // third order
double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-8;
double fx=0,dfx=0;
int i;
double x1=0,dfx1=0;
for(i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   x1=x-fx/(2.0*dfx);
   dfx1=f.dfunc(x1);
   x-=fx/dfx1;
  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 hasan47a 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[] hasan47b(f_x f,double x)
{ //third order
double a[]=new double[3];
int nmax=500;
double tolerance=1.0e-8;
double fx=0,dfx=0;
double x1=0,dfx1=0;
int i=0;
for(i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   x1=x-fx/dfx;
   dfx1=f.dfunc(x1);
   x-=2.0*fx/(dfx+dfx1);
  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 hasan47b 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[] hasan47c(f_x f,double x,double alfa)
{ //third order
double a[]=new double[3];
int nmax=500;
double beta=1-alfa;
double tolerance=1.0e-8;
double fx=0,dfx=0;
double x1=0,dfx1=0;
double x2=0,dfx2=0;
int i=0;
for(i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   x1=x-alfa*fx/dfx;
   dfx1=f.dfunc(x1);
   x2=x-beta*fx/dfx;
   dfx2=f.dfunc(x2);
   x-=2.0*fx/(dfx1+dfx2);
  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 hasan47c 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[] hasan43(f_x f,double x)
{ //fourth order
double a[]=new double[3];
int r=3;
int nmax=500;
double tolerance=1.0e-8;
double fx=0,dfx=0,d2fx=0;
int i=0;
for(i=0;i<nmax;i++)
{  fx=f.func(x);
   dfx=f.dfunc(x);
   d2fx=f.dfunc(x,2);
   x-=fx/(dfx+1.0/r*dfx*Math.log((dfx*dfx-fx*d2fx)/(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 hasan43 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[4][4];
     String s[]={"newton","hasan47a","hasan47b","hasan47c","hasan43"};
     r[0]= hasan47a(f,x0);
     r[1]= hasan47b(f,x0);
     r[2]= hasan47c(f,x0,0.5);
     r[3]= hasan43(f,x0);
     Text.printT(r,s,"2.11 HIGHER DERIVATIVE ORDER METHODS referance 50");
     } 
}
