想利用插值法来近似一个正旋函数,但是输出的结果总是0????
#include<stdio.h>
#include<math.h>
#define n 4
#define h 1
#define a 0
#define b 10
main()
{
double jisuan(double *x,double value,double *y);
long i;
double x[n],y[n],c,value,chushi=0,f[b-a+1]={0,0.8415,0.9093,0.1411,-0.7568,-0.9589,-0.2794,0.6570,0.9894,0.4121,-0.5440};
char zifu;
printf("the input value is:");
scanf("%f",&value);
for(i=0;i<n;i++)
{
x[i]=chushi;
chushi=x[i]+h;
y[i]=f[0+h*i];
}
c=jisuan(x,value,y);
printf("\n the output value is: %f",c);
return;
}
double jisuan(double *x,double value,double *y)通过这个子函数进行近似计算
{
long chaxun(double value,double *x);
double s; long m;
m=chaxun(value, x);
s=(x[m]-value)/h*y[m-1];
s=s+(value-x[m-1])/h*y[m-1];
return (s);
}
long chaxun(double value,double *x)找出输入值所在的范围,因为近似的方法是分端拉哥郎日法
{
long i,m;
for(i=0;i<n;i++)
if(value>=x[i]&&value<=x[i+1])
{
m=i+1;
break;
}
return (m);
}