10个重要的算法C语言实现源代码(其2--牛顿插值)
2008-05-25 21:51
861 查看
2.牛顿插值多项式,用于离散数据的拟合
#include<stdio.h> #include<conio.h>
#include<alloc.h>
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;i<k;i++)
f[i+1]=(f[i]-y[i])/(x[k]-x[i]);
y[k]=f[k];
}
return;
}
main()
{int i,n;
float x[20],y[20],xx,yy;
printf("Input n:");
scanf("%d",&n);
if(n>=20) {printf("Error! The value of n must in (0,20)."); getch();return 1;}
if(n<=0) {printf("Error! The value of n must in (0,20).");getch();return 1;}
for(i=0;i<=n-1;i++)
{ printf("x[%d]:",i);
scanf("%f",&x[i]);
}
printf("/n");
for(i=0;i<=n-1;i++)
{ printf("y[%d]:",i);scanf("%f",&y[i]);}
printf("/n");
difference(x,(float *)y,n);
printf("Input xx:");
scanf("%f",&xx);
yy=y[20];
for(i=n-1;i>=0;i--) yy=yy*(xx-x[i])+y[i];
printf("NewtonInter(%f)=%f",xx,yy);
getch();
}
#include<stdio.h> #include<conio.h>
#include<alloc.h>
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;i<k;i++)
f[i+1]=(f[i]-y[i])/(x[k]-x[i]);
y[k]=f[k];
}
return;
}
main()
{int i,n;
float x[20],y[20],xx,yy;
printf("Input n:");
scanf("%d",&n);
if(n>=20) {printf("Error! The value of n must in (0,20)."); getch();return 1;}
if(n<=0) {printf("Error! The value of n must in (0,20).");getch();return 1;}
for(i=0;i<=n-1;i++)
{ printf("x[%d]:",i);
scanf("%f",&x[i]);
}
printf("/n");
for(i=0;i<=n-1;i++)
{ printf("y[%d]:",i);scanf("%f",&y[i]);}
printf("/n");
difference(x,(float *)y,n);
printf("Input xx:");
scanf("%f",&xx);
yy=y[20];
for(i=n-1;i>=0;i--) yy=yy*(xx-x[i])+y[i];
printf("NewtonInter(%f)=%f",xx,yy);
getch();
}
相关文章推荐
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔 (转帖)
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯等等
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码(其4和5---龙贝格和牛顿迭代)
- 10个重要的算法C语言实现源代码(其3--高斯列主元消去法)
- 10个重要的算法C语言实现源代码
- 10个重要的算法C语言实现源代码