十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
比如有10个数据点,要求用抛物线插值法插值,做法是这样的:
横山网站建设公司创新互联建站,横山网站设计制作,有大型网站制作公司丰富经验。已为横山超过千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的横山做网站的公司定做!
点1、2、3确定一条抛物线,用这条抛物线连接1、2;
点2、3、4确定一条抛丛渗毕物线,用这条抛物线连接2、3;
点3、4、5确定一条抛物线,用这条渗芹抛物线连接3、4;
....
点8、9、10确定一条抛物线,用这条抛物线连接喊宽8、9;
9、10之间怎么办?空着,或者8、9的抛物线继续画,连接9、10;
void main()
{
double a,b,c;
double x1,y1,x2,x3,y2,y3;
double m;
printf("please input 3 points:\n");
printf("(x1,y1),(x2,y2),(x3,y3):\n");
scanf("%lf%lf%lf%lf%lf%lf",x1,y1,x2,y2,x3,y3);
m = x1*x1*x2+x2*x2*x3+x1*x3*x3-x3*x3*x2-x2*x2*x1-x1*x1*x3;
printf("////////////纤昌迹//迅春////////////////////////毁并//////////\n");
if((m+1)==1)
{
printf("No result with this 3 points!\n");
}
else
{
a=(y1*x2+y2*x3+y3*x1-y3*x2-y2*x1-y1*x3) /m;
b=(x1*x1*y2+x2*x2*y3+x3*x3*y1-x3*x3*y2-x2*x2*y1-x1*x1*y3)/m;
c=(x1*x1*x2*y3+x2*x2*x3*y1+x3*x3*x1*y2-x3*x3*x2*y1-x2*x2*x1*y3-x1*x1*x3*y2)/m;
printf("y = ax^2 + bx + c\n(a=%lf,b=%lf,c=%lf)\n",a,b,c);
}
printf("press any key to quit:\n");
getch();
}
我这里有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度的溶解度不太正常