Question
please solve in java Implement Horners method for calculating a polynomial of degree n given a value of x and all of the coefficients (the
- please solve in java
- Implement Horners method for calculating a polynomial of degree n given a value of x and all of the coefficients (the ai terms) using an array of type double:
anxn + an-1xn-1 + an-2xn-2 + + a2x2 + a1x + a0
where n is a positive int and an 0.
Prompt the user: 1) for the degree n (a positive int), 2) for the n+1 coefficients (of type double, beginning with an and ending with a0), and 3) for the value of x (of type double). The algorithm is as follows (the final value of the polynomial evaluated at x is b0):
bn = an;
bn-1 = an-1 + bnx;
bn-2 = an-2 + bn-1x;
b1 = a1 + b2x;
b0 = a0 + b1x;
You do not have to defensively code for user errors. It is recommended that you use 2 arrays: one for the ais and one for the bis. You may use a recursive algorithm or a standard loop. Display the result to the user.
Example: f(x) = y = 2x2 3x + 1 is a second-degree polynomial (n = 2), where a1 = 2, a2 = -3, and a3 = 1. Evaluate the polynomial at x = -1 using Horners method; note that f(-1) =
2 *( -1)2 +( -3) *( -1) + 1 = 2 + 3 + 1 = 6. The array of ais is:
The a array | 1 | -3 | 2 | 0 |
Index | 0 | 1 | 2 | . . . |
Using Horners algorithm, we get:
b2 = a2 = 2
The b array | 0 | 0 | 2 | 0 |
Index | 0 | 1 | 2 | . . . |
b1 = a1 + b2 * x = -3 + 2 * -1 = -3 + -2 = -5
b array | 0 | -5 | 2 | 0 |
Index | 0 | 1 | 2 | . . . |
b0 = a0 + b1 * x = 1 + -5 * -1 = 1 + 5 = 6, and 6 is the final answer.
b array | 6 | -5 | 2 | 0 |
Index | 0 | 1 | 2 | . . . |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started