Thread: Help Please
View Single Post
  #3 (permalink)  
Old 04-03-2008, 10:09 PM
adelgado0723 adelgado0723 is offline
Member
 
Join Date: Apr 2008
Posts: 6
adelgado0723 is on a distinguished road
Code:
class NewtonRaphson { static double f(double x) { return Math.sin(x); } static double fprime(double x) { return Math.cos(x); } public static void main(String argv[]) { double tolerance = .000000005; // Our approximation of zero int max_count = 200; // Maximum number of Newton's method iterations /* x is our current guess. If no command line guess is given, we take 0 as our starting point. */ double x = 2; if(argv.length==1) { x= Double.valueOf(argv[0]).doubleValue(); } for( int count=1; (Math.abs(f(x)) > tolerance) && ( count < max_count); count ++) { x= x - f(x)/fprime(x); System.out.println("Step: "+count+" x:"+x+" Value:"+f(x)); } if( Math.abs(f(x)) <= tolerance) { System.out.println("Zero found at x="+x); } else { System.out.println("Failed to find a zero"); } } }
this is what I have so far, but he wants us to use the JOptionpane to enter a number.
this is the assignment:
Write a class called NewtonRaphson that can be used to find an approximate solution of (square root of a) using Newton's method. i.e.
f(x) = x2 - a. The process should terminate when the positive difference between two consecutive approximations is sufficiently small or when the number of iterations exceeds some upper limit. Print the iteration sequence and the approximation for each iteration. (That is, in a tabular form).
Write a driver class called TestNewton. Use the following data to test the class NewtonRaphson.
• The initial guess is 2.0
• The process terminates when the difference between two consecutive approximations is less than 0.000000005
-----We are supposed to use Newton's method to do this (Newton-Raphson Method... I also know that I have to use the while loop.
-----Please help me out on this. I'm completely lost on this one!
thanks
Reply With Quote