斯特林插值公式程序

鉴于 N 浮点数x及其相应的函数值f(x)估计自变量x的任何中间值的数学函数值,即x=a。 例如:

null
Input : n = 5        x_1 = 0, x_2 = 0.5,         x_3 = 1.0, x_4 = 1.5,         x_5 = 2.0        f(x_1) = 0, f(x_2) = 0.191,         f(x_3) = 0.341, f(x_4) = 0.433,         f(x_5) = 0.477        a = 1.22Output : The value of function at 1.22 is 0.389 .As can be seen f(1.0) = 0.341 and f(1.5) = 0.433, so f(1.22) should be somewhere in between these two values . Using Stirling Approximation, f(1.22)comes out to be 0.389.Input : n = 7        x_1 = 0, x_2 = 5,         x_3 = 10, x_4 = 15,        x_5 = 20, x_6 = 25,         x_7 = 30         f(x_1) = 0, f(x_2) = 0.0875,         f(x_3) = 0.1763, f(x_4) = 0.2679,         f(x_5) = 0.364, f(x_6) = 0.4663,         f(x_7) = 0.5774        a = 16Output : The value of function at 16 is 0.2866 .

斯特林扩散

斯特林近似或斯特林插值公式是一种插值技术,用于获得已知数据点离散集范围内中间点处的函数值。 斯特林公式由高斯向前公式和高斯向后公式的平均值得到。高斯向前公式和向后公式都是用于获得表列集合中间附近函数值的公式。

如何找到

斯特林近似法涉及使用正向差分表,该差分表可由给定的x和f(x)或y组成,如下所示-

图片[25]-斯特林插值公式程序-yiteyi-C++库

这个表格是在x及其对应的f(x)或y的帮助下编制的。然后,通过计算前一列中前一列值和后一列值之间的差值来计算下一列值,如 Delta y_0 =y _1 –y _0 , Delta y_1 =y _2 –y _1 , Delta ^2 y_0 = Delta y_1 Delta y_0 等等 现在,求f(x)或y的高斯正演公式 A. 是: y_p = y_0 + pDelta y_0 + frac{p(p-1)}{2!}Delta ^2 y_-_1+frac{p(p-1)(p+1)}{3!}Delta ^3 y_-_1 + frac{p(p-1)(p+1)(p-2)}{4!}Delta ^4 y_-_2 + ....... 哪里 p= frac{a - x_0}{h} , A. 是我们必须确定f(x),x的点 _0 是从给定x中选择的更接近 A. (通常,从表格中间选择一个值),h是任意两个连续x之间的差值 _0 成为对应于x的值 _0 和x之前的值 _0 有负数下标,后面有负数下标 x_0 具有正下标,如下表所示-

图片[42]-斯特林插值公式程序-yiteyi-C++库

以及求f(x)或y的Gauss向后公式 A. 是: y_p = y_0 + pDelta y_-_1 + frac{p(p+1)}{2!}Delta ^2 y_-_1+frac{p(p-1)(p+1)}{3!}Delta ^3 y_-_2 + frac{p(p+1)(p-1)(p+2)}{4!}Delta ^4 y_-_2 + ....... 现在,取上述两个公式的平均值,得到斯特林近似公式,如下所示—— P_2_n(x) = y_0 + q.frac{Delta y_-_1+Delta y_0}{2}+frac{q^2}{2!},Delta^2 y_-_1 +frac{q(q^2-1)}{3!}.frac{Delta^3 y_-_2 + Delta^3 y_-_1}{2}+frac{q^2(q^2-1)}{4!}. Delta ^4 y_-_2 + frac{q(q^2-1)(q^2-2^2)}{5!}.frac{Delta ^5 y_-_3+ Delta ^5y_-_2}{2}+frac{q^2(q^2-1)(q^2-2^2)}{6!}.Delta^6y_-_3+....+ frac{q(q^2-1)(q^2-2^2)(q^2-3^2)...[q^2-(n-1)^2]}{(2n-1)!}* frac{Delta ^{2n-1}y_-_n+ Delta ^{2n-1}y_-_{n-1}}{2}+frac{q^2(q^2-1)(q^2-2^2)...[q^2-(n-1)^2]}{(2n)!} Delta^{2n}y_{-n}, 这里,在高斯公式中,q与p相同,其余所有符号都相同。 何时使用

  • 当q介于 frac{-1}{2} frac{1}{2}.
  • 在这个范围之外,它仍然可以使用,但计算值的准确性会更低。
  • 它给出了范围的最佳估计 frac{-1}{4} .

C++

// C++ program to demonstrate Stirling
// Approximation
#include <bits/stdc++.h>
using namespace std;
// Function to calculate value using
// Stirling formula
void Stirling( float x[], float fx[], float x1,
int n)
{
float h, a, u, y1 = 0, N1 = 1, d = 1,
N2 = 1, d2 = 1, temp1 = 1, temp2 = 1,
k = 1, l = 1, delta[n][n];
int i, j, s;
h = x[1] - x[0];
s = floor (n / 2);
a = x[s];
u = (x1 - a) / h;
// Preparing the forward difference
// table
for (i = 0; i < n - 1; ++i) {
delta[i][0] = fx[i + 1] - fx[i];
}
for (i = 1; i < n - 1; ++i) {
for (j = 0; j < n - i - 1; ++j) {
delta[j][i] = delta[j + 1][i - 1]
- delta[j][i - 1];
}
}
// Calculating f(x) using the Stirling
// formula
y1 = fx[s];
for (i = 1; i <= n - 1; ++i) {
if (i % 2 != 0) {
if (k != 2) {
temp1 *= ( pow (u, k) -
pow ((k - 1), 2));
}
else {
temp1 *= ( pow (u, 2) -
pow ((k - 1), 2));
}
++k;
d *= i;
s = floor ((n - i) / 2);
y1 += (temp1 / (2 * d)) *
(delta[s][i - 1] +
delta[s - 1][i - 1]);
}
else {
temp2 *= ( pow (u, 2) -
pow ((l - 1), 2));
++l;
d *= i;
s = floor ((n - i) / 2);
y1 += (temp2 / (d)) *
(delta[s][i - 1]);
}
}
cout << y1;
}
// Driver main function
int main()
{
int n;
n = 5;
float x[] = { 0, 0.5, 1.0, 1.5, 2.0 };
float fx[] = { 0, 0.191, 0.341, 0.433,
0.477 };
// Value to calculate f(x)
float x1 = 1.22;
Stirling(x, fx, x1, n);
return 0;
}


JAVA

// Java program to demonstrate Stirling
// Approximation
import java.io.*;
import static java.lang.Math.*;
public class A {
static void Stirling( double x[], double fx[],
double x1, int n)
{
double h, a, u, y1 = 0 , N1 = 1 , d = 1 ,
N2 = 1 , d2 = 1 , temp1 = 1 ,
temp2 = 1 , k = 1 , l = 1 , delta[][];
delta = new double [n][n];
int i, j, s;
h = x[ 1 ] - x[ 0 ];
s = ( int )floor(n / 2 );
a = x[s];
u = (x1 - a) / h;
// Preparing the forward difference
// table
for (i = 0 ; i < n - 1 ; ++i) {
delta[i][ 0 ] = fx[i + 1 ] - fx[i];
}
for (i = 1 ; i < n - 1 ; ++i) {
for (j = 0 ; j < n - i - 1 ; ++j) {
delta[j][i] = delta[j + 1 ][i - 1 ]
- delta[j][i - 1 ];
}
}
// Calculating f(x) using the Stirling
// formula
y1 = fx[s];
for (i = 1 ; i <= n - 1 ; ++i) {
if (i % 2 != 0 ) {
if (k != 2 ) {
temp1 *= (pow(u, k) -
pow((k - 1 ), 2 ));
}
else {
temp1 *= (pow(u, 2 ) -
pow((k - 1 ), 2 ));
}
++k;
d *= i;
s = ( int )floor((n - i) / 2 );
y1 += (temp1 / ( 2 * d)) *
(delta[s][i - 1 ] +
delta[s - 1 ][i - 1 ]);
}
else {
temp2 *= (pow(u, 2 ) -
pow((l - 1 ), 2 ));
++l;
d *= i;
s = ( int )floor((n - i) / 2 );
y1 += (temp2 / (d)) *
(delta[s][i - 1 ]);
}
}
System.out.print(+ y1);
}
// Driver main function
public static void main(String args[])
{
int n;
n = 5 ;
double x[] = { 0 , 0.5 , 1.0 , 1.5 , 2.0 };
double fx[] = { 0 , 0.191 , 0.341 , 0.433 ,
0.477 };
// Value to calculate f(x)
double x1 = 1.22 ;
Stirling(x, fx, x1, n);
}
}


Python3

# Python3 program to demonstrate Stirling
# Approximation
import math
# Function to calculate value using
# Stirling formula
def Stirling(x, fx, x1, n):
y1 = 0 ; N1 = 1 ; d = 1 ;
N2 = 1 ; d2 = 1 ;
temp1 = 1 ; temp2 = 1 ;
k = 1 ; l = 1 ;
delta = [[ 0 for i in range (n)]
for j in range (n)];
h = x[ 1 ] - x[ 0 ];
s = math.floor(n / 2 );
a = x[s];
u = (x1 - a) / h;
# Preparing the forward difference
# table
for i in range (n - 1 ):
delta[i][ 0 ] = fx[i + 1 ] - fx[i];
for i in range ( 1 , n - 1 ):
for j in range (n - i - 1 ):
delta[j][i] = (delta[j + 1 ][i - 1 ] -
delta[j][i - 1 ]);
# Calculating f(x) using the Stirling formula
y1 = fx[s];
for i in range ( 1 , n):
if (i % 2 ! = 0 ):
if (k ! = 2 ):
temp1 * = ( pow (u, k) - pow ((k - 1 ), 2 ));
else :
temp1 * = ( pow (u, 2 ) - pow ((k - 1 ), 2 ));
k + = 1 ;
d * = i;
s = math.floor((n - i) / 2 );
y1 + = (temp1 / ( 2 * d)) * (delta[s][i - 1 ] +
delta[s - 1 ][i - 1 ]);
else :
temp2 * = ( pow (u, 2 ) - pow ((l - 1 ), 2 ));
l + = 1 ;
d * = i;
s = math.floor((n - i) / 2 );
y1 + = (temp2 / (d)) * (delta[s][i - 1 ]);
print ( round (y1, 3 ));
# Driver Code
n = 5 ;
x = [ 0 , 0.5 , 1.0 , 1.5 , 2.0 ];
fx = [ 0 , 0.191 , 0.341 , 0.433 , 0.477 ];
# Value to calculate f(x)
x1 = 1.22 ;
Stirling(x, fx, x1, n);
# This code is contributed by mits


C#

// C# program to demonstrate Stirling
// Approximation
using System;
public class A
{
static void Stirling( double [] x, double [] fx,
double x1, int n)
{
double h, a, u, y1 = 0, d = 1, temp1 = 1,
temp2 = 1, k = 1, l = 1;
double [,] delta;
delta = new double [n, n];
int i, j, s;
h = x[1] - x[0];
s = ( int )Math.Floor(( double )(n / 2));
a = x[s];
u = (x1 - a) / h;
// Preparing the forward difference
// table
for (i = 0; i < n - 1; ++i)
{
delta[i, 0] = fx[i + 1] - fx[i];
}
for (i = 1; i < n - 1; ++i)
{
for (j = 0; j < n - i - 1; ++j)
{
delta[j, i] = delta[j + 1, i - 1]
- delta[j, i - 1];
}
}
// Calculating f(x) using the Stirling
// formula
y1 = fx[s];
for (i = 1; i <= n - 1; ++i)
{
if (i % 2 != 0)
{
if (k != 2)
{
temp1 *= (Math.Pow(u, k) -
Math.Pow((k - 1), 2));
}
else
{
temp1 *= (Math.Pow(u, 2) -
Math.Pow((k - 1), 2));
}
++k;
d *= i;
s = ( int )Math.Floor(( double )((n - i) / 2));
y1 += (temp1 / (2 * d)) *
(delta[s, i - 1] +
delta[s - 1, i - 1]);
}
else
{
temp2 *= (Math.Pow(u, 2) -
Math.Pow((l - 1), 2));
++l;
d *= i;
s = ( int )Math.Floor(( double )((n - i) / 2));
y1 += (temp2 / (d)) *
(delta[s, i - 1]);
}
}
Console.Write(+ y1);
}
// Driver Code
public static void Main()
{
int n;
n = 5;
double [] x = {0, 0.5, 1.0, 1.5, 2.0 };
double [] fx = {0, 0.191, 0.341, 0.433,
0.477 };
// Value to calculate f(x)
double x1 = 1.22;
Stirling(x, fx, x1, n);
}
}
// This code is contributed
// by Akanksha Rai


PHP

<?php
// PHP program to demonstrate Stirling
// Approximation
// Function to calculate value using
// Stirling formula
function Stirling( $x , $fx , $x1 , $n )
{
$y1 = 0; $N1 = 1;
$d = 1;
$N2 = 1; $d2 = 1; $temp1 = 1; $temp2 = 1;
$k = 1; $l = 1; $delta [ $n ][ $n ] = array ();
$h = $x [1] - $x [0];
$s = floor ( $n / 2);
$a = $x [ $s ];
$u = ( $x1 - $a ) / $h ;
// Preparing the forward difference
// table
for ( $i = 0; $i < $n - 1; ++ $i )
{
$delta [ $i ][0] = $fx [ $i + 1] - $fx [ $i ];
}
for ( $i = 1; $i < $n - 1; ++ $i )
{
for ( $j = 0; $j < $n - $i - 1; ++ $j )
{
$delta [ $j ][ $i ] = $delta [ $j + 1][ $i - 1] -
$delta [ $j ][ $i - 1];
}
}
// Calculating f(x) using the
// Stirling formula
$y1 = $fx [ $s ];
for ( $i = 1; $i <= $n - 1; ++ $i )
{
if ( $i % 2 != 0)
{
if ( $k != 2)
{
$temp1 *= (pow( $u , $k ) -
pow(( $k - 1), 2));
}
else
{
$temp1 *= (pow( $u , 2) -
pow(( $k - 1), 2));
}
++ $k ;
$d *= $i ;
$s = floor (( $n - $i ) / 2);
$y1 += ( $temp1 / (2 * $d )) *
( $delta [ $s ][ $i - 1] +
$delta [ $s - 1][ $i - 1]);
}
else
{
$temp2 *= (pow( $u , 2) -
pow(( $l - 1), 2));
++ $l ;
$d *= $i ;
$s = floor (( $n - $i ) / 2);
$y1 += ( $temp2 / ( $d )) *
( $delta [ $s ][ $i - 1]);
}
}
echo $y1 ;
}
// Driver Code
$n = 5;
$x = array (0, 0.5, 1.0, 1.5, 2.0 );
$fx = array ( 0, 0.191, 0.341, 0.433,
0.477 );
// Value to calculate f(x)
$x1 = 1.22;
Stirling( $x , $fx , $x1 , $n );
// This code is contributed by jit_t
?>


输出:

0.389 

主要 有利条件 斯特林公式比其他类似公式的优点是,它比其他差分公式下降得快得多,因此考虑前几个项本身将提供更好的精度,而它会受到 缺点 对于适用的斯特林近似,任何两个连续的x之间应该有一个一致的差。 参考—— B.S.格雷瓦尔的《高等工程数学》。 本文由 辛格先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享