希望能帮助到你!
牛顿插值法
#include<stdio.h>
#include<math.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=1k<=nk
)
{
f[0]=y[k]
for(i=0i<ki
)
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
*
问题补充,因字数限制,挪到这1.拉格朗日插值简介:
对给定的n个插值节点x1,x2,…,xn,及其对应的函数值y1=f(x1), y2=f(x2),…, yn=f(xn)使用拉格朗日插值公式,计算在x点处的对应的函数值f(x)
2.一维拉格朗日插值c语言程序:
Int lagrange(x0, y0, n, x, y)
Float xo[], yo[], x
Int n
Float *y
{
Int i, j
Float p
*y=0
If (n>1)
{
For(i=0i<ni++)
{
P=1
For(j=1j<nj++)
{
If(i!=J)
P=p*(x-x0[j]/x0[i]-x0[j])
}
*y=*y+p*y0[i]
Return(0)
}
Else
Return(-1)
}
3.例题。已知函数如下表所示,求x=0.472处的函数值:
X 0.46 0.47 0.480.49
Y 0.484655 0.4903745 0.502750 0.511668
计算这个问题的c语言程序如下:
#minclude stdio
#includeM<nath.h>
Main()
{
Float x0[4]={ 0.46, 0.47,0.48,0.49}
Float y0[4]={ 0.484655 ,0.4903745 ,0.502750 ,0.511668}
Float x, y
Int n, rtn
N=4
X=0.472
Rth=lagrange(x0,y0,n,x,&y)
If(rtn=0)
{
Prinf(“Y(0.472)=:%f\n”,y)
}
Else
{
Prinf(“n must be larger than 1.\n”)
}
}
计算结果:Y(0.472)=:0.495553
4.问题补充
我的问题与上面的例子类似,计算三维空间一点(x,y,z)对应的函数值(Vx,Vy,Vz).不同的是自变量(point_coordinate.txt)为三维空间散乱点(不是正方体的顶点),因变量(point_data.txt)为矢量(向量 )。插值算法比较多,常数法,拉格朗日插值,埃特金插值,三阶样条插值等。最简单的就是常数法,查找离目标点(x,y,z)距离最近的已知自变量(Xi,Yi,Zi),把该点的函数值赋给目标点做函数值,求高手帮忙写写。
牛顿插值法:#include<stdio.h>
#include<alloc.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=0i<=n-1i++)
{
a[i]=y[i]
for(j=0j<=n-1j++)
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.牛顿插值法#include<stdio.h>
#include<math.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=1k<=nk++)
{
f[0]=y[k]
for(i=0i<ki++)
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-1i>=0i--)b=b*(varx-x[i])+y[i]
printf("Nn(%f)=%f",varx,b)
getchar()
}
留下个邮箱,我发给你:牛顿插值法的程序设计与应用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)