线性插值函数C语言编程 线性插值函数c语言编程代码

用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语言程序!帮帮忙!拜托各位了!

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语言编写一个线性插值程序

#include stdio.h

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语言编程 线性插值函数c语言编程代码
分享网址:http://ybzwz.com/article/ddephis.html