//Numerical Analysis
// Muller method for root finding
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
//============= Function definition ================

class fb extends f_x
{
   public double func (double x)
   { 
	 double ff=10-20*(Math.exp(-0.15*x)-Math.exp(-0.5*x))-5;
	 return ff;
	 }
}
//===========================================================


public class NA10
{
public static double muller(f_x f,double x0,double x1,double x2)
{
//Finding real roots by using Muller method
//orta xr noktasından +/- h kadar giderek diğer 2 noktaya ulaşıyoruz.
int maxit=5;
double xr=x0;
int iter=0;
double es1=0.001;
double es=es1;
double ea=1.1*es;
double h0,h1;
double d0,d1;
double a,b,c;
double fx0,fx1,fx2;
double determinant;
double den;
double dxr;
while(iter<maxit && ea>es)
{
    fx0=f.func(x0);
    fx1=f.func(x1);
    fx2=f.func(x2);
    h0=x1-x0;
    h1=x2-x1;
    d0=(fx1-fx0)/h0;
    d1=(fx2-fx1)/h1;
    a=(d1-d0)/(h1+h0);
    b=a*h1+d1;
    c=fx2;
    determinant=b*b-4.0*a*c;
    if(determinant<=0) determinant=0;//we do not want to calculate complex roots here
    else determinant=Math.sqrt(determinant);
    if(Math.abs(b+determinant)>Math.abs(b-determinant)) {den=b+determinant;;}
    else {den=b-determinant;}
    dxr=-2*c/den;
    xr=x2+dxr;
    System.out.println("a="+a+"b="+b+"c="+c+"disc="+determinant+"xr="+xr+"iter="+iter);
    ea=Math.abs((xr-x2)/xr)*100;
    iter++;
    x0=x1;
    x1=x2;
    x2=xr;
} 
if(iter>=maxit) JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return xr;
}

     public static void main (String args[])  
     { 
	 fb f=new fb();   
     double x0,x1,x2;
     x0=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x0 : "));
     x1=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x1 : "));
     x2=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x2 : "));
     double r;
     r= muller(f,x0,x1,x2);
     JOptionPane.showMessageDialog(null," Value of the root  : "+r+"\nFunction value : "+f.func(r),
     "root estimation with Muller method : ",JOptionPane.PLAIN_MESSAGE);
     System.exit(0);
     } 
}