网站建设资讯

NEWS

网站建设资讯

分段插值函数的c语言程序 分段函数的编程c语言

分段线性插值问题

#includestdio.h

专注于为中小企业提供成都做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业泗县免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

#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)); //分段二次插值

}

怎么用c语言编程一个分段函数?

#include

int main()

{

int x,y;

scanf("%d",x);

if(0xx10) y=3*x+2;

else

{if(x=0) y=0;

else

{if (x0) y=x*x;

else printf("go die\n");

}

}

printf("%d",y);

return 0;

}该程序的分段函数如下:

f(x)=3x+2  (0x10)

f(x)=1         (x=0)

f(x) = x*x    (x0)

#include stdio.h

#include math.h

void main()

{

float x;

double y;

printf("Please input the value of x:");

scanf("%f",x);

if(x=-10x=4)

{

y=fabs(x-2);

printf("y=%.2f\n",y);

}

else if(x=5x=7)

{

y=x+10;

printf("y=%.2f\n",y);

}

else if(x=8x=12)

{

y=pow(x,4);

printf("y=%.2f\n",y);

}

else

printf("No answer\n");

}

C语言编写什么程序可以计算分段函数?

1、编写如下:

//100分制

#include stdio.h

void main()

{

int score,t;

printf("输入成绩:");

scanf("%d",score);

t=score/10;//t的取值0,1,2,3,4,5,6,7,8,9,10

switch(t)

{

case 0:

case 1:

case 2:

case 3:

case 4:

case 5:printf("不及格\n");break;

case 6:printf("及格\n");break;

case 7:

case 8:printf("良好\n");break;

case 9:

case 10:printf("优秀\n");break;

}

}

2、C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点。它由美国贝尔研究所的D.M.Ritchie于1972年推出,1978年后,C语言已先后被移植到大、中、小及微型机上,它可以作为工作系统设计语言,编写系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。

3、它的应用范围广泛,具备很强的数据处理能力,不仅仅是在软件开发上,而且各类科研都需要用到C语言,适于编写系统软件,三维,二维图形和动画,具体应用比如单片机以及嵌入式系统开发。

4、在开发中,他们还考虑把UNIX移植到其他类型的计算机上使用。C语言强大的移植性(Portability)在此显现。机器语言和汇编语言都不具有移植性,为x86开发的程序,不可能在Alpha,SPARC和ARM等机器上运行。而C语言程序则可以使用在任意架构的处理器上,只要那种架构的处理器具有对应的C语言编译器和库,然后将C源代码编译、连接成目标二进制文件之后即可运行。

C 语言编程 分段抛物线插值

我这里有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度的溶解度不太正常


网站名称:分段插值函数的c语言程序 分段函数的编程c语言
链接URL:http://cdweb.net/article/hgsjhs.html