Steepest descent with exact line search method

조회 수: 146 (최근 30일)
Abdul Rahim Shihabuddin
Abdul Rahim Shihabuddin 2021년 9월 15일
댓글: Nasser Issa 2024년 2월 17일
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일
I have reformatted your code and put it in a code well:

댓글을 달려면 로그인하십시오.

답변 (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