MATLAB Answers

Globally Converging Newton's Method won't stop running

3 ビュー (過去 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

  0 件のコメント

サインイン to comment.

回答 (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?

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by