Numerical Differentiation by Finite Differences

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite
1












Numerical differentiation is known to be ill-conditioned unless using a Chebyshev series, but this requires global information about the function and a priori knowledge of a compact domain on which the function will be evaluated. For this reason, simple finite differences are often useful. The unit roundoff gives a natural choice of step-see here and here for more details. I felt that this technique is better than the technique used in the GSL which requires specifying the stepsize, so I felt it should be given life in Boost.Math, esp. since spectral element methods are getting so popular. Regrettably, the pull-request was panned because the implementation is awkward, requiring a bunch of if checks on the order of accuracy and quite a bit of code duplication. Can it be cleaned up?



#ifndef BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
#define BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
#include <boost/math/constants/constants.hpp>

namespace boost namespace math namespace tools
template<class F, class Real, size_t order=6>
Real finite_difference_derivative(const F f, Real x, Real* error = nullptr)
order == 2


#endif






share|improve this question



























    up vote
    3
    down vote

    favorite
    1












    Numerical differentiation is known to be ill-conditioned unless using a Chebyshev series, but this requires global information about the function and a priori knowledge of a compact domain on which the function will be evaluated. For this reason, simple finite differences are often useful. The unit roundoff gives a natural choice of step-see here and here for more details. I felt that this technique is better than the technique used in the GSL which requires specifying the stepsize, so I felt it should be given life in Boost.Math, esp. since spectral element methods are getting so popular. Regrettably, the pull-request was panned because the implementation is awkward, requiring a bunch of if checks on the order of accuracy and quite a bit of code duplication. Can it be cleaned up?



    #ifndef BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
    #define BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
    #include <boost/math/constants/constants.hpp>

    namespace boost namespace math namespace tools
    template<class F, class Real, size_t order=6>
    Real finite_difference_derivative(const F f, Real x, Real* error = nullptr)
    order == 2


    #endif






    share|improve this question























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      Numerical differentiation is known to be ill-conditioned unless using a Chebyshev series, but this requires global information about the function and a priori knowledge of a compact domain on which the function will be evaluated. For this reason, simple finite differences are often useful. The unit roundoff gives a natural choice of step-see here and here for more details. I felt that this technique is better than the technique used in the GSL which requires specifying the stepsize, so I felt it should be given life in Boost.Math, esp. since spectral element methods are getting so popular. Regrettably, the pull-request was panned because the implementation is awkward, requiring a bunch of if checks on the order of accuracy and quite a bit of code duplication. Can it be cleaned up?



      #ifndef BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
      #define BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
      #include <boost/math/constants/constants.hpp>

      namespace boost namespace math namespace tools
      template<class F, class Real, size_t order=6>
      Real finite_difference_derivative(const F f, Real x, Real* error = nullptr)
      order == 2


      #endif






      share|improve this question













      Numerical differentiation is known to be ill-conditioned unless using a Chebyshev series, but this requires global information about the function and a priori knowledge of a compact domain on which the function will be evaluated. For this reason, simple finite differences are often useful. The unit roundoff gives a natural choice of step-see here and here for more details. I felt that this technique is better than the technique used in the GSL which requires specifying the stepsize, so I felt it should be given life in Boost.Math, esp. since spectral element methods are getting so popular. Regrettably, the pull-request was panned because the implementation is awkward, requiring a bunch of if checks on the order of accuracy and quite a bit of code duplication. Can it be cleaned up?



      #ifndef BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
      #define BOOST_MATH_TOOLS_NUMERICAL_DIFFERENTIATION_HPP
      #include <boost/math/constants/constants.hpp>

      namespace boost namespace math namespace tools
      template<class F, class Real, size_t order=6>
      Real finite_difference_derivative(const F f, Real x, Real* error = nullptr)
      order == 2


      #endif








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 27 at 20:02
























      asked Jan 27 at 18:07









      user14717

      20719




      20719

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186142%2fnumerical-differentiation-by-finite-differences%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186142%2fnumerical-differentiation-by-finite-differences%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?