0

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

  • "Is there any special check that I can do that would take an n number of degree?" I don't understand what you are asking. Do you mean giving an array of coefficients, can you determine the degree? That's pretty simple if you require all coefficients for every term in the polynomial. For `n` coefficients, the degree is `n-1`. – Code-Apprentice Nov 21 '19 at 23:38
  • 1
    like if the user puts in a **3** for the degree and the **coefs = 3 , 2 ,1** the equation should be `y = 3*x*x + 2*x + 1`. How can I formulate that equation without hardcoding each case. –  Nov 21 '19 at 23:44
  • 1
    That polynomial has degree 2, not degree 3. – Code-Apprentice Nov 21 '19 at 23:49
  • "How can I formulate that equation without hardcoding each case." What is each case? Can you define them by a simple rule? For example, are the coefficients always decreasing integers? – Code-Apprentice Nov 21 '19 at 23:50

1 Answers1

1

Im not sure if this is what you are asking but if you want to know the size of the array you can do something like this:

size_t n = sizeof(A)/sizeof(A[0]);//size of list divided by size of first element (should be 8 for double)
degree = (int)n - 1; //degree of the poly is one minus this

Not sure what you are meaning by "use that equation later on in the future" If you are just wanting to be able to evaluate the function I think that the most efficient way is to use horner's algorithm:

#include <stdio.h>

double horner(double *poly, double x,int degree) 
{ 
    double result = poly[0];  // Initialize result (poly[0] should be coeff of x^(degree))

    // Evaluate value of polynomial using Horner's method

    for (int i=1; i<=degree; i++){ 
        result = result*x + poly[i]; 
    }
    return result; 
} 

int main(void) {
  double poly[] = {1.0,2.0,-1.0,5.0};
  //x^3+2x^2-1x+5 @ 3.0 should get 47.0

  // use the n we found earlier
  int n = sizeof(poly)/sizeof(poly[0]) - 1;
  double x = 3.0;
  printf("%f",horner(poly, x,n));
  //should print 47
  return 0;
}

adapted from: https://www.geeksforgeeks.org/horners-method-polynomial-evaluation/

I put it in a repl as well if it helps: https://repl.it/repls/CumbersomeLeanEquipment

Sami Wood
  • 305
  • 3
  • 10