Results 1 to 4 of 4
 02222013, 05:03 AM #1Member
 Join Date
 Feb 2013
 Posts
 68
 Rep Power
 0
Newton's Square Root Method, need somebody to help see the problem.
Java Code:import java.util.Scanner; public class SquareRoot { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a value for (n)"); double n = in.nextDouble(); double x = 1, y = 1; x = y; while(Math.abs(x  y) <= 0.00001) { x=y; x = ((n / x) + x) / 2.0; } System.out.println("Newton("+n+") = "+x); System.out.println("Math.sqrt("+n+") = "+Math.sqrt(n)); } }
Enter a value for (n)
4
Newton(4.0) = 2.5
Math.sqrt(4.0) = 2.0
Last edited by LetsG0Blue; 02222013 at 05:14 AM.
 02222013, 05:25 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,248
 Rep Power
 9
Re: Newton's Square Root Method, need somebody to help see the problem.
Well, initially, xy == 0 which is less than .00001 so the loop goes thru once.
Then x = 2.5 and since 2.5 1 > .00001 the loop exits.
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 02222013, 05:31 AM #3Member
 Join Date
 Feb 2013
 Posts
 68
 Rep Power
 0
Re: Newton's Square Root Method, need somebody to help see the problem.
But if I put a > there it equals 1 for the output of Newton
 02222013, 05:38 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,248
 Rep Power
 9
Re: Newton's Square Root Method, need somebody to help see the problem.
That is because you chose x and y to be the same values to start so the loop would never enter if x = y = 1. So you could either chose different values for x and y. Or just change to a do while loop to force at least one iteration of the loop.
And I believe you want to set y = x.
JimLast edited by jim829; 02222013 at 05:47 AM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Creating recursion method to use Newton's method for square roots
By bdl1127 in forum New To JavaReplies: 2Last Post: 03232012, 05:53 AM 
How to work out square root of Total
By ls7897 in forum New To JavaReplies: 23Last Post: 03092011, 06:18 AM 
Find the square root with a particular method
By roud9 in forum New To JavaReplies: 2Last Post: 09272010, 11:39 PM 
Simple square root problem!
By nortski in forum New To JavaReplies: 7Last Post: 04012009, 02:11 PM 
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02252009, 02:21 PM
Bookmarks