When calculating the value of a polynomial function fx anxn


When calculating the value of a polynomial function f(x) = anxn + an - ixn - i+ ...+ao in C + + it is unwieldy to repeatedly calculate x^A j;. Develop an alternative formulation for calculating the polynomial expression that avoids the need for repeated power calculation and reduces the risk of numerical instability.

Solution

P(x) = AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0

This can be re written as:-

P(x) = A0 + X(A1 + X(A2 + X(A3 + X(Q4 + X(...X(An-1 + XAn)))), and evaluation starting from the inner loop.

The c code is:-

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100

void main()
{
int arr[MAXSIZE],i, number, pow;
float x, psum;
printf(\"Enter order of the polynomial \ \");
scanf(\"%d\", &number);
printf(\"Enter the value of x \ \");
scanf(\"%f\", &x);
/* Reading the coefficients into an arr */
printf(\"Enter %d coefficients \ \", number + 1);
for (i = 0; i <= number; i++)
{
scanf(\"%d\", &arr[i]);
}
psum = arr[0];
for (i = 1; i <= number; i++)
{
psum = psum * x + arr[i];
}
pow = number;

printf(\"The polynomial is: \ \");
for (i = 0; i <= number; i++)
{
if (pow < 0)
{
break;
}

if (arr[i] > 0)
printf(\" + \");
else if (arr[i] < 0)
printf(\" - \");
else
printf(\" \");
printf(\"%dx^%d \", abs(arr[i]), pow--);
}
printf(\"\ Sum of the polynomial is = %6.2f \ \", psum);
}

 When calculating the value of a polynomial function f(x) = anxn + an - ixn - i+ ...+ao in C + + it is unwieldy to repeatedly calculate x^A j;. Develop an alter

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site