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 01-03-2008, 12:12 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Root Finder for polynomials
Hey guys, i've been working on a program that finds the roots of a complex function in Pre-Calculus (x^3 + 2x^2 - 4x + 1 or something like that), and i'm not doing this to help me with my math, i just enjoy making programs that are useful in math and physics. Now, i've already made a class that does Synthetic Division for you, after putting in the root and all of the coefficients of the original function, and it returns the resulting function, and tells you whether or not the root you tested is a root of the function. This class works great, as seen through my tester class where the user enters the coefficients and the root they want to test.
So here's the problem. This isn't as efficient as i would like it to be. i want to make it so that the user doesn't have to enter in the possible root, it automatically finds the possible roots (when you do constant coefficient over leading coefficient, and you take the factors). So with all of this said, my problem is that i'm trying to figure out a way to get all of the possible roots.

MY IDEA FOR THIS: divide the number (leading and constant) by all of the numbers less than that number.

I hope this isn't too wordy or confusing, so let me give you an example.

x^3 + 3x^2 + 2x + 2

Finding the possible roots:

2/1 ( 1 , 2 )/ 1

So, the possible roots are -1, -2, 1, 2. (because you take the positive and the negative of each division).

So like i said, my idea is to test if the number divided by each individual number less than it is an int. (because if it were a decimal number, it is not a factor) .

Would it be possible to test if the resulting number is instanceof Integer?


Any help would be appreciated, and i'm sorry if explained it terribly =]
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-03-2008, 12:28 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 326
tim is on a distinguished road
Hello

I'm also into this stuff. I programmed a similar program in C# and I'm sure it's possible in Java. I would recommend that you use Newton's method for root finding. Just Google it. It's very fast. It's a bit tricky to list all the roots if you do not know where to begin. Why don't you try the turning points of the polynomials. That should give you a good starting point. A warning though: you cannot start Newton's method at a turning point.

Have fun!
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-03-2008, 12:44 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
k, i'll check out Newton's method.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-03-2008, 12:54 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
i realized a major flaw in my idea. no matter what, the quotient of the two numbers will always be an int, because when you divide an int by and int, you get an int no matter what. but, i cant make the numbers doubles because then the quotient will be a double no matter what (1.0 or something). damn. back at square 1.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-03-2008, 01:16 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 253
roots is on a distinguished road
Correct me if i am wrong

Code:
public class demo { public static void main(String[] args) { assert isInt(2.2) ; assert ! isInt(2.0) ; System.out.println("Passed simple test"); } public static boolean isInt(double number){ return ((Math.floor(number) - number ) == 0.0); } }
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-03-2008, 01:44 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
dude. i had no idea that such a method existed (floor). i just looked up the API, and yep, this should work nicely. thanks alot man!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-03-2008, 02:12 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 253
roots is on a distinguished road
No problem .. we are here .. lol
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-03-2008, 02:34 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
YESSSSSSSSSS i got everything working. It finds the possible roots. Now for the easy part, since i got my SyntheticDivision class working. I'll update you when the final program is done.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-03-2008, 11:31 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 326
tim is on a distinguished road
Hello gibsonrocker800.

I think that when you write a scientific program, you should always use the most accurate data type to represent a number, like Double.
You will need to differentiate a simple polynomial like the one you gave us initially. If you can't do differentiation then Newton's method won't help you much. In that case, look at the Bisection or Secant methods. Can you explain your SyntheticDivision class and what it does?

Thank you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-03-2008, 11:04 PM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Ok, my SyntheticDivision constructor calls for an array of doubles, the root you want to perform on, and the number of numbers (although this is kinda useless).
Here, its a very short simple class, so here's the code.

------------------------------------------------------

public class SyntheticDivision {

private double[] numbers;
private double root;
private double mid;
private double length;
public SyntheticDivision(double[] nums, double rt, double lth)
{
length = lth;
root = rt;
numbers = nums;
mid = 0;
}

public double findCoefficients(int in)
{

int index = in;
double coef = numbers[index] + mid;
mid = coef * root;
return coef;
}

}

-------------------------------------------------------------
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-03-2008, 11:45 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 326
tim is on a distinguished road
Okay then
Oh. I thought it was a class implementing some kind of root finding method. Thank you anyway.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-04-2008, 12:26 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
ahh, well, i'm working on a class RootFinder. and this class is what finds the root. (it uses SyntheticDivision)
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find nth root of a number perito New To Java 1 03-03-2008 07:51 AM
JSP/Servlet – getting root directory Java Tip Java Tips 0 01-31-2008 01:52 PM
Listing file system from root directory Java Tip Java Tips 0 01-14-2008 10:32 AM
Library Finder 1.2 JavaBean Java Announcements 0 07-15-2007 05:43 PM
Context Root change rvasu_77 Advanced Java 2 07-12-2007 11:18 AM


All times are GMT +3. The time now is 03:21 AM.


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