C++ Program Help (Newtons method)

  • Context: Comp Sci 
  • Thread starter Thread starter physics724334
  • Start date Start date
  • Tags Tags
    C++ Method Program
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment requiring the implementation of Newton's Method in C++ to calculate the square root of a number. Participants focus on debugging an infinite loop issue in the code and ensuring the correct updating of variables during iterations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes the initial problem of an infinite loop due to the incorrect assignment of values in the code.
  • Another participant suggests replacing the equality check (x0 == x1) with an assignment (x0 = x1) to update the guess correctly.
  • A later reply indicates that even after the suggested change, the values of x0 and x1 remain the same, leading to confusion about the loop's functionality.
  • Participants discuss the need for x1 to converge to the square root rather than simply equating x0 and x1.
  • One participant proposes testing the initial guess outside the loop to ensure proper updates during iterations.
  • Another participant shares a successful modification of the code structure, leading to the correct calculation of the square root.
  • There is a suggestion to use absolute value in the loop condition for clarity.

Areas of Agreement / Disagreement

Participants generally agree on the need to update the values of x0 and x1 correctly, but there are varying opinions on the best approach to structure the loop and the conditions for convergence.

Contextual Notes

Some participants note that the loop condition could be improved by using absolute differences, but this remains a point of discussion rather than a settled change.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in numerical methods and debugging techniques.

physics724334
Messages
4
Reaction score
0
I'm new to programming. my assignment wants me to create a program using c++ that can find the square root of any number using Newtons Method.

The user has to enter a number (y) and the program has to calculate the root and has to show each iteration. the initial guess (x0) has to be y/4

the iterations have to stop when the initial guess (x0) and the new guess (x1) are within 0.0001 of each other

Newtons method equation: x1 = (x0 + (y/x0))/2

the problem is that i keep getting a infinite loop showing the first x0 and x1 values
I think the loop isn't updating the x1 and x0 values after each iteration
how do i get the loop to update the x1 and x0 values
any help would be appreciated


here what i got for my code so far:

# include <iostream>
# include <math.h>

using namespace std;

int main ()
{
double y = 0, x1 = 0, x0 = 0;

count << "enter number" << endl;
cin >> y;

x0 = ( y/4);

while (!( x1 - x0 <= 0.0001 && x1 - x0 >= -0.0001))
{

x1 = ((x0 + (y/x0))/2);
x0 == x1;

count << "x0=" << x0 << "x1=" << x1 << endl;

}

return 0;
}
 
Physics news on Phys.org
This would probably work a whole lot better if you were to replace x0 with x1 (x0 = x1) instead of asking if they are the same (x0 == x1) and then not doing anything with the answer.

Once you fix THAT mistake, you'll find another.

EDIT: by the way, if you plan on doing much programming at ALL, you really should learn to use whatever debugger facility you have available.
 
phinds,

I changed x0 == x1 to x0 = x1 instead

This stopped the infinite loop, but it shows x1 and x0 as the same value.The loop still isn't updating x1 and x0. Is there something I'm missing, what's wrong with my source code.
 
physics724334 said:
phinds,

I changed x0 == x1 to x0 = x1 instead

This stopped the infinite loop, but it shows x1 and x0 as the same value.The loop still isn't updating x1 and x0. Is there something I'm missing, what's wrong with my source code.

x1 = ((x0 + (y/x0))/2);
x0 = x1;

in normal terms:

x1 = calculate a new value

x0 = update me to that new calculated value

Now x0 holds the same value as x1

When you print them out, you confirm this.

Which means your while loop is seeing X1-X0 (BOTH BEING EQUAL) = 0
This breaks the while loop.
edit:
May I suggest you attempt to solve this on paper minus the coding part. It is solved like how a while loop is coded.
I was curious how this worked so I looked it up.

http://mitpress.mit.edu/sicp/chapter1/node9.html

If you can solve it on paper you will see what you need to do to x0 in order to fix the error.
 
Last edited:
physics724334 said:
it shows x1 and x0 as the same value

and what does your loop test do when that's the case?
 
the loop ends when x1 and x0 are equal, but i don't need them to be equal i need x1 to be equal to the root of the y value
 
I suggest testing the first guess outside the while loop

x0 = (y/4);
x1 = (x0+(y/x0))/2;

while (!( x1 - x0 <= 0.0001 && x1 - x0 >= -0.0001))

It will enter the loop if it wasn't divisible by 4. Now you need a new guess to use. Update your guess x0 = something.

Then you can try your formula again. If it fails, the while loop will update your guess again.
 
thanks

that last reply helped me figure out what i was doing wrong
I put the equation, x1= (x0+(y/x0))/2 outside of the loop
and put x0 = x1 inside the loop along with x1= (x0+(y/x0))/2 and it worked

x0 = (y/4);
x1 = ((x0 + (y/x0))/2);

while (!( x1 - x0 <= 0.0001 && x1 - x0 >= -0.0001))
{
x0 = x1;
x1 = ((x0 + (y/x0))/2);

count << "x0 =" << x0 << "x1 =" << x1 << endl;
}

count << "The root is " << x1 << endl;



thanks for all the help ducks and phinds
 
Put your programs in [noparse]
Code:
[/noparse] tags:

Code:
while (!( x1 - x0 <= 0.0001 && x1 - x0 >= -0.0001))
{
   x0 = x1;
   x1 = ((x0 + (y/x0))/2);

   cout << "x0 =" << x0 << "x1 =" << x1 << endl; 
}

It doesn't break space and tab formatting.

BTW: why not abs(x0-x1) < 0.0001?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K