// Stephensen metod x=g(x) 

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

class fb extends f_x
{  //x = g(x) function 
   //f(x)=(5/(x+1))^0.5-x
   public double func (double x)
   { return Math.sqrt(5.0/(x+1));}
}

//===========================================================
public class NA4
{
 	
public static double stephensen(f_x f,double x0)
{
//Stephensen root finding method
double es,ea;
int maxit=100,iter=0;
es=0.000001;
ea=1.1*es;
double x,x1,x2;
x=0;
while(iter<maxit && ea>es)
{
x1=f.func(x0);
x2=f.func(x1);
x=x0-(x1-x0)*(x1-x0)/(x2-2*x1+x0);
ea=Math.abs(x-x0)*100.0;
x0=x;
iter++;
}
if(iter>=maxit) JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+
   " result might not be valid","MAKSİMUM NUMBER OF ITERATION WARNING",JOptionPane.WARNING_MESSAGE); 
return x;
}

     public static void main (String args[])  
     { 
	 fb g=new fb();    
     double xa,xu;
     xa=Double.parseDouble(JOptionPane.showInputDialog(" first guess for the root value : "));
     double r= stephensen(g,xa);
     double fr=g.func(r);
     String s=" root value  : "+r+"\ng(x) = "+fr+"\nf(x)=g(x)-x = "+(fr-r);     
     JOptionPane.showMessageDialog(null,s,
     "Stephensen root finding method : ",JOptionPane.PLAIN_MESSAGE);
     } 
}
