//  Wegstein method x=g(x) 
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

public class NA4A
{	
public static double Wegstein(f_x f,double x0)
{
//Wegstein root finding method
double es,ea;
int maxit=100,iter=0;
es=0.000001;
ea=1.1*es;
double x1,x2;
double g0,g1,g2;
x2=0;
g0=x1=f.func(x0);
while(iter<maxit && ea>es)
{
g1=f.func(x1);	
x2=(x0*g1-x1*g0)/((x0-g0)-(x1-g1));
ea=Math.abs(x2-x1)*100.0;
x0=x1;
x1=x2;
g0=g1;
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 x2;
}
     public static void main (String args[])  
     { 

     double xa,xu;
     xa=Double.parseDouble(JOptionPane.showInputDialog(" first guess for the root value : "));
     double r= Wegstein(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,
     "Wegstein root finding method : ",JOptionPane.PLAIN_MESSAGE);
     } 
}
