线性插值函数c语言 线性插值法c语言程序

用C语言编写一个线性插值程序

#include stdio.h

创新互联致力于互联网网站建设与网站营销,提供成都做网站、成都网站设计、网站开发、seo优化、网站排名、互联网营销、重庆小程序开发、公众号商城、等建站开发,创新互联网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

double Lerp(double x0,double y0,double x1,double y1,double x)

{

double dy = y1 - y0;

if(dy == 0){

printf("除0错误!\n");

return 0;

}

return x * (x1 - x0) / dy;

}

int main()

{

double x0,x1,y1,y0,x,y;

printf("Inptu x0 y0 x1 y1 x:");

scanf("%lf %lf %lf %lf %lf",x0,y0,x1,y1,x);

y = Lerp(x0,y0,x1,y1,x);

printf("y = %lf\n",y);

return 0;

}

用C语言编一个线性插值的小程序,很着急

#includestdio.h

#includestdlib.h

#includeiostream.h

typedef struct data

{

float x;

float y;

}Data;//变量x和函数值y的结构

Data d[20];//最多二十组数据

float f(int s,int t)//牛顿插值法,用以返回插商

{

if(t==s+1)

return (d[t].y-d[s].y)/(d[t].x-d[s].x);

else

return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x);

}

float Newton(float x,int count)

{

int n;

while(1)

{

cout"请输入n值(即n次插值):";//获得插值次数

cinn;

if(n=count-1)// 插值次数不得大于count-1次

break;

else

system("cls");

}

//初始化t,y,yt。

float t=1.0;

float y=d[0].y;

float yt=0.0;

//计算y值

for(int j=1;j=n;j++)

{

t=(x-d[j-1].x)*t;

yt=f(0,j)*t;

//coutf(0,j)endl;

y=y+yt;

}

return y;

}

float lagrange(float x,int count)

{

float y=0.0;

for(int k=0;kcount;k++)//这儿默认为count-1次插值

{

float p=1.0;//初始化p

for(int j=0;jcount;j++)

{//计算p的值

if(k==j)continue;//判断是否为同一个数

p=p*(x-d[j].x)/(d[k].x-d[j].x);

}

y=y+p*d[k].y;//求和

}

return y;//返回y的值

}

void main()

{

float x,y;

int count;

while(1)

{

cout"请输入x[i],y[i]的组数,不得超过20组:";//要求用户输入数据组数

cincount;

if(count=20)

break;//检查输入的是否合法

system("cls");

}

//获得各组数据

for(int i=0;icount;i++)

{

cout"请输入第"i+1"组x的值:";

cind[i].x;

cout"请输入第"i+1"组y的值:";

cind[i].y;

system("cls");

}

cout"请输入x的值:";//获得变量x的值

cinx;

while(1)

{

int choice=3;

cout"请您选择使用哪种插值法计算:"endl;

cout" (0):退出"endl;

cout" (1):Lagrange"endl;

cout" (2):Newton"endl;

cout"输入你的选择:";

cinchoice;//取得用户的选择项

if(choice==2)

{

cout"你选择了牛顿插值计算方法,其结果为:";

y=Newton(x,count);break;//调用相应的处理函数

}

if(choice==1)

{

cout"你选择了拉格朗日插值计算方法,其结果为:";

y=lagrange(x,count);break;//调用相应的处理函数

}

if(choice==0)

break;

system("cls");

cout"输入错误!!!!"endl;

}

coutx" , "yendl;//输出最终结果

}

求用c语言编写牛顿插值法

牛顿插值法:

#includestdio.h

#includealloc.h

float Language(float *x,float *y,float xx,int n)

{

int i,j;

float *a,yy=0.0;

a=(float *)malloc(n*sizeof(float));

for(i=0;i=n-1;i++)

{

a[i]=y[i];

for(j=0;j=n-1;j++)

if(j!=i)a[i]*=(xx-x[j])/(x[i]-x[j]);

yy+=a[i];

}

free(a);

return yy;

}

void main()

{

float x[4]={0.56160,0.5628,0.56401,0.56521};

float y[4]={0.82741,0.82659,0.82577,0.82495};

float xx=0.5635,yy;

float Language(float *,float *,float,int);

yy=Language(x,y,xx,4);

printf("x=%f,y=%f\n",xx,yy);

getchar();

}

2.牛顿插值法#includestdio.h

#includemath.h

#define N 4

void Difference(float *x,float *y,int n)

{

float *f;

int k,i;

f=(float *)malloc(n*sizeof(float));

for(k=1;k=n;k++)

{

f[0]=y[k];

for(i=0;ik;i++)

f[i+1]=(f[i]-y[i])/(x[k]-x[i]);

y[k]=f[k];

}

return;

}

main()

{

int i;

float varx=0.895,b;

float x[N+1]={0.4,0.55,0.65,0.8,0.9};

float y[N+1]={0.41075,0.57815,0.69675,0.88811,1.02652};

Difference(x,(float *)y,N);

b=y[N];

for(i=N-1;i=0;i--)b=b*(varx-x[i])+y[i];

printf("Nn(%f)=%f",varx,b);

getchar();

}

留下个邮箱,我发给你:牛顿插值法的程序设计与应用

求双线性插值法的C语言程序!帮帮忙!拜托各位了!

a   b

t

c   d

就是两次线性插值,先在x方向插出t上下方的_t1、_t2,然后再用它们插出t来

float test(float x,float y)

{

float _t1,_t2,t;

_t1 = a+(b-a)*(x-ax)/(bx-ax);

_t2 = c+(d-c)*(x-cx)/(dx-cx);

t = _t1 +(_t2-_t1)*(y - ay);

return t;

}


新闻标题:线性插值函数c语言 线性插值法c语言程序
分享网址:http://www.kswcd.cn/article/hjcjoh.html

其他资讯

Copyright © 2007-2020 成都快上网科技有限公司 All Rights Reserved 蜀ICP备19037934号
友情链接: 成都网站制作 成都网站设计公司 古蔺网站建设 品牌网站建设 成都网站建设 成都网站建设推广 盐亭网站设计 定制网站建设 网站建设公司 成都网站建设公司 成都网站建设 攀枝花网站设计 网站制作公司 手机网站制作 成都网站建设 成都h5网站建设 成都网站建设公司 重庆电商网站建设 成都网站建设公司 成都企业网站设计 成都网站设计 外贸营销网站建设