### Thread: Determining the real roots of a polnomial within a given range

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2014
Posts
2
Rep Power
0

#### Determining the real roots of a polnomial within a given range

I am a beginner in coding especially in java and I have tried plenty of times figuring how to find the real roots of a polynomial within a given range. The program is supposed to find all the real roots of the given polynomial the user provided. For example, the program should run as follows: Enter the degree: 3 Enter 4 coefficients: -6 11 -6 1 Enter the left and right endpoints: -10 10 Root found at: 1.00000 Root found at: 2.00000 Root found at: 3.00000. Attached below is the format of my program.

[COLOR=Red]import java.util.Scanner;
class Roots{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double resolution=0.01;
double tolerance=0.0000001;
double threshold=0.001;
double roots;
System.out.print("Enter the degree: ");
int degree =sc.nextInt();
System.out.print("Enter "+(degree+1)+" coefficients: ");
double[] C=new double[degree+1];
for(int i=0; i<C.length;i++){
C[i]=sc.nextDouble();
}
System.out.print("Enter the left and right endpoints: ");
double a=sc.nextInt();
double b=sc.nextInt();
if(poly(C,a)*poly(C,b)<0){
roots=findRoot(C,a,b,tolerance);
}
}
}
static double poly(double[] C, double x){
int n=C.length-1;
int K;
double sum=0.0;
for(int i=0;i<n;i++){
sum+=C[i]*(Math.pow((x-i),n));
}
return sum;
}
static double[] diff(double[] C){
int n=C.length-1;
int K;
double[]D=new double[n];
for(int i=0;i<n;i++){
D[i]=C[i]*(n-1);
}
return D;
}
static double findRoot(double[] C, double a, double b, double tolerance){
//loops here
}
}
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
67
Rep Power
0
That's java, this is javascript. Might want to move this post...

• web_loone08 agrees : I have already mentioned it to the moderator. So they should be moving it, anytime.
3. Next time please wait for a moderator to move your thread before making a new one.

• requinix agrees : that one is actually a few hours *older*
4. Oh, oops. Thanks requinix. I had just looked at the time "9:32 PM" and missed that it said "yesterday" instead of "today".

In that case, jojohaha, please don't post the same question in multiple forums. Pick the one that you feel is best. (If it turns out another is better, a moderator can move your thread for you.)