function res = f(x)
  res = x^4 + x^3 - 3*x^2 + 2
endfunction

function res = Df(x)
  res = 4*x^3 + 3*x^2 - 6*x
endfunction

function res = Ddf(x)
  res = 12*x^2 + 6*x - 6
endfunction

function res = backtracking(d,x,var_alpha,var_beta)
  var_gamma = 1;
  while(f(x + var_gamma * d) > (f(x) + var_alpha * var_gamma * Df(x) * d))
    temp = var_gamma;
    var_gamma = var_beta * temp;
  end
  res = var_gamma;
endfunction

function res = gradient(x,iter,var_alpha,var_beta)
  timer();
  for i=1:iter
    temp = x;
    d = -Df(x);
    var_gamma = backtracking(d,x,var_alpha,var_beta);
    x = temp + var_gamma * d;
  end
  t = timer();
  printf("Computation time: %f seconds\n", t);
  printf("result = %f\n", x);
  res = x
endfunction

function res = newton(x,iter)
  timer();
  for i=1:iter
    temp = x;
    d = -inv(Ddf(x)) * Df(x);
    x = temp + d;
  end
  t = timer();
  printf("Computation time: %f seconds\n", t);
  printf("result = %f\n", x);
  res = x
endfunction

