Issues with Newton's Method

Hey Guys

I have to write a Newton's Method program for a class I am in and I am running into some issues here. The program has to iterate through until x1 and x0 differ by 0.0001 or less and display x0 and x1 for each iteration. Also the assignment asks me to "use the conditional operator to compare the value of x0 to x1 so that the difference is a positive number". When I try and run this program all it does is give me an output of inf or when I switch the order of x1 and x0 in the first line of the do statement it just gives me infinite alternating outputs. Any help would be greatly appreciated.

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
 // Newton's Method

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

int main()
{
	double y, x1, x0, x2;
	cout << "Input the value that you wish to square root:\n";
	cin >> y;
	x0 = y / 4;
	
	do {
		x0 = x1;
		x1 = (x0 + (y / x0)) / 2;
	
		cout << "x0 = " << x0 << endl;
		cout << "x1 = " << x1 << endl;
		
	} while (abs(x1 - x0) <= 0.0001);

return 0;
		
 } 
Last edited on
What is the value of x1 on line 15 the first time through the loop?
Topic archived. No new replies allowed.