User defined functions

Pages: 12
Okay, Much more understandable now. I feel like its all coming together. I'm sorry for getting frustrated. I should have made myself clear about not understand what was going on. It is a pretty simple concept after looking into it. I have a few questions though. I kept c1 c2 c3 as global and kept them defined where they were in my last post of my code, so for all the other variables that i need would i just define them in the respected function. Like in my calculation function ( now deriv function) would i define TestTolerance, DeltaX ,FPrime. so they are only defined in that one function? I also ran into a problem with this part of the function.
1
2
3
4
double F(double X) { // values set as points to pass reference to other defined functions
	
	return (C1 * pow(X, 2)) + (C2 * X) + C3;
}

I did it the way Lastchance suggested but when i build the program it gives me an error telling me that "X" is an undeclared identifier in my main function when i call the function F(x); , which makes no sense to me.
If you call F(double X), you need to fill in some value with X.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Example program
#include <iostream>
#include <cmath>

using namespace std;

// globals (avoid overusing)
double C1 = 1;
double C2 = 2;
double C3 = 3;

double F(double X) { // x is passed by value here
	return (C1 * pow(X, 2)) + (C2 * X) + C3;
}

int main()
{
    cout << F(0) << endl;
    cout << F(3) << endl;
    
    double my_local_x = 42; // storing a value in a variable. note that it's a local variable.
    cout << F(my_local_x) << endl;
}
Last edited on
Hello @draco,
That snippet of code appears to be OK. From what you say the error sounds like it is in your calling routine.

You declare X correctly for this function - but its scope is only this function. You could actually call that variable whatever you liked. When it is called from somewhere else the value will be copied into the argument X so that your function can use it.

Because this X only exists in this function (that's what we mean by its "scope" is only this function) if you use an 'X' somewhere else then that variable will also have to be declared somewhere else. Even though it has the same name it isn't the same variable as its scope will be restricted to somewhere else. It's like saying there is a Birmingham in England and also a Birmingham in the USA: they have the same name, but they are different places.

It may be as simple as declaring
double X;
at the start of whatever function your compiler says X is not declared in.

If you still have problems after looking at that then please repost your code (below) and we'll have another look at it.


Apologies: missed the rest of your question. Yes, you would declare the variables like DeltaX etc. within the deriv routine. We would say that they are "local variables" because they are only known in that function. Be a little careful in your deriv routine: because I was asking you to move all calculations of the derivative out of F() you will need to have the first estimate of the derivative worked out in deriv. A while loop would stop when the absolute difference between current and previous estimates of the derivative is less than the tolerance.
Last edited on
Okay so i'm having trouble following again. This is the modified code that ive worked out from your tips.

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
//David Wood October 18,2018
//Programming assignment CH6
// Finding derivative of a polynomial using the magic of user defined functions


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

//declared variables 
double C1; // user input 1	
double C2; // user input 2 
double C3; // user input 3

 // starting value for the tolerance of the function to calculate derivative in a loop






void Introduction() { // Defined function for the introduction of the code
	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;
	cout << " " << endl;

}

double Input() { // Defined Function for the user inputs ( Set as points to pass reference
	cout << "Please input your values for C1, C2 and C3: " << endl; // prompting user to input values
	cin >> C1 >> C2 >> C3; // user inputs for c1 c2 c3

	cout << " The function you entered is : " << C1 << "x^2 + " << C2 << "x + " << C3 << endl; // Shows user the function that they entered
	cout << " " << endl;
	double X; // defined variable for point at which derivative will be taken

	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; // user input for x value of function
	cout << " The x value the derivative will be calculated at is " << X << endl;

	return X;

}

// Defined user function for Variables of while loop
double F(double X) { // values set as points to pass reference to other defined functions
	
	return (C1 * pow(X, 2)) + (C2 * X) + C3;
}

void Deriv(double X) {
	
	double TestTolerance = .001;// starting value for the tolerance of the function to calculate derivative in a loop
	double DeltaX = .1; // value of deltax
	double FxDeltax; // defined function for F(x) + DeltaX
	double FPrime; // defined function for the first derivative of funtion 
	int Number_of_loops = 0; // counter for the number of iterations it took loop to calculate the code

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

		TestTolerance = FPrime;


		FxDeltax = (X + DeltaX);

		FPrime = (F(X + DeltaX) - F(X));
			//((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;


}






int main() {
	Introduction();
	Input();
	F(X);
	Deriv(X);


	system("pause");
	return 0;
}


Im getting an error for lines in my main function for F(X) saying that X is undeclared. I dont understand how or what to do to change that. I also tried adding double x into a few locations but that didnt do the trick
Last edited on
In main() you need to declare
double X;
right at the start of main() in order to use that local value of X in main(). Then you need to receive X from input:
X = Input();
(In fact you could do both of these together, but that's for another day.)

You should also remove the call to F(X) in main: it should only be called from Deriv. Delete the line
F(X);
in main().


Consider revamping your Deriv() function:
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
void Deriv(double X) {
        
        double TestTolerance = .001;    // when successive estimates of derivative differ by less than this you are done

        double DeltaX = 0.1;            // FIRST value of deltax

        double dFdX = ( F(X+DeltaX) - F(X) ) / DeltaX;
                                        // FIRST estimate of the derivative

        double dFdX_old = dFdX + 1.0;   // Start with ANYTHING SUFFICIENTLY DIFFERENT from dFdX

        int Number_of_loops = 0;        // counter for the number of iterations it took loop to calculate the code


        while ( abs( dFdX - dFdX_old ) > TestTolerance ) {     // here you compare successive values
                DeltaX = DeltaX / 2;

                dFdX_old = dFdX;                               // store previous value;
                dFdX = ( F(X+DeltaX) - F(X) ) / DeltaX;        // NEXT estimate of gradient
                Number_of_loops++;
        }
        
        cout << " The derivative of F(x) at x = " << X << " is : " << setprecision(3) << dFdX << endl;
        cout << "This code took " << Number_of_loops << " iterations to find the accuracy of the derivative " << endl;
}



There's a few niceties to improve afterwards, but this should get your code to work.
Last edited on
Okay, That all makes sense now. since i'm still new to this whole thing debugging and finding errors isn't easy. I should have just revamped my equation and that probably would have worked but i didn't think of it.
Okay so, i wrote the program like how it was suggested in here and it isnt the way my assignment wants me to write it. I was supposed to pass information by reference instead of using global and local variable. Im having trouble figuring out how to make that work. Im not sure if i completely understand the concept of it .
I redid the code real quick trying to piece things together by passing by reference but my code doesn't compute the derivative correctly and i dont know what im doing wrong

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;

}

Your uncommented code:
FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX); // Value for fprime
Your commented code:
FPrime = ((C1 * (pow(FxDeltax, 2)) + (C2 * (FxDeltax)) + C3) - (F_of_x)) / (DeltaX);

Same problem as your previous thread, be careful with those -/+'s.

Also, I see you're returning values from your functions, but this is unnecessary because you're passing by reference the same value you return.
If you don't do something with the return value, mark the function void.

e.g.
1
2
3
void 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)
}

Last edited on
ah shit okay that makes sense. I changed the mistakes but when i run the full code with my comments as actual code the derivative still comes out as inf so something still doesnt work
Last edited on
Turn warnings on your compiler on. I feel they will help you a lot.
In function 'int main()':
53:16: warning: 'FPrime' is used uninitialized in this function [-Wuninitialized]
71:9: note: 'FPrime' was declared here


This is what your Prime function's signature looks like:
double Prime(double &FxDeltax , double X , double &DeltaX , double &FPrime, double C1, double C2, double C3, double F_of_x)
This is how you're calling it:
Prime(FxDeltax, DeltaX, FPrime, C1, C2, C3, X, F_of_x );

1: FxDeltax, X, DeltaX, FPrime, C1, C2, C3, F_of_x
2: FxDeltax, DeltaX, FPrime, C1, C2, C3, X, F_of_x

Notice the problem?

Once you fix that, if you're still having trouble post your updated code.

Also your variable name called "TestTolerance" sounds very misleading, because you're essentially using it as the old value of FPrime.
Last edited on
Thank you. Im sorry for not finding little mistakes. I really need to get better at debugging my own programs.
Topic archived. No new replies allowed.
Pages: 12