title: Why is my code for calculating derivatives using central difference method keep returning 0s and 1s?
body: <p>I'm trying to run a program that will calculate the first and second derivative of a linear function f(x)=x and a cubic function f(x)=x^3 using central difference. I need to use function prototypes to write both of those as functions that take a double as an argument and return a double. The program is supposed to feed the functions into the same derivative operator and then calculate the derivatives at x=0.5 and x=1. When I run the code and enter in a value for x, no matter what I enter I keep getting back 1 as the first derivative and 0 as the second (for both the linear and cubic function). I've been trying to figure out what I'm doing wrong for hours now and no matter what I try to do I still get back only 1s and 0s.</p>
  
  <p>I figure I could write this without using the function prototype, but I'm not supposed to do that and it's neater using the prototypes. I've been trying to find examples and nothing has really helped me because all of the functions declared (in the examples) have multiple parameters and are using if/else statements, and I don't, I also don't know if those would even help me.</p>
  
  <p>What am I doing wrong? (Sorry for the long code, I'm not sure where I've gone wrong so I didn't want to exclude anything).</p>
  
  <p>Edit: I excluded that the first and second derivative need to be calculated.</p>
  
  <pre><code>double linearFunctionPrime(double f1Prime);
  double linearFunctionDoublePrime(double f1DoublePrime);
  double cubicFunctionPrime(double f2Prime);
  double cubicFunctionDoublePrime(double f2DoublePrime);
  
  int main()
  {
     double x;
     double f1P, f1PP;
     double f2P, f2PP;
     cout &lt;&lt; "Enter a number to calculate the derivatives for f(x)=x and\
     f(x)=x^3" &lt;&lt; endl;
     cin &gt;&gt; x;
  
  
     cout &lt;&lt; setprecision(10);
     f1P = linearFunctionPrime(x);
     cout &lt;&lt; "f'(x) = " &lt;&lt; f1P &lt;&lt; endl;
     f1PP = linearFunctionDoublePrime(x);
     cout &lt;&lt; "f''(x) = " &lt;&lt; f1PP &lt;&lt; endl;
     f2P = cubicFunctionPrime(x);
     cout &lt;&lt; "f'(x^3) = " &lt;&lt; f2P &lt;&lt; endl;
     f2PP = cubicFunctionDoublePrime(x);
     cout &lt;&lt; "f''(x^3) = " &lt;&lt; f2PP &lt;&lt; endl;
  
     return 0;
  }
  
  double h = pow(10, -5);
  double linearFunctionPrime(double x)
  {
     double f1, f1Prime;
     f1 = x;
     f1Prime = ((f1+h)-(f1-h))/(2.0*h);
  
     return f1Prime;
  }
  double linearFunctionDoublePrime(double x)
  {
     double f1, f1DoublePrime;
     f1 = x;
     f1DoublePrime = ((f1+h)+(f1-h)-2.0*(x))/(pow(h, 2.0));
  
     return f1DoublePrime;
  }
  double cubicFunctionPrime(double x)
  {
     double f2, f2Prime;
     f2 = pow(x, 3.0);
     f2Prime = ((f2+h)-(f2-h))/(2.0*h);
  
     return f2Prime;
  }
  double cubicFunctionDoublePrime(double x)
  {
     double f2, f2DoublePrime;
     f2 = pow(x, 3.0);
     f2DoublePrime = ((f2+h)+(f2-h)-2.0*(f2))/(pow(h, 2.0));
  
     return f2DoublePrime;
  }
  </code></pre>