-nan(ind)?

So I'm writing a program that demonstrates Newton's method to find a root of an equation. It outputs the estimated value of the root, the the true error and empirical error. After trying to run the program, the true error and estimated error were outputting "-nan(ind)". I stepped through my program and found out that the original function for Newton's method was returning this "-nan(ind)". What is "-nan(ind)" and how do I avoid it in this program?

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
float newton(int count, float aval, float bval)
{
	count++; //Increase count to avoid going past contraint
	float cval = (aval + bval) / 2; //Get midpoint of interval
	float estroot = cval; //Set estimated root equal to midpoint
	cout << estroot << endl; //Output was root is estimated to be right now

	if (count == 20) //If constraint is reached, end recursion
	{
		return estroot;
	}

	else if (equation(aval) * equation(cval) < 0) //If root is in between left value and midpoint,
												  //recur with that interval
	{
		estroot = newton(count, aval, cval);
	}

	else if (equation(cval) * equation(bval) < 0) //If root is in between midpoint and right value,
												  //recur with that interval
	{
		estroot = newton(count, cval, bval);
	}
}

int main()
{
	char end;
	float aval = 1.0; //Initial a value 
	float bval = 4.0; //Initial b value
	float realroot = 2.0; //Real root
	float estroot;
	int count = 0; //count variable to make sure number of iterations does not exceed 20

	estroot = newton(count, aval, bval); //Function to find root
	float trueerr;
	trueerr = realroot - estroot;
	float emperr;
	emperr = equation(estroot) / derivative(estroot);
	cout << "True error = " << trueerr << "\nEmpirical error = " << emperr << endl;

	cin >> end; //Pauses program so user can read output
}
NaN is "not a number", possibly resulting from some mathematical faux pas like dividing by 0, or using something undefined. Difficult to tell without full code.

You appear to be implementing the bisection method, not the Newton(-Raphson) method for root-finding. I would expect to see the latter involving a function and its derivative, not your "empirical error".

Can we see your equation( ) function, please: the function whose root you are trying to find.

Also note that your newton function won't know what to return if equation(cval) is exactly equal to 0 - which, of course, is the root you are seeking.

Is there any reason you are using recursion rather than the more straightforward iteration?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
float equation(float value)
{
	float output;

	output = 2 * (value * value) - 8; //Equation that we are trying to find root for, can be modified

	return output; //return output of equation
}

float derivative(float value)
{
	float output;

	output = 4 * value;

	return output;
}


Here's the code for the equation and it's first derivative. I'm using recursion even though it's less straightforward because I honestly need more practice in recursion and this gave me a great excuse to try it.
I also tried implementing a condition for equation == 0, but the same problem is still happening
Your original routine newton( ) - your original post - does not return ANYTHING unless count = 20.

To remedy this, simply insert
return estroot;
at the end of this function (between lines 23 and 24 in your original post).

When I checked it, your program then worked.


There are multiple potential problems with your approach.
- Just change your original line 19 to plain
else
Then at least one option will be followed.

- Your method is BISECTION and not NEWTON-RAPHSON (aka Newton's method).

- It would be far better done by iteration than recursion.

- You don't check that your original aval and bval actually span the root.

- dividing by the derivative in line 39 will fail for many functions (e.g. y = (x-2)cubed), where the root also has zero slope.



Topic archived. No new replies allowed.