1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
//David Wood October 15,2018
//Finding the derivative of a function using The limit definition
// This program will take user input for the coefficients of a polynomial and a x value and calculate the derivative to a tolerance of .001.
// The code will be written using user defined functions and information will be passed by reference
#include <iostream>
#include <iomanip>
#include <cmath>
#include <iomanip>
using namespace std;
void 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 determine the derivative at x" << endl;
}
double Input (double &C1 , double &C2, double &C3 , double &X ){
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 >> X;
return (X);
}
double Function(double C1, double C2, double C3, double X , double &F_of_x ) {
F_of_x = (C1 * pow(X, 2)) + (C2 * X) + C3; // value for f(x)
cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;
return (F_of_x);
}
double Prime(double &FxDeltax , double X , double &DeltaX , double &FPrime, double C1, double C2, double C3, double F_of_x) {
//Defined functions for while loop
FxDeltax = (X + DeltaX);// f(x+deltax)
FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX); // Value for fprime
return (FPrime);
}
int main() {
double C1;
double C2;
double C3;
double DeltaX = .1;
double F_of_x;
double TestTolerance = .001;
double X;
double FxDeltax;
double FPrime;
int Number_of_loops = 0;
Input(C1, C2, C3, X);
Function(C1, C2, C3, X, F_of_x);
Prime(FxDeltax, DeltaX, FPrime, C1, C2, C3, X, F_of_x );
/*
while (abs(FPrime - TestTolerance) > .001) {
DeltaX = DeltaX / 2;
TestTolerance = FPrime;
FxDeltax = (X + DeltaX);
FPrime = ((C1 * (pow(FxDeltax, 2)) + (C2 * (FxDeltax)) + C3) - (F_of_x)) / (DeltaX);
Number_of_loops++;
}
*/
//cout << " The derivative of f(x) at x = " << X << " is :" << setprecision(3) << FPrime << endl;
// cout << "This code took " << Number_of_loops << " iterations to find the accuracy of the derivative" << endl;
system("pause");
return 0;
}
|