MATLAB Answers

Steepest descent with exact line search method

21 views (last 30 days)
Abdul Rahim Shihabuddin
Abdul Rahim Shihabuddin on 15 Sep 2021
Edited: Matt J on 15 Sep 2021
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 Comment
Matt J
Matt J on 15 Sep 2021
I have reformatted your code and put it in a code well:

Sign in to comment.

Answers (1)

Matt J
Matt J on 15 Sep 2021
Edited: Matt J on 15 Sep 2021
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));

Community Treasure Hunt

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

Start Hunting!

Translated by