十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
#includestdio.h
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟空间、营销软件、网站建设、鹤峰网站维护、网站推广。
#includestring.h
#define N 100
typedef struct tag{
double x;
double y;
}POINT;
void main()
{
int i,j,n;
double x,temp,Ln=0;
POINT pt[N];
printf("请输入你要输入点的个数,,1=n=%d:\n",N);
printf("n=");
scanf("%d",n);
printf("\n");
printf("\n请输入对应的点数\n");
for(i=0;in;i++)
scanf("%lf,%lf",pt[i].x,pt[i].y);
printf("\n");
printf("输入插值点x的值:\n");
scanf("%lf",x);
printf("\n");
for(i=0;in;i++)
{
for(j=0,temp=1;jn;j++)
{
if(j!=i)
temp=temp*(x-pt[j].x)/(pt[i].x-pt[j].x);
}
Ln=Ln+temp*pt[i].y;
}
printf("输出:\nLn(%lf)=%lf\n",x,Ln);
}
///summary
///分段线性插值,将一组数插值为所需点数
////summary
///param name="dataIn"待插值的数据数组/param
///param name="n"插值点数/param
///returns插值后的数据数组/returns
public static double[] Interpolation(double[] dataIn,int n)
{
double[] dataOut = new double[n];
int lenIn = dataIn.Length;
double[] a = new double[lenIn];
double[] divIn = new double[lenIn];
double[] divOut = new double[n];
divIn[0] = 0;
for (int i = 1; i lenIn; i++)
{
divIn[i] = divIn[i - 1] + 1;
}
divOut[0] = 0;
for (int i = 1; i n; i++)
{
divOut[i] = divOut[i - 1] + lenIn / Convert.ToDouble(n);
}
int k = 0;
for (int i = k; i n; i++)
{
for (int j = 0; j lenIn - 1; j++)
{
if (divOut[i] = divIn[j] divOut[i] divIn[j + 1])
{
dataOut[i] = (dataIn[j + 1] - dataIn[j]) * (divOut[i] - divIn[j]) / (divIn[j + 1] - divIn[j]) + dataIn[j];
k = i;
}
}
}
return dataOut;
}
#includestdio.h
#includemath.h
double Lagrange1(double *x, double *y, double xx) //拉格郎日插值
{
int i,j;
double *a,yy=0.000;
a=new double[6];
for(i=0;i 6;i++)
{
a[i]=y[i];
for(j=0;j 6;j++)
if(j!=i)
a[i]*=(xx-x[j])/(x[i]-x[j]);
yy+=a[i];
}
delete a;
return yy;
}
double Lagrange2(double *x, double *y, double input) //分段线性插值
{
double output;
int i;
for (i=0;i5;i++)
{
if (x[i] = input x[i+1] = input)
{
output=y[i] +(y[i+1]-y[i])*(input-x[i])/(x[i+1]-x[i]);
break;
}
}
return output;
}
double Lagrange3(double *x,double *y,double u) //分段二次插值
{
int i,k=0;
double v;
for(i=0;i6;i++)
{
if(ux[1])
{
k=0;
v=y[k]*(u-x[k+1])*(u-x[k+2])/((x[k]-x[k+1])*(x[k]-x[k+2]))+y[k+1]*(u-x[k])*(u-x[k+2])/((x[k+1]-x[k])*(x[k+1]-x[k+2]))+y[k+2]*(u-x[k])*(u-x[k+1])/((x[k+2]-x[k])*(x[k+2]-x[k+1]));
}
if((x[i]uu=x[i+1])(fabs(u-x[i])=fabs(u-x[i+1])))
{
k=i-1;
v=y[k]*(u-x[k+1])*(u-x[k+2])/((x[k]-x[k+1])*(x[k]-x[k+2]))+y[k+1]*(u-x[k])*(u-x[k+2])/((x[k+1]-x[k])*(x[k+1]-x[k+2]))+y[k+2]*(u-x[k])*(u-x[k+1])/((x[k+2]-x[k])*(x[k+2]-x[k+1]));
}
if ((x[i]uu=x[i+1])fabs(u-x[i])fabs(u-x[i+1]))
{
k=i;
v=y[k]*(u-x[k+1])*(u-x[k+2])/((x[k]-x[k+1])*(x[k]-x[k+2]))+y[k+1]*(u-x[k])*(u-x[k+2])/((x[k+1]-x[k])*(x[k+1]-x[k+2]))+y[k+2]*(u-x[k])*(u-x[k+1])/((x[k+2]-x[k])*(x[k+2]-x[k+1]));
}
if(ux[4])
{
k=3;
v=y[k]*(u-x[k+1])*(u-x[k+2])/((x[k]-x[k+1])*(x[k]-x[k+2]))+y[k+1]*(u-x[k])*(u-x[k+2])/((x[k+1]-x[k])*(x[k+1]-x[k+2]))+y[k+2]*(u-x[k])*(u-x[k+1])/((x[k+2]-x[k])*(x[k+2]-x[k+1]));
}
}
return v;
}
void main()
{
double x[6] = {0.0, 0.1, 0.195, 0.3, 0.401, 0.5},y[6] = {0.39894,0.39695,0.39142,0.38138,0.36812,0.35206};
double u;
scanf("%lf",u);
printf("%f\n",Lagrange1(x,y,u)); //拉格郎日插值
printf("%f\n",Lagrange2(x,y,u)); //分段线性插值
printf("%f\n",Lagrange3(x,y,u)); //分段二次插值
}
我这里有2个程序,第一个用了2个函数
第二个用了1个函数,感觉误差小些
说下用法吧
先输入数据 两个两个的输,中间用空格隔开,比如
please input data1: 11 11.08然后回车
依次输入完五组数据
完了你想查某个温度的溶解度,他要求你输入温度你输入11.5
它就输出对应的溶解度,然后他提示你是否继续查溶解度,是就输入y,想结束程序
就输入n.
#includestdio.h
#includestdlib.h
#define NUMBER 5
typedef struct
{
double x;
double y;
}Point;
double * parabola(Point*, Point*, Point*);
double calculate(double* , double );
int main()
{
double x = 0, y = 0;
double* f = NULL;
int i = 0, n = 0;//n为要插的中间那个点的位置
char c = 'y';
Point p[NUMBER];
for(i = 0; i NUMBER; i++)
{
printf("Please input the point%d:", i+1);
scanf("%lf%lf", p[i].x, p[i].y);
while(10 != getchar())
{
continue;
}
}
while('n' != c)
{
printf("Please input the temperature:");
scanf("%lf", x);
while(10 != getchar())
{
continue;
}
//计算插值点的位子
if(2 * x = (p[1].x + p[2].x)) n = 1;
else if(2 * x = (p[NUMBER-2].x + p[NUMBER-1].x)) n = NUMBER - 2;
else n = (int)NUMBER / 2;
printf("%d\n", n);
f = parabola(p[n-1],p[n],p[n+1]);//计算抛物线方程
y = calculate(f,x);//计算溶解度
printf("The solubility at this temperature is %lf\n", y);
printf("Continue?(y/n) ");
scanf("%c", c);
while(10 != getchar())
{
continue;
}
//释放内存
free(f);
}
return 0;
}
//下面为计算抛物线方程的函数
//设抛物线函数为y = a * x^2 + b * x + c
//以下f[0]为a,f[1]为b,f[2]为c
double * parabola(Point* p1, Point* p2, Point* p3)
{
double temp1 = 0, temp2 = 0;
double * f = NULL;
f = (double*)calloc(3,sizeof(double));
if(NULL == f)
{
printf("Calloc failed!\n");
return NULL;
}
temp1 = (p2-y - p1-y)/(p2-x - p1-x);
temp2 = (p3-y - p2-y)/(p3-x - p2-x);
f[0] = (temp2 - temp1)/(p3-x - p1-x);
f[1] = temp1 - f[0] * (p1-x + p2-x);
f[2] = p1-y - p1-x * (p1-x * f[0] + f[1]);
return f;
}
//根据温度计算溶解度
double calculate(double* f, double x)
{
double y;
y = x * (f[0] * x + f[1]) + f[2];
return y;
}
//这里是第二个程序
#includestdio.h
#includestdlib.h
#define NUMBER 5
typedef struct
{
double x;
double y;
}Point;
double parabola(Point*, Point*, Point*, double);
int main()
{
double x = 0, y = 0;
int i = 0, n = 0;//n为要插的中间那个点的位置
char c = 'y';
Point p[NUMBER];
for(i = 0; i NUMBER; i++)
{
printf("Please input the data%d:", i+1);
scanf("%lf%lf", p[i].x, p[i].y);
while(10 != getchar())
{
continue;
}
}
while('n' != c)
{
printf("Please input the temperature:");
scanf("%lf", x);
while(10 != getchar())
{
continue;
}
//计算插值点的位子
if(2 * x (p[1].x + p[2].x)) n = 1;
else if(2 * x (p[NUMBER-2].x + p[NUMBER-1].x)) n = NUMBER - 2;
else n = (int)NUMBER / 2;
//printf("%d\n", n);
y = parabola(p[n-1],p[n],p[n+1],x);
printf("The solubility at this temperature is %lf\n", y);
printf("Continue?(y/n) ");
scanf("%c", c);
while(10 != getchar())
{
continue;
}
}
return 0;
}
//直接套用公式计算溶解度
double parabola(Point* p1, Point* p2, Point* p3, double x)
{
double temp1 = 0, temp2 = 0, temp3 = 0, y = 0;
double a = 0, b = 0, c = 0;
temp1 = (p2-y - p1-y) * (x - p1-x);
temp2 = (p3-y - p1-y) * (p2-x - p1-x) - (p2-y - p1-y) * (p3-x - p1-x);
temp3 = (p3-x - p1-x) * (p3-x - p1-x) * (p2-x - p1-x);
y = p1-y + temp1 / (p2-x - p1-x) + temp2 * (x - p1-x) * (x - p2-x) / temp3;
return y;
}
该程序误差绝对小 但结果和你不是完全符合
你的数据好象有点问题 14度的溶解度不太正常