#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
float lagrange( float x);
long double cc[25][2]={\
2479.8 , 200,
2538.6 , 190,
2598.8 , 180,
2663.3 , 170,
2726.2 , 160,
2790.8 , 150,
2857.3 , 140,
2928.2 , 130,
2998.7 , 120,
3071.8 , 110,
3145.4 , 100,
3221.3 , 90,
3301.5 , 80,
3381.5 , 70,
3463 , 60,
3549.6 , 50,
3632.6 , 40,
3722.7 , 30,
3815.5 , 20,
3903 , 10,
3996.9 , 0,
4091.1 , -10,
4190.8 , -20,
4287.6 , -30,
4383.4 , -40,
};
int main()
{
printf("y= %f \n",lagrange(4383.50));
printf("y= %f \n",lagrange(5001.23));
printf("y= %f \n",lagrange(1234.56 ));
printf("y= %f \n",lagrange(3904 ));
return 0;
};
float lagrange( float x)
{
int i,j;
long double result = 0.0,k=1.0;
int m,n;
if (x>= cc[22][0])
{
m = 25;
n= 22;
}
else if (x<=cc[2][0])
{
m = 3;
n = 0;
}
else
{
n=2;
while (x>cc[n][0]){
n++;
}
n= n-1;
m= n+3;
}
for(i=n;i<m;i++)
{
k=1.0;
for(j=n;j<m;j++)
{
if(j!=i)
{
k=k*(x-cc[j][0])/(cc[i][0]-cc[j][0]);
}
}
result =result+cc[i][1]*k;
}
return result;
}
;; |