Programming assignment To find Derivative

I have an assignment for one of my classes that requires me to find the derivative using the limit definition of a derivative. I set up a while loop to try to find the derivative as DeltaX approaches 0 (basically cutting it in half every time). My problem is that the while loop just reaches infinity and i dont know what to do to stop it and actually give me the result. It might just be right under my nose and i'm completely missing it but its worth a shot posting it here.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <iomanip>
using namespace std;


int main() {
//introduction
cout << " This program will calculate the derivative of a function at a point x with some help from the user" << endl;
cout << " The Quadratic function of c1x^2+c2x+c3 is the function that will be used" << endl;
cout << " You the user will give numerical imputs for c1,c2, and c3 and x to dertermine the derivative at x" << endl;


// function definitions

double C1;
double C2;
double C3;
double DeltaX = .1;
double F_of_x;
double TestTolerance = .001;
double XValue;
double FxDeltax;
double FPrime;
int Number_of_loops = 0;

// user input for c1,c2,c3
cout << " Please input values for c1, c2 ,and c3 : " << endl;
cin >> C1;
cin >> C2;
cin >> C3;

cout << " The function you entered is : " << C1 << "x^2 + " << C2 << "x + " << C3 << endl;

// Prompting user to input a x value to calculate the derivative at that point

cout << " Using this function the program will calculate the derivative at a x value that you input" << endl;
cout << " Please input the x value you wish to use to find the derivative : " << endl;
cin >> XValue;
F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3; // value for f(x)
cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;

//Defined functions for while loop
FxDeltax = (XValue + DeltaX);// f(x+deltax)
FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX); // Value for fprime

// While loop to get Derivative - TestTolerance < .001


while ((FPrime - TestTolerance) > .001) {
DeltaX = DeltaX / 2;

TestTolerance = FPrime;


FxDeltax = (XValue + DeltaX);

FPrime = ((C1 * (pow(FxDeltax, 2)) + (C2 * (FxDeltax)) + C3) + (F_of_x)) / (DeltaX);

Number_of_loops++;


}

cout << " The derivative of f(x) at x = " << XValue << " is :" << showpoint << FPrime << endl;
cout << "This code took " << Number_of_loops << " iterations to find the accuracy of the derivative" << endl;





























system("pause");
return 0;

}
FPrime - TestTolerance grows without bound. (e.g. somewhere in the magnitude of 10^32 in 100 iterations).

You know the derivative can just be calculated in constant time for a given polynomial, right?
(ax^2 + bx + c ---> 2ax + b).

Forget about the code for a second. Can you please explain the method or algorithm you are trying to implement, in words?

For example, if my input is 1x^2 + 1x + 1, at x = 1, what process do you expect to be happening? What should the answer be after 1 iteration, for example?

If we pretend that we can't just calculate the result in constant time, what I would do is the following:
Calculate:
- x1 = x - delta,
- x2 = x + delta,
- y1 = f(x - delta),
- y2 = f(x + delta),
- calculate slope = (y2 - y1) / (x2 - x1),
- and halve delta each time until old_slope ~= new_slope.

It's possible for this to have rounding errors due to the small denominator, though. Perhaps there's a better algorithm for this that is escaping my memory! I'm not sure, but I can probably help if you tell me what's supposed to be happening in your code.

Edit: Apparently there are ways to mitigate the rounding error with my method, see: https://en.wikipedia.org/wiki/Numerical_differentiation
"Practical considerations using floating point arithmetic"
Last edited on
So the assignment asked to implement a Tolerance (which is .001) which makes it so the derivative wont vary in the last 3 decimal places and delta x was to start at .1 and through each loop the deltax would be halved basically approaching 0. so if you put in 1 for each input and for x the code should calculate what f(x) is and then use that in the limit equation to calculate the derivative, and then continue to calculate the derivative until delta x is infinitely small and the derivative and tolerance are less than .001 meaning that the derivative and tolerance are about the same. The whole while loop is supposed to do that but i dont know why the code when ran comes out to infinity. I have it so TestTolerance would equal Fprime so eventually the two should equal each other . I don't know really how to explain it so maybe that helped,

This is the assignment)
The program to be written will determine the derivative of a quadratic function (form shown below) at
any given value of x with an accuracy of 3 decimal places using an iterative process for increasingly
smaller values of x until the value determined for the derivative does not vary in the first 3 decimal
places. The program will output to the user the equation describing the function, the value of x at which
the derivative is found, the numerical value of the derivative, and the number of iterations that were
required to get the necessary accuracy in decimal places.
The user must be able to enter whatever values desired for the coefficients {c1, c2, c3} and the value for x
at which the derivative is to be determined. The steps to take to find the derivative are:
1. Store values from user for c1, c2, c3, and x.
2. Calculate f(x) using the values from the user and retain that value in a variable (name of your
choosing).
3. Start with x = 0.1 and a variable called testTolerance = 0.001.
4. Calculate f(x+x) using the value of x given by the user and the current value of x.
5. Calculate f’(x) = (f(x+x) – f(x))/ x using the value calculated in step 2 and step 4 above and the
current value of x.
6. If (f’(x) – testTolerance) < 0.001 then the value calculated for f’(x) is within three decimal places
of the correct value for f’(x) and the work is done.
7. Otherwise, set the new value for x to be the old value divided by 2 and set testTolerance equal
to the current value of f’(x) and repeat steps 4 through 6.
Last edited on
Okay, thanks for taking the time to respond thoroughly.

In hindsight, I now see that it's obviously line FPrime = ((C1 * (pow(FxDeltax, 2)) + (C2 * (FxDeltax)) + C3) + (F_of_x)) / (DeltaX); that is attempting to do
f’(x) = (f(x + dx) – f(x))/ dx
.

Your issue is you're using a plus (+) instead of a minus (-).
1
2
FPrime = ((C1 * (pow(FxDeltax, 2)) + (C2 * (FxDeltax)) + C3) - (F_of_x)) / (DeltaX);
                                                             ^


You also should be doing the absolute difference between old and new, abs(FPrime - TestTolerance) > 0.001
Last edited on
Awesome, I knew that something stupid like that was the problem. got it all figured out now. Thank you so much
Topic archived. No new replies allowed.