Globally Converging Newton's Method won't stop running
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
My goal is to write a function for the Globally Converging Newton's method to find the minimum of the function f. This is what I have for my code; I think my issue lies in the while loop possibly but I'm pretty new to MATLAB and not wonderful at it so any advice/help would be really great!
function [min] = GCNewtons(x0)
f = @(x) exp(2*sin(x)) - x;
g = @(x) (2*exp(2*sin(x))*cos(x)) - 1;
h = @(x) (-2*exp(2*sin(x)))*(sin(x)-2*cos(x)^2);
tol = 10^-6;
x = x0;
for k = 1:50
while abs(g(x)) >= tol
y = x - (g(x) / h(x));
if f(y) < f(x)
min = y;
else
%Backtracking
if g(x) < 0
xtest = x + ((abs(y - x)) / 2);
while f(xtest) >= f(x)
xtest = (x + xtest) / 2;
end
else
xtest = x - ((abs(y - x)) / 2);
while f(xtest) >= f(x)
xtest = (x + xtest) / 2;
end
end
min = xtest;
end
end
end
end
0 comentarios
Respuestas (1)
Geoff Hayes
el 13 de Feb. de 2020
Julia - the above code may be overcomplicating Newton's Method. I think the code is trying to only allow for 50 iterations (due to the for loop) but then has an unneeded while loop (plus a couple of others for the "backtracking"). You may want to start with a simpler algorithm and then decide if the backtracking is needed. Also, while you calculate y on each iteration, you don't use it on subsequent ones (you probably want to be setting the result to x to instead so that it changes on each iteration).
I think the above could be simplified to
function [min] = GCNewtons(x0)
f = @(x) exp(2*sin(x)) - x;
g = @(x) (2*exp(2*sin(x))*cos(x)) - 1;
h = @(x) (-2*exp(2*sin(x)))*(sin(x)-2*cos(x)^2);
tol = 10^-6;
x = x0;
for k = 1:50
x = x - (g(x) / h(x));
% add any tolerance checks here
end
min = 0; % what should this be?
end
I've left the tolerance checks and the assignment of min to you. I'm assuming that h is the correct derivative of g.
2 comentarios
Geoff Hayes
el 13 de Feb. de 2020
hmm..ok. So how should the xtest be used on each iteration (if backtracking)? How should min be used? Or should the new x (on subsequent iterations) be that min?
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!