Steepest descent with exact line search method

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

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));

3 Comments

Can I ask how can this code be applied to a system of more equations? I am new to this and Ive been finding examples but I still do not understand.
Matt J
Matt J on 16 Feb 2022
Edited: Matt J on 16 Feb 2022
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.
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

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Commented:

on 17 Feb 2024

Community Treasure Hunt

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

Start Hunting!