February 18th, 2014, 09:32 PM

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.length1;
int K;
double sum=0.0;
for(int i=0;i<n;i++){
sum+=C[i]*(Math.pow((xi),n));
}
return sum;
}
static double[] diff(double[] C){
int n=C.length1;
int K;
double[]D=new double[n];
for(int i=0;i<n;i++){
D[i]=C[i]*(n1);
}
return D;
}
static double findRoot(double[] C, double a, double b, double tolerance){
//loops here
}
}
February 20th, 2014, 02:26 PM

Newton's method is probably what you're supposed to use. The short explanation is that you pick a value to start at, calculate the line tangent to the function at that value (ie, the derivative) and where it intersects with y=0, and use that x value as the next place to get the tangent. Repeat until you end up with a f(x) value very close to 0.