Newton to see when the Gradient = 0
Show older comments
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
function dfx=Gradient(x)
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:
Newton('Rosenbrock', 'Gradient', 'Hessematrix', [1.2;1.2])
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
Answers (1)
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:
function dfx=Gradient(x)
dfx=[-400 * x(1) * (x(2)-x(1)^2)+2*(x(1)-1); 200*(x(2)-x(1)^2)];
% ^ instead of a ","
end
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!