## Author: M. Turhan ÇOBAN
## Created: 2021-06-05

function   retval=cubic_poly_opt(f,x0,x1)
  nmax=500;
  tolerance=1.0e-15;
  h=0.00001;
  k=0;
  f0=f(x0);
  f1=f(x1);
  df0=df(f,x0,h);
  df1=df(f,x1,h);
  while abs(x0-x1)>tolerance && k<nmax
    k=k+1;
    X=1.0/(x1-x0);
    F=X*(f1-f0);
    G=X*(df1-df0);
    H=X*(F-df0);
    a=X*(G-2.0*H);
    b=3.0*H-G;
    c=df0;
    xs=x0+(sqrt(b*b-3.0*a*c)-b)/(3.0*a);
    fxs=f(xs);
    dfxs=df(f,xs,h);
  if dfxs > 0  
    x1=xs;f1=fxs;
    df1=dfxs;
  else          
    x0=xs;f0=fxs;
    df0=dfxs;
  end 
  endwhile
  retval=(x0+x1)/2.0;
endfunction
