Horner's Method
There are two
common methods of expressing polynomial functions, power basis and Bézier.
Although mathematically equivalent, we will see that the Bézier method is far
better suited to representing and manipulating shape in a computer.
Horner’s method is
most efficiently computed by giving
and
the point
on
a power basis curve.
include <iostream>
using namespace std;
int main(){
//
..........................................................................//
//sum: the sum of the equation if we give the
value of the u.
//a: coefficient of power basis representation.
//u: variable
//degree: the formula will change based on the
degree of function
//function is f(u) = ao + a1(u) + a2(u^2)+ ... +
an(u^n) for degree n
//..........................................................................//
double sum[50]; //we want to store the degree only max=50
double u, a[50];
int degree;
cout << "Degree =
";
cin >> degree;
if ( degree <= 0) {
cin >> degree;
}
else {
cout << "Please input
the u value -> u =";
cin >> u;
cout << "Please input
coefficients (a)" << degree << "= ";
cin >> a[degree];
cout << "Please input
coefficients (a)" << degree – 1 << "= ";
cin >> a[degree - 1];
}
sum[degree] = a[degree] * u + a[degree -
1];
for (int i = degree; i > 1; i--){
cout << "Please input
coefficients (a)" << i - 2 << "= ";
cin >> a[i - 2];
sum[i - 1] = (sum[i] * u + a[i -
2]);
}
cout << "Function is:
f(u) = ";
for (int i = degree; i > 0; i--){
cout << a[i] << "*u^" << i
<< "+";
}
cout << a[0] << endl;
cout << "Horner result
is: f(" << u << ")=" << sum[1] << endl;
system("pause");
return 0;
}
No comments:
Post a Comment