// OPTIMISATION
//  2nd degree polynomial (Quadratic polynomial) optimisation
//  of function f(x) 
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

class fb extends f_x
{
   public double func (double x)
   { return -(1.0/(x*x-x+0.26)+1.0/(x*x-1.6*x+0.68))  ;}
}

public class NA32
{
public static double polinom2derece(f_x f,double x0,double x2)
{double x1=(x0+x2)/2.0;
 return polinom2derece(f,x0,x1,x2);
}
public static double polinom2derece(f_x f,double x0,double x1,double x2)
{
double epsilon=1.0e-10;
double delta=1.0e-5;
int print=0;
return polinom2derece(f,x0,x1,x2,epsilon,delta,print);
}
public static double polinom2derece(f_x f,double x0,double x1,double x2,double epsilon,double delta,int print)
{  
int maxit=100;    
double f0 = f.func(x0);
    double f1 = f.func(x1);
    double f2 = f.func(x2);
    double f3 = f2;
    double x3 = 0;
    double h = x1 - x0;
    double k=1;
    double dd=Math.abs(f1-f0);
    while ((dd >epsilon) || (h>delta))
    {
      k++;
      x3=(f0*(x1*x1-x2*x2)+f1*(x2*x2-x0*x0)+f2*(x0*x0-x1*x1))/
             (2*f0*(x1-x2)+2.0*f1*(x2-x0)+2.0*f2*(x0-x1));
      f3=f.func(x3); 
      System.out.println("k="+k+"x3="+x3+"f3="+f3);     
      if(x3 >= x0 && x3<x1)
        {x2=x1;f2=f1;x1=x3;f1=f3;}
      else if(x3 >= x1 && x3 <x2) 
        {x0=x1;f0=f1;x1=x3;f1=f3; }
      else if(x3 >x2) 
        {x0=x1;f0=f1;x1=x2;f1=f2;x2=x3;f2=f3; }
      else if(x3 < x0) 
        {x0=x3;f0=f3;x1=x0;f1=f0;x2=x1;f2=f1; }
      dd=Math.abs(f1-f0);
      h=Math.abs(x1-x0);
      if(k>maxit) break;          
    }//end of while
    if(print==1)
    {System.out.println("x  = "+x3+"f = "+f3+"hata x ="+h+"hata f(x)=y "+dd); }
    return x3;
}

     public static void main (String args[])  
     { 
	 fb f=new fb();    
     double x0,x1,x2;
     x0=Double.parseDouble(JOptionPane.showInputDialog(" enter lower limit of the function  x0 : "));
     x1=Double.parseDouble(JOptionPane.showInputDialog(" enter middle value of the function x1 : "));
     x2=Double.parseDouble(JOptionPane.showInputDialog(" enter upper limit of the function  x2 : "));
     double r;
     r= polinom2derece(f,x0,x1,x2,1.0e-10,1.0e-10,1);
    JOptionPane.showMessageDialog(null," optimisation value  : "+r+"\nFunction value : "+f.func(r),
     "Quadratic polynomial optimisation : ",JOptionPane.PLAIN_MESSAGE);
     System.exit(0);
     } 
}
