A. 已知函数值c语言中用三次样条插值求某个点值程序
void SPL(int n, double *x, double *y, int ni, double *xi, double *yi); 是你所要。
已知 n 个点 x,y; x 必须已按顺序排好。要插值 ni 点,横坐标 xi[], 输出 yi[]。
程序里用double 型,保证计算精度。
SPL调用现成的程序。
现成的程序很多。端点处理方法不同,结果会有不同。想同matlab比较,你需 尝试 调用 spline()函数 时,令 end1 为 1, 设 slope1 的值,令 end2 为 1 设 slope2 的值。
#include
#include
int spline (int n, int end1, int end2,
double slope1, double slope2,
double x[], double y[],
double b[], double c[], double d[],
int *iflag)
{
int nm1, ib, i, ascend;
double t;
nm1 = n - 1;
*iflag = 0;
if (n < 2)
{ /* no possible interpolation */
*iflag = 1;
goto LeaveSpline;
}
ascend = 1;
for (i = 1; i < n; ++i) if (x[i] <= x[i-1]) ascend = 0;
if (!ascend)
{
*iflag = 2;
goto LeaveSpline;
}
if (n >= 3)
{
d[0] = x[1] - x[0];
c[1] = (y[1] - y[0]) / d[0];
for (i = 1; i < nm1; ++i)
{
d[i] = x[i+1] - x[i];
b[i] = 2.0 * (d[i-1] + d[i]);
c[i+1] = (y[i+1] - y[i]) / d[i];
c[i] = c[i+1] - c[i];
}
/* ---- Default End conditions */
b[0] = -d[0];
b[nm1] = -d[n-2];
c[0] = 0.0;
c[nm1] = 0.0;
if (n != 3)
{
c[0] = c[2] / (x[3] - x[1]) - c[1] / (x[2] - x[0]);
c[nm1] = c[n-2] / (x[nm1] - x[n-3]) - c[n-3] / (x[n-2] - x[n-4]);
c[0] = c[0] * d[0] * d[0] / (x[3] - x[0]);
c[nm1] = -c[nm1] * d[n-2] * d[n-2] / (x[nm1] - x[n-4]);
}
/* Alternative end conditions -- known slopes */
if (end1 == 1)
{
b[0] = 2.0 * (x[1] - x[0]);
c[0] = (y[1] - y[0]) / (x[1] - x[0]) - slope1;
}
if (end2 == 1)
{
b[nm1] = 2.0 * (x[nm1] - x[n-2]);
c[nm1] = slope2 - (y[nm1] - y[n-2]) / (x[nm1] - x[n-2]);
}
/* Forward elimination */
for (i = 1; i < n; ++i)
{
t = d[i-1] / b[i-1];
b[i] = b[i] - t * d[i-1];
c[i] = c[i] - t * c[i-1];
}
/* Back substitution */
c[nm1] = c[nm1] / b[nm1];
for (ib = 0; ib < nm1; ++ib)
{
i = n - ib - 2;
c[i] = (c[i] - d[i] * c[i+1]) / b[i];
}
b[nm1] = (y[nm1] - y[n-2]) / d[n-2] + d[n-2] * (c[n-2] + 2.0 * c[nm1]);
for (i = 0; i < nm1; ++i)
{
b[i] = (y[i+1] - y[i]) / d[i] - d[i] * (c[i+1] + 2.0 * c[i]);
d[i] = (c[i+1] - c[i]) / d[i];
c[i] = 3.0 * c[i];
}
c[nm1] = 3.0 * c[nm1];
d[nm1] = d[n-2];
}
else
{
b[0] = (y[1] - y[0]) / (x[1] - x[0]);
c[0] = 0.0;
d[0] = 0.0;
b[1] = b[0];
c[1] = 0.0;
d[1] = 0.0;
}
LeaveSpline:
return 0;
}
double seval (int n, double u,
double x[], double y[],
double b[], double c[], double d[],
int *last)
{
int i, j, k;
double w;
i = *last;
if (i >= n-1) i = 0;
if (i < 0) i = 0;
if ((x[i] > u) || (x[i+1] < u))
{
i = 0;
j = n;
do
{
k = (i + j) / 2;
if (u < x[k]) j = k;
if (u >= x[k]) i = k;
}
while (j > i+1);
}
*last = i;
w = u - x[i];
w = y[i] + w * (b[i] + w * (c[i] + w * d[i]));
return (w);
}
void SPL(int n, double *x, double *y, int ni, double *xi, double *yi)
{
double *b, *c, *d;
int iflag,last,i;
b = (double *) malloc(sizeof(double) * n);
c = (double *)malloc(sizeof(double) * n);
d = (double *)malloc(sizeof(double) * n);
if (!d) { printf("no enough memory for b,c,d\n");}
else {
spline (n,0,0,0,0,x,y,b,c,d,&iflag);
if (iflag==0) printf("I got coef b,c,d now\n"); else printf("x not in order or other error\n");
for (i=0;i<ni;i++) yi[i] = seval(ni,xi[i],x,y,b,c,d,&last);
free(b);free(c);free(d);
};
}
main(){
double x[6]={0.,1.,2.,3.,4.,5};
double y[6]={0.,0.5,2.0,1.6,0.5,0.0};
double u[8]={0.5,1,1.5,2,2.5,3,3.5,4};
double s[8];
int i;
SPL(6, x,y, 8, u, s);
for (i=0;i<8;i++) printf("%lf %lf \n",u[i],s[i]);
return 0;
}
B. 三次样条插值用c语言具体怎么做
void SPL(int n, double *x, double *y, int ni, double *xi, double *yi); 是你所要。
已知 n 个点 x,y; x 必须已按顺序排好。要插值 ni 点,横坐标 xi[], 输出 yi[]。
程序里用double 型,保证计算精度。
SPL调用现成的程序。
现成的程序很多。端点处理方法不同,结果会有不同。想同matlab比较,你需 尝试 调用 spline()函数 时,令 end1 为 1, 设 slope1 的值,令 end2 为 1 设 slope2 的值。
C. c语言中插值排序法怎么打程序
(1)“冒泡法”
冒泡法大家都较熟悉。其原理为从a[0]开始,依次将其和后面的元素比较,若a[0]>a[i],则交换它们,一直比较到a[n]。同理对a[1],a[2],...a[n-1]处理,即完成排序。下面列出其代码:
void bubble(int *a,int n) /*定义两个参数:数组首地址与数组大小*/
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++) /*注意循环的上下限*/
if(a[i]>a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
冒泡法原理简单,但其缺点是交换次数多,效率低。
下面介绍一种源自冒泡法但更有效率的方法“选择法”。
(2)“选择法”
选择法循环过程与冒泡法一致,它还定义了记号k=i,然后依次把a[k]同后面元素比较,若a[k]>a[j],则使k=j.最后看看k=i是否还成立,不成立则交换a[k],a[i],这样就比冒泡法省下许多无用的交换,提高了效率。
void choise(int *a,int n)
{
int i,j,k,temp;
for(i=0;i<n-1;i++) {
k=i; /*给记号赋值*/
for(j=i+1;j<n;j++)
if(a[k]>a[j]) k=j; /*是k总是指向最小元素*/
if(i!=k) { /*当k!=i是才交换,否则a[i]即为最小*/
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
选择法比冒泡法效率更高,但说到高效率,非“快速法”莫属,现在就让我们来了解它。
(3)“快速法”
快速法定义了三个参数,(数组首地址*a,要排序数组起始元素下标i,要排序数组结束元素下标j). 它首先选一个数组元素(一般为a[(i+j)/2],即中间元素)作为参照,把比它小的元素放到它的左边,比它大的放在右边。然后运用递归,在将它左,右两个子数组排序,最后完成整个数组的排序。下面分析其代码:
void quick(int *a,int i,int j)
{
int m,n,temp;
int k;
m=i;
n=j;
k=a[(i+j)/2]; /*选取的参照*/
do {
while(a[m]<k&&m<j) m++; /* 从左到右找比k大的元素*/
while(a[n]>k&&n>i) n--; /* 从右到左找比k小的元素*/
if(m<=n) { /*若找到且满足条件,则交换*/
temp=a[m];
a[m]=a[n];
a[n]=temp;
m++;
n--;
}
}while(m<=n);
if(m<j) quick(a,m,j); /*运用递归*/
if(n>i) quick(a,i,n);
}
(4)“插入法”
插入法是一种比较直观的排序方法。它首先把数组头两个元素排好序,再依次把后面的元素插入适当的位置。把数组元素插完也就完成了排序。
void insert(int *a,int n)
{
int i,j,temp;
for(i=1;i<n;i++) {
temp=a[i]; /*temp为要插入的元素*/
j=i-1;
while(j>=0&&temp<a[j]) { /*从a[i-1]开始找比a[i]小的数,同时把数组元素向后移*/
a[j+1]=a[j];
j--;
}
a[j+1]=temp; /*插入*/
}
}
(5)“shell法”
shell法是一个叫 shell 的美国人与1969年发明的。它首先把相距k(k>=1)的那几个元素排好序,再缩小k值(一般取其一半),再排序,直到k=1时完成排序。下面让我们来分析其代码:
void shell(int *a,int n)
{
int i,j,k,x;
k=n/2; /*间距值*/
while(k>=1) {
for(i=k;i<n;i++) {
x=a[i];
j=i-k;
while(j>=0&&x<a[j]) {
a[j+k]=a[j];
j-=k;
}
a[j+k]=x;
}
k/=2; /*缩小间距值*/
}
}
上面我们已经对几种排序法作了介绍,现在让我们写个主函数检验一下。
#include<stdio.h>
/*别偷懒,下面的"..."代表函数体,自己加上去哦!*/
void bubble(int *a,int n)
{
...
}
void choise(int *a,int n)
{
...
}
void quick(int *a,int i,int j)
{
...
}
void insert(int *a,int n)
{
...
}
void shell(int *a,int n)
{
...
}
/*为了打印方便,我们写一个print吧。*/[code]
void print(int *a,int n)
{
int i;
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n");
}
main()
{ /*为了公平,我们给每个函数定义一个相同数组*/
int a1[]={13,0,5,8,1,7,21,50,9,2};
int a2[]={13,0,5,8,1,7,21,50,9,2};
int a3[]={13,0,5,8,1,7,21,50,9,2};
int a4[]={13,0,5,8,1,7,21,50,9,2};
int a5[]={13,0,5,8,1,7,21,50,9,2};
printf("the original list:");
print(a1,10);
printf("according to bubble:");
bubble(a1,10);
print(a1,10);
printf("according to choise:");
choise(a2,10);
print(a2,10);
printf("according to quick:");
quick(a3,0,9);
print(a3,10);
printf("according to insert:");
insert(a4,10);
print(a4,10);
printf("according to shell:");
shell(a5,10);
print(a5,10);
}
D. 求用c语言编写牛顿插值法
牛顿插值法:
#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=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.牛顿插值法#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=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;
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();
}
留下个邮箱,我发给你:牛顿插值法的程序设计与应用
E. 求c语言写的双三次插值函数
void
SPL(int
n,
double
*x,
double
*y,
int
ni,
double
*xi,
double
*yi);
是你所要。
已知
n
个点
x,y;
x
必须已按顺序排好。要插值
ni
点,横坐标
xi[],
输出
yi[]。
程序里用double
型,保证计算精度。
SPL调用现成的程序。
现成的程序很多。端点处理方法不同,结果会有不同。想同matlab比较,你需
尝试
调用
spline()函数
时,令
end1
为
1,
设
slope1
的值,令
end2
为
1
设
slope2
的值。
#include
<stdio.h>
#include
<math.h>
int
spline
(int
n,
int
end1,
int
end2,
double
slope1,
double
slope2,
double
x[],
double
y[],
double
b[],
double
c[],
double
d[],
int
*iflag)
{
int
nm1,
ib,
i,
ascend;
double
t;
nm1
=
n
-
1;
*iflag
=
0;
if
(n
<
2)
{
/*
no
possible
interpolation
*/
*iflag
=
1;
goto
LeaveSpline;
}
ascend
=
1;
for
(i
=
1;
i
<
n;
++i)
if
(x[i]
<=
x[i-1])
ascend
=
0;
if
(!ascend)
{
*iflag
=
2;
goto
LeaveSpline;
}
if
(n
>=
3)
{
d[0]
=
x[1]
-
x[0];
c[1]
=
(y[1]
-
y[0])
/
d[0];
for
(i
=
1;
i
<
nm1;
++i)
{
d[i]
=
x[i+1]
-
x[i];
b[i]
=
2.0
*
(d[i-1]
+
d[i]);
c[i+1]
=
(y[i+1]
-
y[i])
/
d[i];
c[i]
=
c[i+1]
-
c[i];
}
/*
----
Default
End
conditions
*/
b[0]
=
-d[0];
b[nm1]
=
-d[n-2];
c[0]
=
0.0;
c[nm1]
=
0.0;
if
(n
!=
3)
{
c[0]
=
c[2]
/
(x[3]
-
x[1])
-
c[1]
/
(x[2]
-
x[0]);
c[nm1]
=
c[n-2]
/
(x[nm1]
-
x[n-3])
-
c[n-3]
/
(x[n-2]
-
x[n-4]);
c[0]
=
c[0]
*
d[0]
*
d[0]
/
(x[3]
-
x[0]);
c[nm1]
=
-c[nm1]
*
d[n-2]
*
d[n-2]
/
(x[nm1]
-
x[n-4]);
}
/*
Alternative
end
conditions
--
known
slopes
*/
if
(end1
==
1)
{
b[0]
=
2.0
*
(x[1]
-
x[0]);
c[0]
=
(y[1]
-
y[0])
/
(x[1]
-
x[0])
-
slope1;
}
if
(end2
==
1)
{
b[nm1]
=
2.0
*
(x[nm1]
-
x[n-2]);
c[nm1]
=
slope2
-
(y[nm1]
-
y[n-2])
/
(x[nm1]
-
x[n-2]);
}
/*
Forward
elimination
*/
for
(i
=
1;
i
<
n;
++i)
{
t
=
d[i-1]
/
b[i-1];
b[i]
=
b[i]
-
t
*
d[i-1];
c[i]
=
c[i]
-
t
*
c[i-1];
}
/*
Back
substitution
*/
c[nm1]
=
c[nm1]
/
b[nm1];
for
(ib
=
0;
ib
<
nm1;
++ib)
{
i
=
n
-
ib
-
2;
c[i]
=
(c[i]
-
d[i]
*
c[i+1])
/
b[i];
}
b[nm1]
=
(y[nm1]
-
y[n-2])
/
d[n-2]
+
d[n-2]
*
(c[n-2]
+
2.0
*
c[nm1]);
for
(i
=
0;
i
<
nm1;
++i)
{
b[i]
=
(y[i+1]
-
y[i])
/
d[i]
-
d[i]
*
(c[i+1]
+
2.0
*
c[i]);
d[i]
=
(c[i+1]
-
c[i])
/
d[i];
c[i]
=
3.0
*
c[i];
}
c[nm1]
=
3.0
*
c[nm1];
d[nm1]
=
d[n-2];
}
else
{
b[0]
=
(y[1]
-
y[0])
/
(x[1]
-
x[0]);
c[0]
=
0.0;
d[0]
=
0.0;
b[1]
=
b[0];
c[1]
=
0.0;
d[1]
=
0.0;
}
LeaveSpline:
return
0;
}
double
seval
(int
n,
double
u,
double
x[],
double
y[],
double
b[],
double
c[],
double
d[],
int
*last)
{
int
i,
j,
k;
double
w;
i
=
*last;
if
(i
>=
n-1)
i
=
0;
if
(i
<
0)
i
=
0;
if
((x[i]
>
u)
||
(x[i+1]
<
u))
{
i
=
0;
j
=
n;
do
{
k
=
(i
+
j)
/
2;
if
(u
<
x[k])
j
=
k;
if
(u
>=
x[k])
i
=
k;
}
while
(j
>
i+1);
}
*last
=
i;
w
=
u
-
x[i];
w
=
y[i]
+
w
*
(b[i]
+
w
*
(c[i]
+
w
*
d[i]));
return
(w);
}
void
SPL(int
n,
double
*x,
double
*y,
int
ni,
double
*xi,
double
*yi)
{
double
*b,
*c,
*d;
int
iflag,last,i;
b
=
(double
*)
malloc(sizeof(double)
*
n);
c
=
(double
*)malloc(sizeof(double)
*
n);
d
=
(double
*)malloc(sizeof(double)
*
n);
if
(!d)
{
printf("no
enough
memory
for
b,c,d\n");}
else
{
spline
(n,0,0,0,0,x,y,b,c,d,&iflag);
if
(iflag==0)
printf("I
got
coef
b,c,d
now\n");
else
printf("x
not
in
order
or
other
error\n");
for
(i=0;i<ni;i++)
yi[i]
=
seval(ni,xi[i],x,y,b,c,d,&last);
free(b);free(c);free(d);
};
}
main(){
double
x[6]={0.,1.,2.,3.,4.,5};
double
y[6]={0.,0.5,2.0,1.6,0.5,0.0};
double
u[8]={0.5,1,1.5,2,2.5,3,3.5,4};
double
s[8];
int
i;
SPL(6,
x,y,
8,
u,
s);
for
(i=0;i<8;i++)
printf("%lf
%lf
\n",u[i],s[i]);
return
0;
}
F. 用C语言编写一个线性插值程序
#include<stdio.h>
doubleLerp(doublex0,doubley0,doublex1,doubley1,doublex)
{
doubledy=y1-y0;
if(dy==0){
printf("除0错误! ");
return0;
}
returnx*(x1-x0)/dy;
}
intmain()
{
doublex0,x1,y1,y0,x,y;
printf("Inptux0y0x1y1x:");
scanf("%lf%lf%lf%lf%lf",&x0,&y0,&x1,&y1,&x);
y=Lerp(x0,y0,x1,y1,x);
printf("y=%lf ",y);
return0;
}
G. 高分悬赏 求三维数据点C语言插值计算程序
问题补充,因字数限制,挪到这
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=0;i<n;i++)
{
P=1;
For(j=1;j<n;j++)
{
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.48 0.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),把该点的函数值赋给目标点做函数值,求高手帮忙写写。
H. C语言的拉格朗日插值法
double 型,输入格式要用 %lf, 不能用 %f。
(double 型,输出格式 可以用 %f 的。 千万不要混淆,以为输入也允许。)
改正后再看有无别的问题。另外注意运算中分母不能为0。
I. 用C语言实现拉格朗日插值、牛顿插值、等距结点插值算法
#include<stdio.h>
#include<stdlib.h>
#include<iostream.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次插值):";//获得插值次数
cin>>n;
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;
//cout<<f(0,j)<<endl;
y=y+yt;
}
return y;
}
float lagrange(float x,int count)
{
float y=0.0;
for(int k=0;k<count;k++)//这儿默认为count-1次插值
{
float p=1.0;//初始化p
for(int j=0;j<count;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组:";//要求用户输入数据组数
cin>>count;
if(count<=20)
break;//检查输入的是否合法
system("cls");
}
//获得各组数据
for(int i=0;i<count;i++)
{
cout<<"请输入第"<<i+1<<"组x的值:";
cin>>d[i].x;
cout<<"请输入第"<<i+1<<"组y的值:";
cin>>d[i].y;
system("cls");
}
cout<<"请输入x的值:";//获得变量x的值
cin>>x;
while(1)
{
int choice=3;
cout<<"请您选择使用哪种插值法计算:"<<endl;
cout<<" (0):退出"<<endl;
cout<<" (1):Lagrange"<<endl;
cout<<" (2):Newton"<<endl;
cout<<"输入你的选择:";
cin>>choice;//取得用户的选择项
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;
}
cout<<x<<" , "<<y<<endl;//输出最终结果
}