java In this assignment you will write a java program that determines the real r
ID: 3667359 • Letter: J
Question
java
In this assignment you will write a java program that determines the real roots of a polynomial that lie within a specified range. Recall that the roots (or zeros) of an nth degree polynomial are those numbers x for which f(x)0. In general an nth degree polynomial has at most n roots, some of which may be real and some complex. Your program, which will be called Roots.java, will find only the real roots. Program Operation Your program will prompt the user to enter the degree of the polynomial, the coefficients, and the left and right endpoints of the interval in which to search. For example, to find the roots of the cubic polynomial x3-6x2 1x-6 in the interval [-10, 10], your program would run as follows java Roots 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 Notice that the user enters the coefficients from lowest power of x to highest power. The degree can be any non-negative integer, while the coefficients and range limits are doubles. No error checking on these inputs are required for this project. You may therefore assume that your program will be tested only on inputs that conform to these requirements. The root values will be rounded to 5 decimal places of accuracy Consider the fourth degree polynom ial X4-X2-2-(x2-2)(x2 + 1), written in factored form it is easy to if you are see that the roots are V2,- V2,/, and -i. (See http://en.wikipedia org/wiki/Imaginary unit unfamiliar with the number i.) As we can see the program does not the find complex roots. % java Roots Enter the degree: 4 Enter 5 coefficients: -2 0 -1 0 1 Enter the left and right endpoints:-5 5 Root found at -1.41421 Root found at 1.41421 Your program will find only those roots that are contained in the interval specified by the user. If no roots are found in that interval, a message to that effect is printed to stdout. We illustrate this with two more searches on the same polynomial x4-2-2-(x2-2)(x2 +DExplanation / Answer
Roots.java
import java.lang.*;
import java.text.DecimalFormat;
import java.util.Scanner;
class HelloWorld {
//initializes globals of sorts
static double resolution = Math.pow(10, -2);
static double tolerance = Math.pow(10, -7);
static double threshold = Math.pow(10, -3);
public static void main(String args[]) {
System.out.print("Enter the degree: ");
int degree=takeInt();
int numCo=degree+1;
System.out.print("Enter the "+(numCo)+" coefficients: ");
double[] coEfs = takeDoubleArray(numCo);
double[] derivCo = diff(coEfs);
System.out.print("Enter the left and right endpoints: ");
double[] endPoints = takeDoubleArray(2);
double leftPoint = endPoints[0];
double rightPoint = endPoints[1];
System.out.print(" ");
double root=0;
double derivRoot=0;
boolean foundRoot=false;
//finds the number of & splits the intervals to check between the endpoints
int intervals= (int) ((rightPoint-leftPoint)/resolution);
double[] intPoint;
intPoint = new double[intervals+1];
for(int i=0; i<(intervals+1); i++) intPoint[i] = leftPoint + (i*resolution);
DecimalFormat df = new DecimalFormat("#.00000");
for(int i=0; i<intervals; i++) {
//looks for odd roods, & prints them
if( ((poly(coEfs, intPoint[i])<=0) && (poly(coEfs, intPoint[i+1])>0)) || ((poly(coEfs, intPoint[i])>=0) && (poly(coEfs, intPoint[i+1])<0)) ) {
root=findRoot(coEfs, intPoint[i], intPoint[i+1], tolerance);
foundRoot=true;
root=root*100000;
root=(double)Math.round(root)/100000;
System.out.println("Root found at "+df.format(root) );
}
//looks for even roots & prints them
if( ((poly(derivCo, intPoint[i])<0) && (poly(derivCo, intPoint[i+1])>0)) || ((poly(derivCo, intPoint[i])>=0) && (poly(derivCo, intPoint[i+1])<0)) ) {
derivRoot=findRoot(derivCo, intPoint[i], intPoint[i+1], tolerance);
if(Math.abs(poly(coEfs, derivRoot))<threshold) {
foundRoot=true;
root=(double)Math.round(root*100000)/100000;
System.out.println("Root found at "+df.format(derivRoot) );
}
}
}
//the above checks [L, R), given findRoot's algorithm. this checks the final endpoint so [L,R] has been checked.
if(poly(coEfs, endPoints[1])==0) {
foundRoot=true;
System.out.println("Root found at "+df.format(endPoints[1]) );
}
//prints if no roots are found
if(foundRoot==false) System.out.println("No roots were found in the specified range.");
}
//given coefficients & value of x, finds value of a polynomial
static double poly(double[] C, double x) {
double sum=0;
for(int i=0; i<C.length; i++) {
sum=sum+C[i]*(Math.pow(x, i));
}
return sum;
}
//derivative by power rule
static double[] diff(double[] C) {
double[] result;
result = new double[C.length-1];
for(int i=0; i<C.length-1; i++) result[i]=C[i+1]*(i+1);
return result;
}
//finds an approximation of a root within a given interval of a polynomial within tolerance
static double findRoot(double[] C, double a, double b, double tol) {
double left=a;
double right=b;
double mid=(a+b)/2;
double midPoint=poly(C, mid);
double leftPoint=poly(C, a);
double rightPoint=poly(C, b);
//if the interval goes from negative to positive
if(leftPoint<0) {
while(Math.abs(midPoint)>tol) {
if(midPoint<0) {
left=mid;
}
if(midPoint>0) {
right=mid;
}
mid=((left+right)/2);
midPoint=poly(C, mid);
}
}
//if the interval goes from positive to negative
if(leftPoint>0) {
while(Math.abs(midPoint)>tol) {
if(midPoint<0) {
right=mid;
}
if(midPoint>0) {
left=mid;
}
mid=((left+right)/2);
midPoint=poly(C, mid);
}
}
//if the endpoints ARE the roots, only takes the left to not receive duplicates
//however, this means through the whole program, [L, R) is checked.
if(leftPoint==0) mid=left;
return mid;
}
//takes a given integer
static int takeInt() {
Scanner in = new Scanner(System.in);
int result=0;
while(true) {
while( !in.hasNextInt() ) {
in.next();
System.out.print("Please enter a positive integer: ");
}
result = in.nextInt();
if( result>0 ) break;
System.out.print("Please enter a positive integer: ");
}
return result;
}
//provides a reference for an array of doubles given input
//Note: Doesn't seem to like infiles.
static double[] takeDoubleArray(int a) {
Scanner in = new Scanner(System.in);
String deLimiter="[ ]+";
String input;
input = in.nextLine();
String[] inputTokens = input.split(deLimiter);
double[] result = new double[a];
for(int i=0; i<a; i++) result[i] = Double.parseDouble(inputTokens[i]);
return result;
}
}
Sample Output
sh-4.3$ javac HelloWorld.java
sh-4.3$ java -Xmx128M -Xms16M HelloWorld
Enter the degree: 4
Enter the 5 coefficients: -2 0 -1 0 1
Enter the left and right endpoints: 0 2
Root found at 1.41421
sh-4.3$ javac HelloWorld.java
sh-4.3$ java -Xmx128M -Xms16M HelloWorld
Enter the degree: 4
Enter the 5 coefficients: -2 0 -1 0 1
Enter the left and right endpoints: 0 1
No roots were found in the specified range.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.