ええと、等距離の点を持つニュートン補間多項式(より具体的にはニュートン-グレゴリー前方差分補間多項式)を使用して、与えられたx = Xの値f(x)を概算したいと思います。s =(X-x0)/ hと仮定します。ここで、x0は最初のxであり、hは、fの正確な値がわかっている残りのxを取得するためのステップです。
double coef (double s, int k)
{
double c(1);
for (int i=1; i<=k ; ++i)
c *= (s-i+1)/i ;
return c;
}
double P_interp_value(double s, int Num_of_intervals , double f[] /* values of f in these points */) // P_n_s
{
int N=Num_of_intervals ;
double *df0= new double[N+1]; // calculing df only for point 0
for (int n=0 ; n<=N ; ++n) // n here is the order
{
df0[n]=0;
for (int k=0, sig=-1; k<=n; ++k, sig=-sig) // k here is the "x point"
{
df0[n] += sig * coef(n,k) * f[n-k];
}
}
double P_n_s = 0;
for (int k=0; k<=N ; ++k ) // here k is the order
{
P_n_s += coef(s,k)* df0[k];
}
delete []df0;
return P_n_s;
}
int main()
{
double s=0.415, f[]={0.0 , 1.0986 , 1.6094 , 1.9459 , 2.1972 };
int n=1; // Num of interval to use during aproximacion. Max = 4 in these example
while (true)
{
std::cin >> n;
std::cout << std::endl << "P(n=" << n <<", s=" << s << ")= " << P_interp_value(s, n, f) << std::endl ;
}
}
それは印刷します:
1
P(n = 1、s = 0.415)= 0.455919
2
P(n = 2、s = 0.415)= 0.527271
3
P(n = 3、s = 0.415)= 0.55379
4
P(n = 4、s = 0.415)= 0.567235
比較対象:
http: //ecourses.vtu.ac.in/nptel/courses/Webcourse-contents/IIT-KANPUR/Numerical%20Analysis/numerical-analysis/Rathish-kumar/rathish-oct31/fratnode8.html
できます。これで、これらのコードの最適化を開始できます。