Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Matlab) Let f(x) be a twice continuously differentiable function and suppose we

ID: 3831917 • Letter: #

Question

(Matlab) Let f(x) be a twice continuously differentiable function and suppose we are already given three Matlab functions "f. m" with header "function [y] = f(x)" that returns values of f(x) "df. m", with header "function [y] = df(x)", that returns values of f'(x); "d2f.m", with header "function [y] = d2f(x)", that returns values of f"(x). Use these to write a Matlab program that inputs x0, the initial guess n, the number of iterations and outputs the value of x_n from Newton's method applied to the function h (x) = f(x)/f'(x): x_n + 1 = x_n - h(x_n)/h'(x_n).

Explanation / Answer

function [xn] = newton(x0, n)

% set the value of xn to initial guess x0
xn = x0;

% iterate for n times
for i = 1:n

   % calculate h(xn)
   h = f(xn) / df(xn);

   % calculate dh(xn)
   dh = ( df(xn)^2 - f(xn)*d2f(xn) ) / df(xn)^2;
  
   % update xn
   xn = xn - ( h / dh );

end

% return xn

end