Minimize the function \(f(x) = x^4 - 3 x^2 + x + 1\) from Example 30.3.1 using gradient descent with \(\beta = 0.1\) and initial point \(0\text{.}\)
Solution.
f = @(x) x.^4 - 3*x.^2 + x + 1;
fp = @(x) 4*x.^3 - 6*x + 1;
beta = 0.1;
a = 0;
max_tries = 10000;
for k = 1:max_tries
x = a - beta*fp(a);
if abs(x-a) < 100*eps(a)
break
end
a = x;
end
if k < max_tries
fprintf('Found x = %g with f(x) = %g after %d steps\n', x, f(x), k);
else
disp('Failed to converge')
end
The code takes more steps than Newton’s method in Example 30.3.1 but it actually minimizes the function.
