Results 1 to 5 of 5

# Thread: Help Please

- 04-01-2008, 10:23 PM #1Member
- Join Date
- Apr 2008
- Posts
- 6

- Rep Power
- 0

## Help Please

Hey, I would like to just say hi to everyone as this is my first post. Honestly, I joined because I love java and want to learn more. I also need help for a class that I'm taking. I've done fine, but I got this assignment that I have no idea how to even approach. I attached it as a zip file to this post. Any help or direction would be really appreciated! Thanks!!!

- 04-03-2008, 04:51 PM #2
After downloading and examining your code, which does not appear to be in applet form, and since this is also an introduction, I've moved your thread to the New To Java sub-forum. Instead of posting zip files containing projects, which further contain classes that could possibly contain viruses- it is preferred that you simply enter the code that you have trouble with into your post- using [ code] [ /code] tags.

Note that you also include no specifications for your problem- thus how can we know what it is you need to have us take a look at ? All I see essentially is two source files, Math and TestMath, from that I cannot determine where you need help. You need to be specific. Therefore, please read the FAQ before you post again.

Thank you for your understanding and cooperation. Once you've done the above, I'm sure someone will be happy to assist.Vote for the new slogan to our beloved Java Forums! (closes**on****September 4,****2008**)

Want to voice your opinion on your IDE/Editor of choice? Vote now!

Got a little Capt'n in you?*(drink responsibly)*

- 04-03-2008, 08:09 PM #3Member
- Join Date
- Apr 2008
- Posts
- 6

- Rep Power
- 0

Java 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 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

- 04-03-2008, 09:45 PM #4Java Code:
public class NRTest { public static void main(String[] args) { NewtonRaphson f = new NewtonRaphson(); double x = 2; double root = f.solve(x); //System.out.println("root = " + root); } } class NewtonRaphson { // Our approximation of zero final double TOLERANCE = .000000005; // Maximum number of Newton's method iterations final int MAX_COUNT = 200; // f(x) = cos(x) private double f(double x) { return Math.sin(x); } // f'(x) = sin(x) private double fprime(double x) { return Math.cos(x); } public double solve(double x0) { System.out.printf(" %-8s%-13s%-12s%-16s%-15s%s%n", "n", "x_n", "f(x_n)", "f'(x_n)", "x_n+1", "dx"); double x = x0; int n = 0; do { x = x0 - f(x)/fprime(x); double dx = x - x0; System.out.printf("%2d %10f %10f %10f %10f %10f%n", n, x0, f(x), fprime(x), x, dx); x0 = x; if(Math.abs(dx) < TOLERANCE) break; } while(++n < MAX_COUNT); if( Math.abs(f(x)) <= TOLERANCE) { System.out.println("Zero found at x="+x); } else { System.out.println("Failed to find a zero"); } return x; } }

- 04-03-2008, 09:58 PM #5Member
- Join Date
- Apr 2008
- Posts
- 6

- Rep Power
- 0

## Bookmarks