Fill in the gaps in the code function [xi,fi,n] = newton(f,fp,x0,tol) % Descript
ID: 673626 • Letter: F
Question
Fill in the gaps in the code
function [xi,fi,n] = newton(f,fp,x0,tol)
% Description: Approximate the root of f(x) using Newton's method.
%
%
% Inputs
%
% f: function handle, function whose root we seek
%
% fp: function handle, derivative of function f
%
% x0: double, initial guess for x
%
% tol: double, tolerance level (tol > 0)
%
%
% Outputs
%
% xi: double, (n+1,1) vector listing the iterates x0,x1,...xn
%
% fi: double, (n+1,1) vector listing the function values f(xi)
%
% n: int, number of iterations the algorithm took to converge
%
%
%
%
% -------------------------------------------------------------------------
n_max = 100; % maximum number of iterations
count = 0; % iteration counter
% Initialize xi
xi = zeros(n_max,1);
xi(1) = x0;
% Initialize fi
fi = zeros(n_max,1);
fi(1) = f(x0);
xnew = x0;
error = 2*tol; % initialize the error to be larger than the tolerance
while error > tol && count < n_max
count = count + 1;
% ---------------------------------------------------------------------
% Check if derivative is zero
% ---------------------------------------------------------------------
% If the derivative is zero at x = x0, then assign xi = nan,
% fi = nan, and n = nan, print a warning and exit.
% !! Your code here !!
% ---------------------------------------------------------------------
% Update the x-value
% ---------------------------------------------------------------------
% Update the previous iterate
x0 = xnew;
xnew = % !! your code here !!
% Store iterate and function value in vectors
xi(count+1) = xnew;
fi(count+1) = % !! your code here !!
% ---------------------------------------------------------------------
% Compute the error
% ---------------------------------------------------------------------
error = % !! your code here !!
end
% Discard unwanted entries
n = count;
xi = xi(1:n+1);
fi = fi(1:n+1);
Explanation / Answer
Answer:
function [xi,fi,n] = newton(f,fp,x0,tol)
n_max = 100; % maximum number of iterations
count = 0; % iteration counter
% Initialize xi
xi = zeros(n_max,1);
xi(1) = x0;
% Initialize fi
fi = zeros(n_max,1);
fi(1) = f(x0);
xnew = x0;
error = 2*tol; % initialize the error to be larger than the tolerance
while error > tol && count < n_max
count = count + 1;
% ---------------------------------------------------------------------
% Check if derivative is zero
% ---------------------------------------------------------------------
% If the derivative is zero at x = x0, then assign xi = nan,
% fi = nan, and n = nan, print a warning and exit.
if (feval(fp,x0)==0)
fi = nan;
n=nan;
xi=nan;
error('Derivative of function is 0')
end
% ---------------------------------------------------------------------
% Update the x-value
% ---------------------------------------------------------------------
% Update the previous iterate
x0 = xnew;
xnew = count;
% Store iterate and function value in vectors
xi(count+1) = xnew;
fi(count+1) = x0-feval(f,x0)/feval(fp,x0);
% ---------------------------------------------------------------------
% Compute the error
% ---------------------------------------------------------------------
error = abs(fi(count+1));
end
% Discard unwanted entries
n = count;
xi = xi(1:n+1);
fi = fi(1:n+1);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.