C++ Program Help (Newtons method)

In summary, the user is new to programming and is assigned to create a program 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. The problem is that the user keeps getting a infinite loop showing the first x0 and x1 values. The user thinks the loop isn't updating the x1 and x0 values after each iteration. The user needs to fix a mistake to
  • #1
physics724334
4
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;

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

x0 = ( y/4);

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

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

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

}

return 0;
}
 
Physics news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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:
  • #5
physics724334 said:
it shows x1 and x0 as the same value

and what does your loop test do when that's the case?
 
  • #6
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
 
  • #7
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.
 
  • #8
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);

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

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



thanks for all the help ducks and phinds
 
  • #9
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?
 

1. What is the Newton's method algorithm in C++?

The Newton's method algorithm is a root-finding algorithm used to find the roots of a given function. It is based on the concept of using an initial guess and iteratively improving that guess until a satisfactory approximation of the root is found.

2. How do I implement Newton's method in my C++ program?

To implement Newton's method in your C++ program, you will need to define the function you want to find the root of, as well as its derivative. Then, use a loop to iterate through the algorithm until the desired accuracy is achieved. Finally, use the calculated root as the solution to your problem.

3. What are the advantages of using Newton's method in C++?

Newton's method is a fast and efficient algorithm for finding roots of a function. It converges quickly and can handle complex functions with multiple roots. Additionally, it only requires knowledge of the function and its derivative, making it easy to implement in C++.

4. How do I choose the initial guess for Newton's method in C++?

The initial guess for Newton's method should be close to the actual root, but it does not have to be exact. It is usually chosen based on the experience and intuition of the programmer, but there are also methods for finding a good initial guess, such as the bisection method.

5. Can Newton's method fail in my C++ program?

Yes, Newton's method can fail in some cases. If the initial guess is too far from the actual root or if the function has a flat region where the derivative is close to 0, the algorithm may not converge or may converge to a wrong root. It is important to test the algorithm with different initial guesses and check the results for accuracy.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
761
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top