Globally Converging Newton's Method won't stop running

조회 수: 2 (최근 30일)
Sarah Johnson
Sarah Johnson 2020년 2월 13일
댓글: Geoff Hayes 2020년 2월 13일
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

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 2월 13일
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
Sarah Johnson
Sarah Johnson 2020년 2월 13일
This is actually a different problem that I have done. That one was called the "Simple Newton Iteration" and the one I'm having the issues with is supposed to be globally convergent with the backtracking. Most of the code here was provided by my professor in our notes and the instructions said to use the algorithm as such with backtracking. I added in the while loop for the tolerance so I'm thinking it may need to go somewhere else? I'm just not sure where to go to fix the code with backtracking. I do appreciate your explaination though! It did help me to understand it a little better
Geoff Hayes
Geoff Hayes 2020년 2월 13일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by