# Newton to see when the Gradient = 0

1 view (last 30 days)
Oskar Soth on 5 Jul 2022
Answered: Jan on 5 Jul 2022
Hey i have a Problem i defined the following function:
function Newton(f,df,Hesf,x)
% Newtonverfahren
sigma=1e-4;
beta=0.5;
rho=1e-8;
p=2.1;
eps=1e-6;
kmax=200;
k=0;
fx=feval(f,x);
dfx=feval(df,x);
while norm(dfx) > eps && k<=kmax
Hf=feval(Hesf,x);
if condest(Hf) > 1e+15
d=-dfx;
else
d=(-Hf\dfx')';
if dfx*d' > -rho*norm(d)^p
d=-dfx;
end
end
xd=x+d;
fxd=feval(f,xd);
j=0;
dfxd=dfx*d';
while fxd > fx + sigma * beta^j * dfxd
j=j+1;
xd=x+beta^j*d;
fxd=feval(f,xd);
end
x=xd;
fx=fxd;
dfx=feval(df,x);
k=k+1;
end
disp(x)
%disp(x(1));
%disp(x(2));
end
this is how i defined my functions:
function fx = Rosenbrock(x)
fx=100*(x(2)-x(1)^2)^2+(1-x(1))^2;
end
dfx=[-400 * x(1) *(x(2)-x(1)^2)+2*(x(1)-1), 200*(x(2)-x(1)^2)];
end
function dfx=Hessematrix(x)
dfx=[-400 *(x(2)-x(1)^2)+800*x(1)^2+2, -400*x(1); -400*x(1), 200];
end
When im now calling the Newton Function with:
I get a 2x2 Matrix. But i would expect an output of 2x1 since the output should be the value for the gradient.
I thought maybe something is wrong with my input. Maybe someone can help me

Jan on 5 Jul 2022
In the line:
xd=x+d;
x is a [2x1] vector and d a [1x2] vector. The implicite expanding replies a [2 x 2] matrix.
Maybe this fixes the problem:
dfx=[-400 * x(1) * (x(2)-x(1)^2)+2*(x(1)-1); 200*(x(2)-x(1)^2)];
% ^ instead of a ","
end

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by