Hi,
Following code Illustrates the working of Gradient Descent for 3 variables.
To eliminate error changes were made to:
- Initial value
- Maxiter value
- Alpha value
function [xopt,fopt,niter,gnorm,dx] = grad_descent(varargin)
if nargin==0
x0 = [3 3 3]';
elseif nargin==1
x0 = varargin{1};
else
error('Incorrect number of input arguments.')
end
tol = 1e-6;
maxiter = 100000;
dxmin = 1e-6;
alpha = 0.000001;
gnorm = inf; x = x0; niter = 0; dx = inf;
f = @(x1,x2,x3) 4*(x1.^2 + x2-x3).^2 +10;
f2 = @(x) f(x(1),x(2),x(3));
while and(gnorm>=tol, and(niter <= maxiter, dx >= dxmin))
g = grad(x);
gnorm = norm(g);
xnew = x - alpha*g;
if ~isfinite(xnew)
display(['Number of iterations: ' num2str(niter)])
error('x is inf or NaN')
end
refresh
niter = niter + 1;
dx = norm(xnew-x);
x = xnew;
end
xopt = x;
fopt = f2(xopt);
niter = niter - 1;
function g = grad(x)
g = 4*(x(1).^2 + x(2)-x(3)).^2 +10;
ans =
0.3667
0.3667
0.3667
OR
Alternately make use of the following code for accurate result
fun = @(x) 4*(x(1).^2 + x(2)-x(3)).^2 +10;
x0 = [3,3,3];
x = fminsearch(fun,x0);
Best Answer