Finding the second derivative using central difference formula

Click For Summary

Discussion Overview

The discussion revolves around the implementation of a program to calculate the second derivative of the function pi(16 x^2 - y^4) at y=2 using the second-order Central Difference Formula. Participants explore issues related to numerical accuracy, programming errors, and the definition of the function itself.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) presents a program that attempts to compute the second derivative but encounters crashes and nonsensical output values.
  • One participant questions whether the program operates in double precision, suggesting that Excel handles similar calculations without issues.
  • Another participant seeks clarification on how the expression "pi(16 x^2 - y^4)" defines a function, indicating confusion about its form.
  • Multiple participants suggest that the function should be corrected to f(x) = pi(16 x^2 - x^4), with one asserting that the second derivative at x=2 is -16π.
  • There is a recognition of a typo in the OP's function, leading to further clarification about the intended expression.

Areas of Agreement / Disagreement

Participants express confusion regarding the function's definition and agree on the correction of the function to f(x) = pi(16 x^2 - x^4). However, the discussion about the program's output and numerical methods remains unresolved, with no consensus on the cause of the crashes or the validity of the results.

Contextual Notes

There are limitations regarding the assumptions made about the function's form and the numerical methods employed. The discussion highlights unresolved issues with the program's implementation and the accuracy of the numerical results.

Who May Find This Useful

This discussion may be useful for individuals interested in numerical methods for derivatives, programming in computational contexts, and clarifying mathematical expressions in applied scenarios.

Kanashii
Messages
9
Reaction score
0
Thread moved from the technical forums, so no Homework Help Template is shown.

Homework Statement


Develop aprogram that will determine the second derivative of pi(16 x^2 - y^4) at y=2 with step sizes of 0.1, 0.01, 0.001…. until the absolute error (numerical-analytical) converges to 0.00001. Use the 2nd order Central Difference Formula.
User Input: y, tolerance
Output: h, second derivative, error

Homework Equations


[f(x+h) - 2f(x) + f(x-h)]/h^2

The Attempt at a Solution


Code:
do
    {
        n[0]= h;
        n[i+1]=n/10;
        f= function (y,n);
        error[0]= error_function(true_value,f);
        error[i+1]= error_function(f,f[i-1]);
        printf("%lf        %lf       %lf\n",n,f,error);
        i++;
    }
    while (error > tolerance || error != tolerance);

When I input 0.00001 (tolerance) and 2 (y) into the program I created, the program crashes but it got the values of h, f`` right and also some values for the error. I do know what to change.
Thank you.

Output:

h ----------------------------f``----------------------------error
0.100000--------------- -50.328314 --------------- 0.062832
0.010000-------------- -50.266111 --------------- 50.328314
0.001000--------------- -50.265489 --------------- 0.062204
0.000100--------------- -50.265481--------------- 0.000622
0.000010--------------- -50.265498--------------- 0.000007
0.000001--------------- -50.249582--------------- 0.000017
0.000000--------------- -54.001248--------------- 0.015916
0.000000--------------- -284.217094--------------- 3.751666
0.000000--------------- 0.000000--------------- 230.215846

This table would also go on and the values do not make any sense.
 
Last edited:
Physics news on Phys.org
Hi Kanashii, :welcome:

Does your program work in double precision ? Excel does, and it has no problem
Fortran:
       0.1 -50.3283143105095 -0.06283185
      0.01 -50.2661107763487 -0.00062832
     0.001 -50.2654887593678 -0.00000630
    0.0001 -50.2654776874780  0.00000477
   0.00001 -50.2654866164213 -0.00000416
  0.000001 -50.2699510880988 -0.00446863
     1E-07 -53.5736601294126 -3.30817767
But, as you see, it runs out of steam only a little bit later after hitting 4e-5 error. Phew...
 
Perhaps I'm being a bit slow today, but can you explain how "pi(16 x^2 - y^4)" defines a function?
 
Pick 16x^2-x^4
 
BvU said:
Pick 16x^2-x^4
Thanks. Still not ringing a bell. I've not come across this before (at least not in this form). Do you happen to have a reference I can have a look at?
 
Bit slow all right ...:rolleyes:
  • I'm not the OP
  • his (or her) function as object of study is ##\ \pi(16x^2-x^4)\ ##. The second derivative at ##x=2## is ##-16\pi##
 
BvU said:
Bit slow all right ...:rolleyes:
  • I'm not the OP
  • his (or her) function as object of study is ##\ \pi(16x^2-x^4)\ ##. The second derivative at ##x=2## is ##-16\pi##
Ah :doh: So there was a typo in the original (no y in the expression) and the function was meant to be ##f(x) = \pi(16x^2-x^4)##. Thanks for the clarification. My mind was going places like implicit differentiation...
 
How is Kanashii doing ?