I'm getting an error in obtaining the optimal solution for a nonlinear equation
6 次查看(过去 30 天)
显示 更早的评论
Bakican Ayna
2020-4-26
回答: Thiago Henrique Gomes Lobato
2020-4-26
I'm trying to apply steepest descent satifying strong wolfe conditions to the Rosenbruck function with inital x0=(1.2,1.2), however, although the function itself has a unique solution at (1,1), I'm getting (-inf,inf) as an optimal solution. Here are the codes:
function [X,Grad,ite] = steepest_descent_wolfe(fhandle,x0,tol,maxit,alpha0,c,cu,mu,amax)
% Description:
% Obtains the iterations of a given function with given datas by using
% steepest descent method with wolfe condition
%
% Input:
% fhandle : objective function
% x0 : initial guess
% tol : tolerance
% maxit : maximum number of iterations
% alpha0 : initial step-length
% c : Armijo parameter
% cu : Strong curvature Parameter
% mu : backtracking parameter
% amax : maximum number of iteration for Wolfe contion
%
% Output:
% X : solution containing each iteration
% Grad : gradient of solution
% ite : number of iteration
% Usage:
% steepest_descent_armijo(fhandle,x0,tol,maxit,alpha0,c,beta,amax)
ite = 1;
% Calculate function values at initial guess
[~,fgrad] = feval(fhandle,x0);
% Compute magnitude of gradient for stopping criteria
Grad(:,1) = norm(fgrad);
% Allocate initial point
x(:,1) = x0;
while( ite < maxit && norm(fgrad) > tol)
% Compute the search direction
p = -fgrad;
% Compute step-length, satisfying Armijo condition
alpha = wolfe(fhandle,x(:,ite),p,alpha0,c,cu,mu,amax);
% Update the solution
x(:,ite+1) = x(:,ite) + alpha*p;
% Compute gradient of function at current point for stopping criteria
[~,fgrad] = feval(fhandle,x(:,ite+1));
% Compute magnitude of gradient for stopping criteria
Grad(:,ite+1) = norm(fgrad);
% Increase the iteration number
ite = ite+1;
end
X = x;
end
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
function alpha = wolfe(fhandle, x, p, alpha0, c, cu, mu, amax)
% Description:
% Compute step-length of an iteration satisfied by Wolfe condition
%
% Input:
% fhandle : function handle
% x : current point
% alpha0 : initial step-length
% c : Armijo parameter
% cu : Curvature parameter
% mu : backtracking parameter
% amax : maximum number of iteration
%
% Output:
% alpha: step-length of an iteration satisfied by Wolfe condition
%
% Usage:
% wolfe(fhandle, x, p, alpha0, c, cu, mu, amax)
j = 0;
% Calculate function values at x
[f,gradx] = feval(fhandle,x);
% Calculate function values at x + alpha*p
[fh,gradk] = feval(fhandle,x+alpha0*p);
while ( ( fh > f + c*alpha0*gradx'*p ) && ( j < amax ) && ( abs(gradk)'*p > cu*abs(gradx)'*p ))
% Update the step length by using backtracking with parameter mu
alpha0 = alpha0*mu;
% Update the iterate
a = x + alpha0*p;
% Calculate function values at x + alpha*p
[fh,gradk] = feval(fhandle,a);
% Increase the iteration number
j = j+1;
end
alpha = alpha0;
end
%where the objective function is
function [f,gradf] = rosenbrock(x)
f = 100*(x(1)^2 - x(2))^2 + (x(1)-1)^2;
gradf = [100*(2*(x(1)^2-x(2))*2*x(1)) + 2*(x(1)-1); ...
100*(-2*(x(1)^2-x(2))) ];
end
%%%the input values
x = [1.2,1.2];
tol = 1.0e-4;
maxit = 100;
alpha0 = 1;
c = 1.0e-4;
cu = 0.9;
mu = 0.5;
amax = 1000;
%%%%%%%%
0 个评论
采纳的回答
Thiago Henrique Gomes Lobato
2020-4-26
You have an error in your wolf loop. It should be like this:
while ( ( ( fh > f + c*alpha0*gradx'*p ) || ( abs(gradk)'*p > cu*abs(gradx)'*p ) ) && ( j < amax ) )
Both conditions have to be fullfiled, if you use all && then only one of them is required. || and && can be indeed a little trickier in while loops. Replacing this in your code with your initial conditions I get:
X(:,end)
ans =
1.1109
1.2339
And, for maxit = 1000:
X(:,end)
ans =
1.0046
1.0093
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!