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

求双线性插值法的C语言程序!帮帮忙!拜托各位了!ab
t
cd
就是两次线性插值,先在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语言编写牛顿插值法牛顿插值法:
#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语言编写一个线性插值程序#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++
#include iostream
#include iomanip
#include stdlib.h
using namespace std;
#define N 100
void lagrange()
{
int n,k,m,q=1;
float x[N],y[N],xx,yyy1,yyy2,yy1,yy2,yy3;
cout"请输入X的个数:";
cinn;
for(k=0;k=n-1;k++)
{
cout"请输入X"k"的值:";
cinx[k];
cout"请输入Y"k"的值:";
ciny[k];
}
system("cls");
cout"则Xi与Yi表格如下:"endl;
cout"Xi""";for(k=0;k=n-1;k++)coutsetiosflags(ios::left)setw(10)x[k];
coutendl;
cout"Yi""";for(k=0;k=n-1;k++)coutsetiosflags(ios::left)setw(10)y[k];
coutendl;
while(q)
{
cout"请输入所求x的值:";
cinxx;
while(xxx[k-1]||xxx[0])
{
cout"输入错误,请重新输入:";
cinxx;
}
for(k=0;k=n-1;k++)
{
if(xxx[k])
{
m=k-1;
k=n-1;
}
}
yyy1=y[m]*((xx-x[m+1])/(x[m]-x[m+1]))+y[m+1]*((xx-x[m])/(x[m+1]-x[m]));
cout"则拉格朗日分段线性插值为:"yyy1endl;
for(k=0;k=n-1;k++)
{
if(xxx[k])
{
m=k-1;
k=n-1;
}
}
if((xx-x[m])(x[m+1]-xx))m=m+1;
else m=m;
yy1=y[m-1]*((xx-x[m])*(xx-x[m+1]))/((x[m-1]-x[m])*(x[m-1]-x[m+1]));
yy2=y[m]*((xx-x[m-1])*(xx-x[m+1]))/((x[m]-x[m-1])*(x[m]-x[m+1]));
yy3=y[m+1]*((xx-x[m-1])*(xx-x[m]))/((x[m+1]-x[m-1])*(x[m+1]-x[m]));

推荐阅读