Steepest descent with exact line search method

147 次查看(过去 30 天)
Noob here . I have been trying to implement steepest descent algorithm on matlab and I first solved it using constant step size. But now I have been trying to implement exact line search method to find the step size which I can't seem to solve . Here's the code I'm working with:
syms x1 x2
syms alpha %stepsize
n=input("Enter the roll number:");
f1=(x1-n)^2 + (x2-2*n)^2;
fx=inline(f1);
fobj=@(x)fx(x(:,1),x(:,2));
%Gradient
grad=gradient(f1);
G=inline(grad);
gradx=@(x) G(x(:,1),x(:,2));
%Initial Parameters
x0=[1 1];
k=0;
X=[];
while norm(gradx(x0))>0.001
X=[X;x0];
gradnew=-gradx(x0);
a=func(x0,alpha,gradnew,n);
X_new=x0 + a*gradnew.';
x0=X_new;
k=k+1;
end
fprintf("Optimal solution x=%f,%f\n",x0);
fprintf("Optimal value f(x)= %d \n",fobj(x0));
function y = func(x,b,d,n)
f=@(a)(x+b.*d' -n).^2 + (x+b.*d'-2.*n).^2;
res=fminunc(inline(f),[1,1])
y=res.b

回答(1 个)

Matt J
Matt J 2021-9-15
编辑:Matt J 2021-9-15
n=input("Enter the roll number:");
fobj=@(x) (x(1)-n)^2 + (x(2)-2*n)^2;
gradobj=@(x)2*[(x(1)-n), (x(2)-2*n)];
x0=[1 1];
k=0;
X=[];
while norm(gradobj(x0))>0.001
X=[X;x0];
linedir=-gradobj(x0);
fline=@(a) fobj(x0+a*linedir);
a=fminsearch(fline,0);
x0=x0 + a*linedir;
k=k+1
end
fprintf("Optimal solution x=%f,%f\n",x0);
fprintf("Optimal value f(x)= %d \n",fobj(x0));
  3 个评论
Matt J
Matt J 2022-2-16
编辑:Matt J 2022-2-16
There are no equations in the problem addressed here. This is a cost function minimization problem.
If you have a cost function (and its gradient) that measures agreeement with your equations, it should work just the same.
However, it would be advisable to use fsolve() if you can, rather than implement your own solver.
Nasser Issa
Nasser Issa 2024-2-17
Bonjour comment allez-vous je suis nouveau mais je cherche une implémentation sur MATLAB ou en python pour optimisation méthode steepest Descent associé à la méthode dichotomie
J'attends votre réponse sur mon adresse mail nasserissahamidou@gmail.com

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by