Sei sulla pagina 1di 10

Jake Phillips

G Period Java
Horner's Method Writeup
In this program, the goal was to implement Horner's Method in order to calculate polynomials
and rational functions at specific domains in the most efficient way possible. As large amounts of input
were necessary for this program, this was also an opportunity to practice effectively catching errors.
Since polynomials are heavily used in mathematics and tend to get quite large, this algorithm
was chosen due to its efficiency in comparison to traditional repeated multiplication and addition.
Horner's method works by factoring the largest coefficient by x and adding the smallest
coefficient, then repeating the process until the entire polynomial is in a nested form.
This program will prompt the user on whether to compute a single polynomial or a rational
function.
If a single polynomial is chosen, the program will prompt the user for the degree, along with the
coefficients of the polynomial in ascending order. The user will then be asked for the start and end of
the domain and how many points to calculate upon. The program will then output a table with a list of
values at each point specified by the user.
If a rational function is chosen the program will prompt the user for the degree and coefficients of the
first polynomial, then the second. Afterwards, the start and end of the domain will be prompted for
along with the frequency of points. The program will then output a table in a similar fashion to a
singular polynomial.
To complete this project, I needed to implement a method to perform Horner's method as it was
an important part of this program. To do this I took the leading coefficient, and created a for loop to
multiply the current coefficient by the domain which was then added to the sum of the polynomial
value. This was repeated for each member of the domain. This process was repeated for rational
functions. since they're defined as the quotient of two polynomials, I took the value of the numerator
and divided it by the denominator

Code:
//Written by Jake Phillips
/*This program computes the y values of a polynomial at a specified domain using horner's
method along with Rational Functions in the same style.
It will exit if 3 user errors are made*/
import java.util.Scanner;
public class Polynomials {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int

choice,
//degree of polynomial
degree,
//numerator of rational function
deg1,
//denominator of rational function
deg2,
frequency = 0,
//arbitrarily named for purpose of incrementing functions where an additional
while loop wouldn't work
j = 0,
q = 0,
//for keeping track of number of errors the user made
eCount = 0;
String check = "Y";
boolean repeat = true;
//amount to increment domain by in calculation
double interval,
//first & last #'s in domain
dStart = 0,
dEnd = 0,
//for storing temporary value of rational function
raTemp;

//array for singular polynomial


double[] coefs;
//arrays for rational functions
double[] coefs1;
double[] coefs2;
while(repeat)
{

System.out.println("Enter 1 if you would like to compute a polynomial function,");


System.out.println("2 if you would like to compute a rational function, ");
System.out.println("Or -1 if you would like to exit");

if(in.hasNextInt() )
choice = in.nextInt();
switch(choice)
{

/*takes degree of polynomial, initializes the array that stores coefficients to be 1


larger than that,
inputs the coefficients, then takes the start and end of domain along with # of pts
to use with Horner's method*/
case 1:
System.out.println("What is the degree of the polynomial?(No more than 10");
if(in.hasNextInt())
{
degree = in.nextInt();
if(degree <= 10 && degree >= 0)
{
coefs = new double[degree + 1];
for(int Count = 0; Count < degree + 1; Count++)
{
System.out.println("What is the next coefficient in ascending
order?(enter 0 for non present ones)");
if(in.hasNextDouble())
{
coefs[Count] = in.nextDouble();
}
else
{
System.out.println("Please only enter numbers for the
coefficients");
eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered
too many errors and will now exit");
break;
}
}
}
domain?");

System.out.println("What is the first number in the

if(in.hasNextDouble())
{

dStart = in.nextDouble();
}
else
{
System.out.println("Please only enter numbers for the
domain");

eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too

many errors and will now exit");

break;

}
System.out.println( "What is the last number in the domain?");
if(in.hasNextDouble())
{
dEnd = in.nextDouble();
}
else
{
System.out.println("Please only enter numbers for the
domain");

eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too

many errors and will now exit");

break;

}
calculated?");

System.out.println("At how many points should the domain be


if(in.hasNextDouble())
{
frequency = in.nextInt();
//for incrementing the x val to calculate at
interval = (dEnd - dStart) / frequency;
System.out.println("X | Y");
//calculates Horner's method at specified # of pts
for(double nDom = dStart; nDom < dEnd; nDom +=interval)
{
System.out.println(nDom + " | " + Horners(nDom,

coefs));

domain");

}
}
else
{
System.out.println("Please only enter numbers for the
eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too

many errors and will now exit");

break;
}
}

}
}

/*Takes degree of numerator and inputs points, then takes degree of


denominator along with points,
Gets domain to calculate at, takes horner's of numerator and
denominator individually and outputs
quotient*/
case 2:
System.out.println("What is the degree of the numerator");
if(in.hasNextInt())
{
deg1 = in.nextInt();
coefs1 = new double[deg1 +1];
for(int Count = 0; Count < deg1 + 1; Count++)
{
System.out.println("What is the next coefficient in ascending order?
(enter 0 for non present ones)");
if(in.hasNextDouble())
{
coefs1[Count] = in.nextDouble();
}
else
{
System.out.println("Please only enter numbers for the coefficients");
eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too many
errors and will now exit");
break;
}
}
}
System.out.println("What is the degree of the denominator");
if(in.hasNextInt())
{
deg2 = in.nextInt();
coefs2 = new double[deg2 + 1];
for(int Count = 0; Count < deg2 + 1; Count++)
{
System.out.println("What is the next coefficient in ascending
order?(enter 0 for non present ones)");
if(in.hasNextDouble())
{
coefs2[Count] = in.nextDouble();
}
System.out.println("What is the first number in the domain?");
if(in.hasNextDouble())
{
dStart = in.nextDouble();

}
else
{
System.out.println("Please only enter numbers for

the domain");

eCount++;
if(eCount == 3)
{
System.out.println("This program has
encountered too many errors and will now exit");
break;
}
}
System.out.println( "What is the last number in the
domain?");

if(in.hasNextDouble())
{
dEnd = in.nextDouble();
}
else
{
System.out.println("Please only enter numbers for the

domain");

eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered

too many errors and will now exit");

break;

}
System.out.println("At how many points should the

domain be calculated?");

domain");

if(in.hasNextDouble())
{
frequency = in.nextInt();
}
else
{
System.out.println("Please only enter numbers for the
eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too

many errors and will now exit");

break;
}

interval)

interval = (dEnd - dStart) / frequency;


System.out.println();
System.out.println("X | Y");
for(double nDom = dStart; nDom < dEnd; nDom +=
{

raTemp = Horners(nDom, coefs1) / Horners(nDom,


coefs2);

System.out.println(nDom + " | " + raTemp);


}
}
else
{
System.out.println("Please only enter numbers for the

coefficients");

eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too

many errors and will now exit");

}
}
}
else
{

break;

System.out.println("Please only enter integers for the degree");


eCount++;
if(eCount == 3)

System.out.println("This program has encountered too many errors

and will now exit");

break;
}

}
else
{
System.out.println("Please only enter integers for the degree");
eCount++;
if(eCount == 3)

System.out.println("This program has encountered too many errors and will

now exit");
}

break;
}

//used to exit program due to running infinitely until user exits


case -1:
System.out.println("This program will now exit");
break;
}
}
System.out.println("Would you like to run again?(Y or N)");
check = in.next();
if(check.equalsIgnoreCase("y"))
{
repeat = true;
}

else if(check.equalsIgnoreCase("n"))
{
repeat = false;
}
else
{
System.out.println("Please only enter Y or N");
eCount++;
if(eCount == 3)
{
exit");

System.out.println("This program has encountered too many errors and will now
break;

}
}
/*in separate method due to repeated use along with ease of editing due to being most
important part of program
takes the current value of the domain, and the array of coefficients and uses horner's
method to find the value
of the polynomial
*/
public static double Horners(double xval, double[] coefs)
{
int degree = coefs.length - 1;
double poly = coefs[degree];
for(int q = 1; q < coefs.length; q++)
{
poly = coefs[q] + (xval * poly);
}
return poly;
}
}

Tested Output:

Test1:
Enter 1 if you would like to compute a polynomial function,
2 if you would like to compute a rational function,
Or -1 if you would like to exit
1
What is the degree of the polynomial?(No more than 10
2
What is the next coefficient in ascending order?(enter 0 for non present ones)
3
What is the next coefficient in ascending order?(enter 0 for non present ones)
5
What is the next coefficient in ascending order?(enter 0 for non present ones)
-2
What is the first number in the domain?
0
What is the last number in the domain?
10
At how many points should the domain be calculated?
10
X | Y
0.0 | -2.0
1.0 | 1.0
2.0 | 0.0
3.0 | -5.0
4.0 | -14.0
5.0 | -27.0
6.0 | -44.0
7.0 | -65.0
8.0 | -90.0
9.0 | -119.0

Test2:
Enter 1 if you would like to compute a polynomial function,
2 if you would like to compute a rational function,
Or -1 if you would like to exit
1
What is the degree of the polynomial?(No more than 10
4
What is the next coefficient in ascending order?(enter 0 for non present
2.76
What is the next coefficient in ascending order?(enter 0 for non present
-3.5
What is the next coefficient in ascending order?(enter 0 for non present
4.5
What is the next coefficient in ascending order?(enter 0 for non present
6.9
What is the next coefficient in ascending order?(enter 0 for non present
-9.32
What is the first number in the domain?
5
What is the last number in the domain?
10
At how many points should the domain be calculated?
10
X | Y
5.0 | -6124.82

ones)
ones)
ones)
ones)
ones)

5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5

|
|
|
|
|
|
|
|
|

-8945.939999999999
-12640.64
-17372.315
-23318.34
-30670.07
-39632.840000000004
-50425.965000000004
-63282.74
-78450.44000000002

Test 3:

Potrebbero piacerti anche