Question

Newton's method for finding the root in MATLAB. I am stuck on the two lines of...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Solve the following problem using the MATLAB environment Write a function [approx_root, num_its] = bisection(f,a,b,tol) that...
Solve the following problem using the MATLAB environment Write a function [approx_root, num_its] = bisection(f,a,b,tol) that implements the bisection method. You function should take as input 4 arguments with the last argument being optional, i.e, if the user does not provide the accuracy tol use a default of 1.0e-6 (use varargin to attain this). Your function should output the approximate root, approx_root and the number of iterations it took to attain the root, num_its. However, if the user calls the...
Using Matlab, find an approximation by the method of false position for the root of function...
Using Matlab, find an approximation by the method of false position for the root of function f(x) = ex −x2 + 3x−2 accurate to within 10−5 (absolute error) on the interval [0,1]. Please answer and show code. Pseudo Code for Method of False Position: Given [a,b] containing a zero of f(x); tolerance = 1.e-7; nmax = 1000; itcount = 0; error = 1; while (itcount <=nmax && error >=tolerance) itcount = itcount + 1; x= a - ((b-a)/(f(b)-f(a)))f(a) error =abs(f(x));...
17. I am using Newton’s method to find the negative root of f(x) = 3−x2. (a)...
17. I am using Newton’s method to find the negative root of f(x) = 3−x2. (a) What would be a good guess for x1? Draw the line tangent to f(x) at your x1 and explain why using Newton’s method would lead to the negative root of the function. (b) What would be a bad guess for x1? Draw the line tangent to f(x) at your x1 and explain why using Newton’s method would not lead to the negative root of...
Using Matlab 1. Give the flowchart for finding the root of the function f(x) = [tanh⁡(x-2)]...
Using Matlab 1. Give the flowchart for finding the root of the function f(x) = [tanh⁡(x-2)] [sin⁡(x+3)+2] with the following methods (6 significant figures required): a) Modified Regula Falsi (Choose two reasonable integers as your initial upper and lower bounds) b) Newton’s Method (Choose one reasonable integer as your initial guess for the root)
Use Newton’s method to find a 6-decimal place approximation to the positive root of f(x) =...
Use Newton’s method to find a 6-decimal place approximation to the positive root of f(x) = x 5 − 7x 2 + 4 that is nearest to the origin. (a) Tell your “Newton function” R(x) (b) Tell what technology you used. (“Handheld calculator” is not acceptable.) (c) Tell your initial guess (x1) and the iterations that you observed. (d) Tell your “stopping criteria.” That is, why did you stop after n iterations
Find the root of the function: f(x)=2x+sin⁡(x)-e^x, using Newton Method and initial value of 0. Calculate...
Find the root of the function: f(x)=2x+sin⁡(x)-e^x, using Newton Method and initial value of 0. Calculate the approximate error in each step. Use maximum 4 steps (in case you do not observe a convergence).
Newton's method: For a function ?(?)=ln?+?2−3f(x)=ln⁡x+x2−3 a. Find the root of function ?(?)f(x) starting with ?0=1.0x0=1.0....
Newton's method: For a function ?(?)=ln?+?2−3f(x)=ln⁡x+x2−3 a. Find the root of function ?(?)f(x) starting with ?0=1.0x0=1.0. b. Compute the ratio |??−?|/|??−1−?|2|xn−r|/|xn−1−r|2, for iterations 2, 3, 4 given ?=1.592142937058094r=1.592142937058094. Show that this ratio's value approaches |?″(?)/2?′(?)||f″(x)/2f′(x)| (i.e., the iteration converges quadratically). In error computation, keep as many digits as you can.
Calculate two iterations of Newton's Method to approximate a zero of the function using the given...
Calculate two iterations of Newton's Method to approximate a zero of the function using the given initial guess. (Round your answers to four decimal places.) f(x) = cos x, x1 = 0.8 n xn f(xn) f '(xn) f(xn) f '(xn) xn − f(xn) f '(xn) 1 2
Calculate two iterations of Newton's Method to approximate a zero of the function using the given...
Calculate two iterations of Newton's Method to approximate a zero of the function using the given initial guess. (Round your answers to three decimal places.) 45. f(x) = x5 − 5,    x1 = 1.4 n xn f(xn) f '(xn) f(xn) f '(xn) xn − f(xn) f '(xn) 1 2 40. Find two positive numbers satisfying the given requirements. The product is 234 and the sum is a minimum. smaller value= larger value= 30.Determine the open intervals on which the graph is...