Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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;
if(in.hasNextInt() )
choice = in.nextInt();
switch(choice)
{
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
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
break;
}
calculated?");
coefs));
domain");
}
}
else
{
System.out.println("Please only enter numbers for the
eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too
break;
}
}
}
}
}
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
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
break;
}
interval)
coefficients");
eCount++;
if(eCount == 3)
{
System.out.println("This program has encountered too
}
}
}
else
{
break;
break;
}
}
else
{
System.out.println("Please only enter integers for the degree");
eCount++;
if(eCount == 3)
now exit");
}
break;
}
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: