Second order approximation of first derivative gives odd results












2












$begingroup$


Assume I want to find the second order approximation of the first derivative in 0 of a function $f(x)$ (not defined for $x<0$). I will use the formulation for the forward derivative:



$f'(0)approx frac{-3f(0)+4f(h)-f(2h)}{2h}$



where $h$ is the step in the regular grid I am using. Now assume that the values of the function are:
$f(0)=0$, $f(h)=5$, $f(2h)=25$.
The function is clearly monotonically increasing and the first derivative in 0 is expected to be positive but, from the formula above, I get a negative value.



What am I missing? Does the issue all lie in the error (that I neglected)?










share|cite|improve this question









$endgroup$












  • $begingroup$
    When you say "the formulation", I'm not sure that's the right way to think about this. Assuming your calculations are correct, your weird result could simply be a function of the particular way you approximated the first derivative. Just as there are lots of ways to approximate a function, there are lots of ways to approximate its derivative, even in only one direction.
    $endgroup$
    – Adrian Keister
    Jan 28 at 17:15






  • 1




    $begingroup$
    For a function like $5^x$ a step size of $h=1$ is just too large. The error term $sim ln(5)^35^hh^2$ should be smaller than the result, so $ln(5)h<0.5$ is necessary to start to get good results.
    $endgroup$
    – LutzL
    Jan 28 at 21:49










  • $begingroup$
    @LutzL, I was not meaning $5^x$, my third point could have also been 23, but yes, the derivative is apparently too large to get good results. Thanks!
    $endgroup$
    – pugliam
    Jan 29 at 9:13












  • $begingroup$
    I was just looking for a simple growing function that had the same type of growth in value and first derivative. All other interpolations will have the same type of behavior, either a rapid oscillation so that the negative derivative is correct, or rapid growth in the higher order derivatives so that the error term is large.
    $endgroup$
    – LutzL
    Jan 29 at 9:35
















2












$begingroup$


Assume I want to find the second order approximation of the first derivative in 0 of a function $f(x)$ (not defined for $x<0$). I will use the formulation for the forward derivative:



$f'(0)approx frac{-3f(0)+4f(h)-f(2h)}{2h}$



where $h$ is the step in the regular grid I am using. Now assume that the values of the function are:
$f(0)=0$, $f(h)=5$, $f(2h)=25$.
The function is clearly monotonically increasing and the first derivative in 0 is expected to be positive but, from the formula above, I get a negative value.



What am I missing? Does the issue all lie in the error (that I neglected)?










share|cite|improve this question









$endgroup$












  • $begingroup$
    When you say "the formulation", I'm not sure that's the right way to think about this. Assuming your calculations are correct, your weird result could simply be a function of the particular way you approximated the first derivative. Just as there are lots of ways to approximate a function, there are lots of ways to approximate its derivative, even in only one direction.
    $endgroup$
    – Adrian Keister
    Jan 28 at 17:15






  • 1




    $begingroup$
    For a function like $5^x$ a step size of $h=1$ is just too large. The error term $sim ln(5)^35^hh^2$ should be smaller than the result, so $ln(5)h<0.5$ is necessary to start to get good results.
    $endgroup$
    – LutzL
    Jan 28 at 21:49










  • $begingroup$
    @LutzL, I was not meaning $5^x$, my third point could have also been 23, but yes, the derivative is apparently too large to get good results. Thanks!
    $endgroup$
    – pugliam
    Jan 29 at 9:13












  • $begingroup$
    I was just looking for a simple growing function that had the same type of growth in value and first derivative. All other interpolations will have the same type of behavior, either a rapid oscillation so that the negative derivative is correct, or rapid growth in the higher order derivatives so that the error term is large.
    $endgroup$
    – LutzL
    Jan 29 at 9:35














2












2








2





$begingroup$


Assume I want to find the second order approximation of the first derivative in 0 of a function $f(x)$ (not defined for $x<0$). I will use the formulation for the forward derivative:



$f'(0)approx frac{-3f(0)+4f(h)-f(2h)}{2h}$



where $h$ is the step in the regular grid I am using. Now assume that the values of the function are:
$f(0)=0$, $f(h)=5$, $f(2h)=25$.
The function is clearly monotonically increasing and the first derivative in 0 is expected to be positive but, from the formula above, I get a negative value.



What am I missing? Does the issue all lie in the error (that I neglected)?










share|cite|improve this question









$endgroup$




Assume I want to find the second order approximation of the first derivative in 0 of a function $f(x)$ (not defined for $x<0$). I will use the formulation for the forward derivative:



$f'(0)approx frac{-3f(0)+4f(h)-f(2h)}{2h}$



where $h$ is the step in the regular grid I am using. Now assume that the values of the function are:
$f(0)=0$, $f(h)=5$, $f(2h)=25$.
The function is clearly monotonically increasing and the first derivative in 0 is expected to be positive but, from the formula above, I get a negative value.



What am I missing? Does the issue all lie in the error (that I neglected)?







derivatives numerical-methods






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Jan 28 at 17:08









pugliampugliam

111




111












  • $begingroup$
    When you say "the formulation", I'm not sure that's the right way to think about this. Assuming your calculations are correct, your weird result could simply be a function of the particular way you approximated the first derivative. Just as there are lots of ways to approximate a function, there are lots of ways to approximate its derivative, even in only one direction.
    $endgroup$
    – Adrian Keister
    Jan 28 at 17:15






  • 1




    $begingroup$
    For a function like $5^x$ a step size of $h=1$ is just too large. The error term $sim ln(5)^35^hh^2$ should be smaller than the result, so $ln(5)h<0.5$ is necessary to start to get good results.
    $endgroup$
    – LutzL
    Jan 28 at 21:49










  • $begingroup$
    @LutzL, I was not meaning $5^x$, my third point could have also been 23, but yes, the derivative is apparently too large to get good results. Thanks!
    $endgroup$
    – pugliam
    Jan 29 at 9:13












  • $begingroup$
    I was just looking for a simple growing function that had the same type of growth in value and first derivative. All other interpolations will have the same type of behavior, either a rapid oscillation so that the negative derivative is correct, or rapid growth in the higher order derivatives so that the error term is large.
    $endgroup$
    – LutzL
    Jan 29 at 9:35


















  • $begingroup$
    When you say "the formulation", I'm not sure that's the right way to think about this. Assuming your calculations are correct, your weird result could simply be a function of the particular way you approximated the first derivative. Just as there are lots of ways to approximate a function, there are lots of ways to approximate its derivative, even in only one direction.
    $endgroup$
    – Adrian Keister
    Jan 28 at 17:15






  • 1




    $begingroup$
    For a function like $5^x$ a step size of $h=1$ is just too large. The error term $sim ln(5)^35^hh^2$ should be smaller than the result, so $ln(5)h<0.5$ is necessary to start to get good results.
    $endgroup$
    – LutzL
    Jan 28 at 21:49










  • $begingroup$
    @LutzL, I was not meaning $5^x$, my third point could have also been 23, but yes, the derivative is apparently too large to get good results. Thanks!
    $endgroup$
    – pugliam
    Jan 29 at 9:13












  • $begingroup$
    I was just looking for a simple growing function that had the same type of growth in value and first derivative. All other interpolations will have the same type of behavior, either a rapid oscillation so that the negative derivative is correct, or rapid growth in the higher order derivatives so that the error term is large.
    $endgroup$
    – LutzL
    Jan 29 at 9:35
















$begingroup$
When you say "the formulation", I'm not sure that's the right way to think about this. Assuming your calculations are correct, your weird result could simply be a function of the particular way you approximated the first derivative. Just as there are lots of ways to approximate a function, there are lots of ways to approximate its derivative, even in only one direction.
$endgroup$
– Adrian Keister
Jan 28 at 17:15




$begingroup$
When you say "the formulation", I'm not sure that's the right way to think about this. Assuming your calculations are correct, your weird result could simply be a function of the particular way you approximated the first derivative. Just as there are lots of ways to approximate a function, there are lots of ways to approximate its derivative, even in only one direction.
$endgroup$
– Adrian Keister
Jan 28 at 17:15




1




1




$begingroup$
For a function like $5^x$ a step size of $h=1$ is just too large. The error term $sim ln(5)^35^hh^2$ should be smaller than the result, so $ln(5)h<0.5$ is necessary to start to get good results.
$endgroup$
– LutzL
Jan 28 at 21:49




$begingroup$
For a function like $5^x$ a step size of $h=1$ is just too large. The error term $sim ln(5)^35^hh^2$ should be smaller than the result, so $ln(5)h<0.5$ is necessary to start to get good results.
$endgroup$
– LutzL
Jan 28 at 21:49












$begingroup$
@LutzL, I was not meaning $5^x$, my third point could have also been 23, but yes, the derivative is apparently too large to get good results. Thanks!
$endgroup$
– pugliam
Jan 29 at 9:13






$begingroup$
@LutzL, I was not meaning $5^x$, my third point could have also been 23, but yes, the derivative is apparently too large to get good results. Thanks!
$endgroup$
– pugliam
Jan 29 at 9:13














$begingroup$
I was just looking for a simple growing function that had the same type of growth in value and first derivative. All other interpolations will have the same type of behavior, either a rapid oscillation so that the negative derivative is correct, or rapid growth in the higher order derivatives so that the error term is large.
$endgroup$
– LutzL
Jan 29 at 9:35




$begingroup$
I was just looking for a simple growing function that had the same type of growth in value and first derivative. All other interpolations will have the same type of behavior, either a rapid oscillation so that the negative derivative is correct, or rapid growth in the higher order derivatives so that the error term is large.
$endgroup$
– LutzL
Jan 29 at 9:35










3 Answers
3






active

oldest

votes


















1












$begingroup$

A difference quotient with three terms should be exact for quadratics. You can even use this fact to derive difference quotients. So, let's fit a quadratic to the points ${(0,0),(h,5),(2h,25)}$; we find that
$$
f(x) = frac{15 x^2}{2 h^2}-frac{5 x}{2 h}.
$$

It's easy enough to check that this quadratic passes through the desired points. It's also easy to check that
$$
f'(0)=frac{4 f(h)-f(2 h)-3 f(0)}{2 h}=-frac{5}{2 h}
$$

and, in particular, that the derivative is negative when $h>0$. In fact, it grows in absolute value as $hsearrow0$.






share|cite|improve this answer









$endgroup$





















    -1












    $begingroup$

    Using
    $f(x+h)
    =f(x)+hf'(x)+h^2f''(x)/2+O(h^3)$
    ,
    we get



    $begin{array}\
    dfrac{-3f(0)+4f(h)-f(2h)}{2h}
    &=dfrac{-3f(0)+4(f(0)+hf'(0)+h^2f''(0)/2+O(h^3))-(f(0)+2hf'(0)+4h^2f''(0)/2+O(h^3))}{2h}\
    &=dfrac{2hf'(0)-3h^2f''(0)/2+O(h^3)}{2h}\
    &=f'(0)-3hf''(0)/2+O(h^2)\
    end{array}
    $



    so the approximation goes
    in the opposite direction
    than $f''(0)$.






    share|cite|improve this answer









    $endgroup$













    • $begingroup$
      The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
      $endgroup$
      – LutzL
      Jan 29 at 9:30



















    -1












    $begingroup$

    Try
    $$ f'(0)approx frac{(2-h)f(2h)-(4-4h)f(h) +(2-3h)f(0)}{2h^2}.$$






    share|cite|improve this answer











    $endgroup$













    • $begingroup$
      That's a difference quotient for $f''(h)$.
      $endgroup$
      – Mark McClure
      Jan 29 at 2:31












    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.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "69"
    };
    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',
    autoActivateHeartbeat: false,
    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
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3091132%2fsecond-order-approximation-of-first-derivative-gives-odd-results%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1












    $begingroup$

    A difference quotient with three terms should be exact for quadratics. You can even use this fact to derive difference quotients. So, let's fit a quadratic to the points ${(0,0),(h,5),(2h,25)}$; we find that
    $$
    f(x) = frac{15 x^2}{2 h^2}-frac{5 x}{2 h}.
    $$

    It's easy enough to check that this quadratic passes through the desired points. It's also easy to check that
    $$
    f'(0)=frac{4 f(h)-f(2 h)-3 f(0)}{2 h}=-frac{5}{2 h}
    $$

    and, in particular, that the derivative is negative when $h>0$. In fact, it grows in absolute value as $hsearrow0$.






    share|cite|improve this answer









    $endgroup$


















      1












      $begingroup$

      A difference quotient with three terms should be exact for quadratics. You can even use this fact to derive difference quotients. So, let's fit a quadratic to the points ${(0,0),(h,5),(2h,25)}$; we find that
      $$
      f(x) = frac{15 x^2}{2 h^2}-frac{5 x}{2 h}.
      $$

      It's easy enough to check that this quadratic passes through the desired points. It's also easy to check that
      $$
      f'(0)=frac{4 f(h)-f(2 h)-3 f(0)}{2 h}=-frac{5}{2 h}
      $$

      and, in particular, that the derivative is negative when $h>0$. In fact, it grows in absolute value as $hsearrow0$.






      share|cite|improve this answer









      $endgroup$
















        1












        1








        1





        $begingroup$

        A difference quotient with three terms should be exact for quadratics. You can even use this fact to derive difference quotients. So, let's fit a quadratic to the points ${(0,0),(h,5),(2h,25)}$; we find that
        $$
        f(x) = frac{15 x^2}{2 h^2}-frac{5 x}{2 h}.
        $$

        It's easy enough to check that this quadratic passes through the desired points. It's also easy to check that
        $$
        f'(0)=frac{4 f(h)-f(2 h)-3 f(0)}{2 h}=-frac{5}{2 h}
        $$

        and, in particular, that the derivative is negative when $h>0$. In fact, it grows in absolute value as $hsearrow0$.






        share|cite|improve this answer









        $endgroup$



        A difference quotient with three terms should be exact for quadratics. You can even use this fact to derive difference quotients. So, let's fit a quadratic to the points ${(0,0),(h,5),(2h,25)}$; we find that
        $$
        f(x) = frac{15 x^2}{2 h^2}-frac{5 x}{2 h}.
        $$

        It's easy enough to check that this quadratic passes through the desired points. It's also easy to check that
        $$
        f'(0)=frac{4 f(h)-f(2 h)-3 f(0)}{2 h}=-frac{5}{2 h}
        $$

        and, in particular, that the derivative is negative when $h>0$. In fact, it grows in absolute value as $hsearrow0$.







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Jan 29 at 1:12









        Mark McClureMark McClure

        23.9k34472




        23.9k34472























            -1












            $begingroup$

            Using
            $f(x+h)
            =f(x)+hf'(x)+h^2f''(x)/2+O(h^3)$
            ,
            we get



            $begin{array}\
            dfrac{-3f(0)+4f(h)-f(2h)}{2h}
            &=dfrac{-3f(0)+4(f(0)+hf'(0)+h^2f''(0)/2+O(h^3))-(f(0)+2hf'(0)+4h^2f''(0)/2+O(h^3))}{2h}\
            &=dfrac{2hf'(0)-3h^2f''(0)/2+O(h^3)}{2h}\
            &=f'(0)-3hf''(0)/2+O(h^2)\
            end{array}
            $



            so the approximation goes
            in the opposite direction
            than $f''(0)$.






            share|cite|improve this answer









            $endgroup$













            • $begingroup$
              The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
              $endgroup$
              – LutzL
              Jan 29 at 9:30
















            -1












            $begingroup$

            Using
            $f(x+h)
            =f(x)+hf'(x)+h^2f''(x)/2+O(h^3)$
            ,
            we get



            $begin{array}\
            dfrac{-3f(0)+4f(h)-f(2h)}{2h}
            &=dfrac{-3f(0)+4(f(0)+hf'(0)+h^2f''(0)/2+O(h^3))-(f(0)+2hf'(0)+4h^2f''(0)/2+O(h^3))}{2h}\
            &=dfrac{2hf'(0)-3h^2f''(0)/2+O(h^3)}{2h}\
            &=f'(0)-3hf''(0)/2+O(h^2)\
            end{array}
            $



            so the approximation goes
            in the opposite direction
            than $f''(0)$.






            share|cite|improve this answer









            $endgroup$













            • $begingroup$
              The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
              $endgroup$
              – LutzL
              Jan 29 at 9:30














            -1












            -1








            -1





            $begingroup$

            Using
            $f(x+h)
            =f(x)+hf'(x)+h^2f''(x)/2+O(h^3)$
            ,
            we get



            $begin{array}\
            dfrac{-3f(0)+4f(h)-f(2h)}{2h}
            &=dfrac{-3f(0)+4(f(0)+hf'(0)+h^2f''(0)/2+O(h^3))-(f(0)+2hf'(0)+4h^2f''(0)/2+O(h^3))}{2h}\
            &=dfrac{2hf'(0)-3h^2f''(0)/2+O(h^3)}{2h}\
            &=f'(0)-3hf''(0)/2+O(h^2)\
            end{array}
            $



            so the approximation goes
            in the opposite direction
            than $f''(0)$.






            share|cite|improve this answer









            $endgroup$



            Using
            $f(x+h)
            =f(x)+hf'(x)+h^2f''(x)/2+O(h^3)$
            ,
            we get



            $begin{array}\
            dfrac{-3f(0)+4f(h)-f(2h)}{2h}
            &=dfrac{-3f(0)+4(f(0)+hf'(0)+h^2f''(0)/2+O(h^3))-(f(0)+2hf'(0)+4h^2f''(0)/2+O(h^3))}{2h}\
            &=dfrac{2hf'(0)-3h^2f''(0)/2+O(h^3)}{2h}\
            &=f'(0)-3hf''(0)/2+O(h^2)\
            end{array}
            $



            so the approximation goes
            in the opposite direction
            than $f''(0)$.







            share|cite|improve this answer












            share|cite|improve this answer



            share|cite|improve this answer










            answered Jan 29 at 1:57









            marty cohenmarty cohen

            74.9k549130




            74.9k549130












            • $begingroup$
              The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
              $endgroup$
              – LutzL
              Jan 29 at 9:30


















            • $begingroup$
              The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
              $endgroup$
              – LutzL
              Jan 29 at 9:30
















            $begingroup$
            The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
            $endgroup$
            – LutzL
            Jan 29 at 9:30




            $begingroup$
            The second derivative terms cancel, that's why this is a second order derivative approximation. You will need the third derivative term to get the coefficient for the leading error term.
            $endgroup$
            – LutzL
            Jan 29 at 9:30











            -1












            $begingroup$

            Try
            $$ f'(0)approx frac{(2-h)f(2h)-(4-4h)f(h) +(2-3h)f(0)}{2h^2}.$$






            share|cite|improve this answer











            $endgroup$













            • $begingroup$
              That's a difference quotient for $f''(h)$.
              $endgroup$
              – Mark McClure
              Jan 29 at 2:31
















            -1












            $begingroup$

            Try
            $$ f'(0)approx frac{(2-h)f(2h)-(4-4h)f(h) +(2-3h)f(0)}{2h^2}.$$






            share|cite|improve this answer











            $endgroup$













            • $begingroup$
              That's a difference quotient for $f''(h)$.
              $endgroup$
              – Mark McClure
              Jan 29 at 2:31














            -1












            -1








            -1





            $begingroup$

            Try
            $$ f'(0)approx frac{(2-h)f(2h)-(4-4h)f(h) +(2-3h)f(0)}{2h^2}.$$






            share|cite|improve this answer











            $endgroup$



            Try
            $$ f'(0)approx frac{(2-h)f(2h)-(4-4h)f(h) +(2-3h)f(0)}{2h^2}.$$







            share|cite|improve this answer














            share|cite|improve this answer



            share|cite|improve this answer








            edited Jan 29 at 3:07

























            answered Jan 29 at 1:38









            John Z. LiJohn Z. Li

            194




            194












            • $begingroup$
              That's a difference quotient for $f''(h)$.
              $endgroup$
              – Mark McClure
              Jan 29 at 2:31


















            • $begingroup$
              That's a difference quotient for $f''(h)$.
              $endgroup$
              – Mark McClure
              Jan 29 at 2:31
















            $begingroup$
            That's a difference quotient for $f''(h)$.
            $endgroup$
            – Mark McClure
            Jan 29 at 2:31




            $begingroup$
            That's a difference quotient for $f''(h)$.
            $endgroup$
            – Mark McClure
            Jan 29 at 2:31


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • 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.


            Use MathJax to format equations. MathJax reference.


            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%2fmath.stackexchange.com%2fquestions%2f3091132%2fsecond-order-approximation-of-first-derivative-gives-odd-results%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory