Newton's root finding method in MATLAB: quadratic vs cubic convergence











up vote
1
down vote

favorite












So I have this example Newton's method for root finding with quadratic convergence below. It takes a function f, the derivative of f df, initial guess g, and tolerance tol. It outputs the # iterations it to reach nth root, the root estimate r, and the error approximation err.



function [it, r, err] = QuadraticN(f, df, g , tol)

num_it = 20;
it_max = num_it + 1;

x(1)=(g + num_it)/2; %set x at n = 1

n = 1;
r = 0; % root
it = 0; % iteration #
err =0; % error

% n = 1
r(1) = x(1)-f(x(1))/df(x(1)); % root at n = 1
err(1) = abs(x(1)-g); % error at n = 1

% n > 1
while (min(abs(f(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

x(n+1)=x(n)-f(x(n))/df(x(n)); % quadratic method

it_max=x(n); % reset counter
r= x(n); % set root to current at n

err = f(x(n)); % set error to current
it = n; % keep track of iterations
n=n+1; % increment to next n

end

end


Given this example, I am trying to implement a second version of the method that uses cubic convergence as opposed to quadratic and this is what I have (this is just the part of the function that would be changed to cubic, it takes the same inputs with the addition of ddf = the second derivative):



 r(1) = x(1)-df(x(1))/ddf(x(1)) + (sqrt((df(x(1)))^2 - 
2*f(x(1))*ddf(x(1)))) / ddf(x(1));

err(1) = abs(x(1)-g); % error at n = 1

while (min(abs(ddf(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

cube = ((sqrt((df(x(n)))^2 - 2*f(x(n))*ddf(x(n)))) / ddf(x(n)));

first = x(n) - df(x(n))/ddf(x(n));
first = real(first); % is this right?

x(n+1) = first + (cube);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% FIX THIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% check when to do plus or minus
if( first > 0)
x(n+1)=x(n)-df(x(n))/ddf(x(n)) + cube;
else
x(n+1)=x(n)-df(x(n))/ddf(x(n)) - cube;

% end


I'm not sure if I am checking when to use +/- cube correctly. I know it will depend on the multiplicity but I'm not sure if using first (Newton's basic method). It is outputting some of the roots correctly, but for instance for the equation -x^3 + 8 with initial guess 2 I am getting -9.85985274E-01. Now I don't know if this is part of the problem above or if converting to real before calculating is. I am also getting negative errors. I am checking in the same way as I did with quadratic but I am not sure if this is right.










share|improve this question


























    up vote
    1
    down vote

    favorite












    So I have this example Newton's method for root finding with quadratic convergence below. It takes a function f, the derivative of f df, initial guess g, and tolerance tol. It outputs the # iterations it to reach nth root, the root estimate r, and the error approximation err.



    function [it, r, err] = QuadraticN(f, df, g , tol)

    num_it = 20;
    it_max = num_it + 1;

    x(1)=(g + num_it)/2; %set x at n = 1

    n = 1;
    r = 0; % root
    it = 0; % iteration #
    err =0; % error

    % n = 1
    r(1) = x(1)-f(x(1))/df(x(1)); % root at n = 1
    err(1) = abs(x(1)-g); % error at n = 1

    % n > 1
    while (min(abs(f(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

    x(n+1)=x(n)-f(x(n))/df(x(n)); % quadratic method

    it_max=x(n); % reset counter
    r= x(n); % set root to current at n

    err = f(x(n)); % set error to current
    it = n; % keep track of iterations
    n=n+1; % increment to next n

    end

    end


    Given this example, I am trying to implement a second version of the method that uses cubic convergence as opposed to quadratic and this is what I have (this is just the part of the function that would be changed to cubic, it takes the same inputs with the addition of ddf = the second derivative):



     r(1) = x(1)-df(x(1))/ddf(x(1)) + (sqrt((df(x(1)))^2 - 
    2*f(x(1))*ddf(x(1)))) / ddf(x(1));

    err(1) = abs(x(1)-g); % error at n = 1

    while (min(abs(ddf(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

    cube = ((sqrt((df(x(n)))^2 - 2*f(x(n))*ddf(x(n)))) / ddf(x(n)));

    first = x(n) - df(x(n))/ddf(x(n));
    first = real(first); % is this right?

    x(n+1) = first + (cube);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% FIX THIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% check when to do plus or minus
    if( first > 0)
    x(n+1)=x(n)-df(x(n))/ddf(x(n)) + cube;
    else
    x(n+1)=x(n)-df(x(n))/ddf(x(n)) - cube;

    % end


    I'm not sure if I am checking when to use +/- cube correctly. I know it will depend on the multiplicity but I'm not sure if using first (Newton's basic method). It is outputting some of the roots correctly, but for instance for the equation -x^3 + 8 with initial guess 2 I am getting -9.85985274E-01. Now I don't know if this is part of the problem above or if converting to real before calculating is. I am also getting negative errors. I am checking in the same way as I did with quadratic but I am not sure if this is right.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      So I have this example Newton's method for root finding with quadratic convergence below. It takes a function f, the derivative of f df, initial guess g, and tolerance tol. It outputs the # iterations it to reach nth root, the root estimate r, and the error approximation err.



      function [it, r, err] = QuadraticN(f, df, g , tol)

      num_it = 20;
      it_max = num_it + 1;

      x(1)=(g + num_it)/2; %set x at n = 1

      n = 1;
      r = 0; % root
      it = 0; % iteration #
      err =0; % error

      % n = 1
      r(1) = x(1)-f(x(1))/df(x(1)); % root at n = 1
      err(1) = abs(x(1)-g); % error at n = 1

      % n > 1
      while (min(abs(f(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

      x(n+1)=x(n)-f(x(n))/df(x(n)); % quadratic method

      it_max=x(n); % reset counter
      r= x(n); % set root to current at n

      err = f(x(n)); % set error to current
      it = n; % keep track of iterations
      n=n+1; % increment to next n

      end

      end


      Given this example, I am trying to implement a second version of the method that uses cubic convergence as opposed to quadratic and this is what I have (this is just the part of the function that would be changed to cubic, it takes the same inputs with the addition of ddf = the second derivative):



       r(1) = x(1)-df(x(1))/ddf(x(1)) + (sqrt((df(x(1)))^2 - 
      2*f(x(1))*ddf(x(1)))) / ddf(x(1));

      err(1) = abs(x(1)-g); % error at n = 1

      while (min(abs(ddf(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

      cube = ((sqrt((df(x(n)))^2 - 2*f(x(n))*ddf(x(n)))) / ddf(x(n)));

      first = x(n) - df(x(n))/ddf(x(n));
      first = real(first); % is this right?

      x(n+1) = first + (cube);

      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %% FIX THIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %%%% check when to do plus or minus
      if( first > 0)
      x(n+1)=x(n)-df(x(n))/ddf(x(n)) + cube;
      else
      x(n+1)=x(n)-df(x(n))/ddf(x(n)) - cube;

      % end


      I'm not sure if I am checking when to use +/- cube correctly. I know it will depend on the multiplicity but I'm not sure if using first (Newton's basic method). It is outputting some of the roots correctly, but for instance for the equation -x^3 + 8 with initial guess 2 I am getting -9.85985274E-01. Now I don't know if this is part of the problem above or if converting to real before calculating is. I am also getting negative errors. I am checking in the same way as I did with quadratic but I am not sure if this is right.










      share|improve this question













      So I have this example Newton's method for root finding with quadratic convergence below. It takes a function f, the derivative of f df, initial guess g, and tolerance tol. It outputs the # iterations it to reach nth root, the root estimate r, and the error approximation err.



      function [it, r, err] = QuadraticN(f, df, g , tol)

      num_it = 20;
      it_max = num_it + 1;

      x(1)=(g + num_it)/2; %set x at n = 1

      n = 1;
      r = 0; % root
      it = 0; % iteration #
      err =0; % error

      % n = 1
      r(1) = x(1)-f(x(1))/df(x(1)); % root at n = 1
      err(1) = abs(x(1)-g); % error at n = 1

      % n > 1
      while (min(abs(f(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

      x(n+1)=x(n)-f(x(n))/df(x(n)); % quadratic method

      it_max=x(n); % reset counter
      r= x(n); % set root to current at n

      err = f(x(n)); % set error to current
      it = n; % keep track of iterations
      n=n+1; % increment to next n

      end

      end


      Given this example, I am trying to implement a second version of the method that uses cubic convergence as opposed to quadratic and this is what I have (this is just the part of the function that would be changed to cubic, it takes the same inputs with the addition of ddf = the second derivative):



       r(1) = x(1)-df(x(1))/ddf(x(1)) + (sqrt((df(x(1)))^2 - 
      2*f(x(1))*ddf(x(1)))) / ddf(x(1));

      err(1) = abs(x(1)-g); % error at n = 1

      while (min(abs(ddf(x(n))))) && (abs(x(n)-it_max)>tol) && (it < num_it)

      cube = ((sqrt((df(x(n)))^2 - 2*f(x(n))*ddf(x(n)))) / ddf(x(n)));

      first = x(n) - df(x(n))/ddf(x(n));
      first = real(first); % is this right?

      x(n+1) = first + (cube);

      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %% FIX THIS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %%%% check when to do plus or minus
      if( first > 0)
      x(n+1)=x(n)-df(x(n))/ddf(x(n)) + cube;
      else
      x(n+1)=x(n)-df(x(n))/ddf(x(n)) - cube;

      % end


      I'm not sure if I am checking when to use +/- cube correctly. I know it will depend on the multiplicity but I'm not sure if using first (Newton's basic method). It is outputting some of the roots correctly, but for instance for the equation -x^3 + 8 with initial guess 2 I am getting -9.85985274E-01. Now I don't know if this is part of the problem above or if converting to real before calculating is. I am also getting negative errors. I am checking in the same way as I did with quadratic but I am not sure if this is right.







      algorithm matlab newtons-method cubic






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 0:54









      dgdgdg

      61




      61





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254768%2fnewtons-root-finding-method-in-matlab-quadratic-vs-cubic-convergence%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254768%2fnewtons-root-finding-method-in-matlab-quadratic-vs-cubic-convergence%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)