
I am very happy to say that the article on the evaluation of polynomials generated a flood of responses from our readers and it is good to see that we aren’t the only engineers that enjoy a little fun with math. In this month's ask Larry I want to revisit the topic polynomials.
Most of the responses made a minor change to our polynomial which is a more mathematically rigorous definition and I will include it here:
solution = a*x^4 + b*x^3 + c*x^2 + d*x + e
We also got plenty of tips on faster methods to evaluate the polynomial like the one shown here:
solution = ((((a*x+b)*x+c)*x+d)*x+e);
Another method of performing the same calculation would be:
f = a*(x*x*x*x);
f += b*(x*x*x);
f += c*(x*x);
f += d*x;
solution = f + e;
Here is a short program that compares three different methods of calculating a polynomial.
main()
{
//Coefficients and solution
float a,b,c,d,e,f,x, solution, i;
//Used for timing the calculation
unsigned long time_0;
float etime0, etime1;
a = 3.14; //Pi
b = -3.14; //negative Pi
c = 2.718; //e
d = -2.718; //negative e
e = 6.022; //Avagadoro's number x 10^23
x = -6.022; //negative Avagadoro's number x 10^23
//First we time a for loop without a calculation to get a baseline
time_0 = MS_TIMER; //Set start time
for (i=0; i<1000; i++ );
etime1 = (float)(MS_TIMER-time_0); // elapsed time w/o calculation
printf("METHOD ONE\n");
time_0 = MS_TIMER; //Set start time
for (i=0; i<1000; i++ )
{
solution = (a * pow(x,4)) + (b * pow(x,3)) + (c * pow(x,2)) + (d * x) + e;
}
etime0 = (float)(MS_TIMER-time_0); // elapsed time with calculation
etime0 = (etime0-etime1)/1000.0; // calculate ms/iteration
printf("solution = %f\n",solution);
printf("Total iteration time = %f milliseconds\n", etime0);
printf("\n\nMETHOD TWO\n");
time_0 = MS_TIMER; //Set start time
for (i=0; i<1000; i++ )
{
f = a*(x*x*x*x);
f += b*(x*x*x);
f += c*(x*x);
f += d*x;
solution = f + e;
}
etime0 = (float)(MS_TIMER-time_0); // elapsed time with calculation
etime0 = (etime0-etime1)/1000.0; // calculate ms/iteration
printf("solution = %f\n",solution);
printf("Total iteration time = %f milliseconds\n", etime0);
printf("\n\nMETHOD THREE\n");
time_0 = MS_TIMER; //Set start time
for (i=0; i<1000; i++ )
{
solution = ((((a*x+b)*x+c)*x+d)*x+e);
}
etime0 = (float)(MS_TIMER-time_0); // elapsed time with calculation
etime0 = (etime0-etime1)/1000.0; // calculate ms/iteration
printf("solution = %f\n",solution);
printf("Total iteration time = %f milliseconds\n", etime0);
}
I think you will find that while each answer produces the same result, but they execute at very different speeds.
Running the program shown here with an RCM 4300 the output produced is:
METHOD ONE
solution = 4936.138100
Total iteration time = 0.510000 milliseconds
METHOD TWO
solution = 4936.138100
Total iteration time = 0.097000 milliseconds
METHOD THREE
solution = 4936.137600
Total iteration time = 0.053000 milliseconds
Thanks to everyone who contributed feedback to the article!
- Larry C.
Larry Cicchinelli is Rabbit’s Technical Support Manager. He has 30 years of embedded experience, and is considered one of the foremost authorities on Rabbit products. Larry and his staff offer comprehensive technical support to Rabbit customers.
Submit your questions for Larry via email at AskLarry@rabbit.com
Read more Ask Larry Answers
