function [x_opt,y] = mynewton(x,fun,xmin,xmax);

x_last = x;

if nargin > 2
    figure;
    axis([xmin,xmax,-5,5]);
    hold on;
    myshowfun(xmin, xmax, fun);
end;

while 1
    [y,g,h] = feval(fun, x);
    
    if nargin > 2
        xpoints = xmin:0.1:xmax;
        ypoints = zeros(length(xpoints));
        i = 1;
        for xq = xpoints
            ypoints(i) = (xq - x)^2*h + (xq-x)*g + y;
            i = i + 1;
        end;
        last_q = plot(xpoints, ypoints,'Color','red');

        pause;
        delete(last_q);
    end
    
    x_last = x;
    x = x - g/h;
  
    if abs(x - x_last) < 1e-6
        x_opt = x;
        return;
    end;
end;
