测试代码如下:- #include <iostream>
- using namespace std;
- float LagrangeInterpolationPolynomia(float x,int n,float a[],float b[])
- {
- int k;
- float t,y=0;
- int j;
- for (k = 0;k < n;k++)
- {
- t = 1;
- for (j = 0;j < n;j++)
- {
- if (j != k)
- t = ((x - a[j])/(a[k]-a[j]))*t;
- }
- y = t * b[k]+y;
- //cout << y << endl;
- }
- return y;
- }
- int main ()
- {
- float t ,z;
- float x[5] = {1.000, 2.000, 3.000, 4.000, 5.000};
- float y[5] = {1277.0, 1208.0, 1016.0, 854.0, 515.0};
- t =1000;
- for(t;t>510;t--)
- {
- cout << "y="<< LagrangeInterpolationPolynomia(t,5,y,x)<<endl;
- }
-
-
-
-
-
-
- while(1);
- }
-
|