I am writing a Polynomial Line of Best Fit solver as a term break project for fun.
Given the input: a degree of polynomial (i.e. 2) and a set of points they want to use
167, 563
264, 429
410, 562
Using Least Squares Approximation Method, when solving the matrix, you get the following Coefs for the system:
x^0 = 1270.1336927645
x^1 = -5.9573736115
x^2 = 0.0103176055
With the equation being
y= (0.01031760552017095)x^2 + (-5.95737361147607913)x^1 + (1270.13369276445428113)x^0
Now, I would like to use that equation later on in the future.
However, can I write these equations out with an n number degree.
Currently the coefs are store in an
double A[];
So if I have a polynomial of 4 Degrees, I know the equation is going to be
y = A[4]x^4 + A[3]x^3 + A[2]x^2 + A[1]x^1 + A[0]x^0
** or **
y = A[4]*x*x*x*x + A[3]*x*x*x + A[2]*x*x + A[1]*x + A[0]
If I have a polynomial of 5 degrees, my equation will be
y = A[5]x^5 + A[4]x^4 + A[3]x^3 + A[2]x^2 + A[1]x^1 + A[0]x^0
or
y = A[5]*x*x*x*x*x + A[4]*x*x*x*x + A[3]*x*x*x + A[2]*x*x + A[1]*x + A[0]
Is there any way that I can formulate the equation based on the degree of the polynomial. I do not want to hardcode the degree for each coef. based on the inout given.
Thank you