Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008, 11:23 PM
Member
 
Join Date: Apr 2008
Posts: 6
adelgado0723 is on a distinguished road
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!!!
Attached Files
File Type: zip Lab 4.zip (13.4 KB, 5 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-03-2008, 05:51 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-03-2008, 09:09 PM
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-03-2008, 10:45 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,142
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-03-2008, 10:58 PM
Member
 
Join Date: Apr 2008
Posts: 6
adelgado0723 is on a distinguished road
thanks! this seems to work!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 04:32 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org