Newton's method for finding the root in MATLAB. I am stuck on the two lines of codes which are required to complete. can anyone give me a hint on this?
function sample_newton(f, xc, tol, max_iter)
% sample_newton(f, xc, tol, max_iter)
% finds a root of the given function f over the interval [a, b] using Newton-Raphson method
% IN:
% f - the target function to find a root of
% function handle with two outputs, f(x), f'(x)
% e.g., f=@(x) [sin(x);cos(x)];
% xc - the initial guess to start the Newton method
% tol - the tolerance to stop the iterative method
% max_iter - the maximum iterations allowed
% OUT:
% none
% (c) Z. Lubberts (JHU)
% v1 by M. Zhong
% Demo. code for EN.553.281
% if there is no user given tolerance, the code will pick one
if nargin < 2
xc = 0;
tol = 1.0e-5;
max_iter = 1000;
elseif nargin < 3
tol = 1.0e-5;
max_iter = 1000;
elseif nargin < 4
max_iter = 1000;
end
% check to see if we can start the code
fc = f(xc);
if length(fc)~=2
error('sample_newton:exception','Incorrect function handle type');
end
if abs(fc(1)) < tol
fprintf('We have found an (approximated) root: x_c = % 10.4e and f(x_c) = % 10.4e.\n', xc, fc);
else if abs(fc(2)) < 1e-8
fprintf('derivative of f at x_c is very close to zero, the code will now abort.\n');
error('sample_newton:exception', 'Bad initial guess!');
end
end
% start the bisection method
ind = 1; % iteration counter
while abs(fc(1))>tol && ind < max_iter
% complete the code to evalute the function and derivative at xk
% complete the code to update xc
if abs(fc(2))<1e-8 % check derivative isn't nearly zero
fprintf('Derivative of f at x_c is very close to zero, the code will now abort.\n');
error('sample_newton:exception', 'Bad initial guess!');
end
if abs(xc)>1e8 % check x_c isn't heading out to infinity
fprintf('x_c appears to be diverging, the code will now abort.\n');
error('sample_newton:exception', 'Bad initial guess!');
end
fprintf('At Iter. # = %4d, x_c = %10.4e and f(x_c) = % 10.4e.\n', ind, xc, fc(1));
ind = ind + 1; % increase the iteration counter
end
end
function sample_newton(f, xc, tol, max_iter)
% sample_newton(f, xc, tol, max_iter)
% finds a root of the given function f over the interval [a, b] using Newton-Raphson method
% IN:
% f - the target function to find a root of
% function handle with two outputs, f(x), f'(x)
% e.g., f=@(x) [sin(x);cos(x)];
% xc - the initial guess to start the Newton method
% tol - the tolerance to stop the iterative method
% max_iter - the maximum iterations allowed
% OUT:
% none
% (c) Z. Lubberts (JHU)
% v1 by M. Zhong
% Demo. code for EN.553.281
% if there is no user given tolerance, the code will pick one
if nargin < 2
xc = 0;
tol = 1.0e-5;
max_iter = 1000;
elseif nargin < 3
tol = 1.0e-5;
max_iter = 1000;
elseif nargin < 4
max_iter = 1000;
end
% check to see if we can start the code
fc = f(xc);
if length(fc)~=2
error('sample_newton:exception','Incorrect
function handle type');
end
if abs(fc(1)) < tol
fprintf('We have found an (approximated) root:
x_c = % 10.4e and f(x_c) = % 10.4e.\n', xc, fc);
else if abs(fc(2)) < 1e-8
fprintf('derivative of f
at x_c is very close to zero, the code will now abort.\n');
error('sample_newton:exception', 'Bad initial guess!');
end
end
% start the bisection method
ind = 1; % iteration counter
while abs(fc(1))>tol && ind < max_iter
fc = f(xc);
% complete the code to evalute the function and
derivative at xk
xc=xc-fc(1)/fc(2);
% complete the code to update xc
if abs(fc(2))<1e-8 % check derivative isn't
nearly zero
fprintf('Derivative of f
at x_c is very close to zero, the code will now abort.\n');
error('sample_newton:exception', 'Bad initial guess!');
end
if abs(xc)>1e8 % check x_c isn't heading out
to infinity
fprintf('x_c appears to
be diverging, the code will now abort.\n');
error('sample_newton:exception', 'Bad initial guess!');
end
fprintf('At Iter. # = %4d, x_c = %10.4e and
f(x_c) = % 10.4e.\n', ind, xc, fc(1));
ind = ind + 1; % increase the iteration
counter
end
end
Get Answers For Free
Most questions answered within 1 hours.