当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++17 std::cyl_bessel_i用法及代码示例


在数学中,微分方程,x^{2}\frac{d^{2}y}{dx^{2}} + x\frac{dy}{dx} + (x^{2} - v^{2})y = 0具有特别重要的意义。方程的解是参数的函数v。对于积分和半积分值v,特别令人感兴趣的解,被称为圆柱贝塞尔函数,以著名的德国数学家弗里 Delhi 希·威廉·贝塞尔的名字命名。要求的原因v是积分还是半积分将在下面给出的解释中清楚。 由于这是一个二阶微分方程,必然有两个线性独立的解,称为第一类和第二类。因此,可以使用以下公式手动求解微分方程弗罗贝尼乌斯法。用于复杂参数的第一类贝塞尔函数x,被称为第一类修正贝塞尔函数,并表示为I_{v}(x)。该方法的应用产生了一个无限级数,其中包含以下项:xv, 给出, I_{v}(x) = \sum_{k = 0}^{\infty} \frac{1}{k!\Gamma (k+v+1)}\left(\frac{x}{2}\right)^{2k+v} 由于表达式中包含 Gamma Function,只能计算积分和半积分值,因此参数v必须是积分或半积分。 C++17 (GCC 7.1) 标准库cmath给出计算第一类圆柱贝塞尔函数值的函数(std::cyl_bessel_j)(这里没有讨论,但与我们讨论的非常相似)和正则修正贝塞尔函数的值(std::cyl_bessel_i)。两者对于小输入都具有相当高的精度,并且可用于各种工程应用。例子:
Input: x = 2.798465, v = 0 Output: 4.152234090041574 Input: x = 3.04513, v = 0.5 Output: 4.792979684692604
注意:以下源代码只能在 C++17 及更高版本上运行。可以检查给定代码的运行示例这里。要运行不同的输入,请访问链接并单击右下角的“Edit”。

// C++17 code for bessel function 
#include <bits/stdc++.h> 
using namespace std; 
  
// Compute the answer from the formulae for first 10 terms 
long double answer(long double x, long double v) 
{ 
  
    long double ans_by_expansion = 0; 
    long double fact = 1; 
  
    for (int k = 0; k < 10; fact = fact * (++k)) { 
        ans_by_expansion += pow((x / 2), (2 * k)) / pow(fact, 2); 
        cout << "ans_by_expansion till term k = "; 
        cout << k << " is " << ans_by_expansion << "\n"; 
    } 
  
  return ans_by_expansion; 
} 
  
// Driver code 
int main() 
{ 
    long double x = 2.798465; 
    long double v = 0; 
  
    // Compute the Regular Modified Bessel Function 
    // for v = 0, x = 2.798465 
    long double ans_by_function = cyl_bessel_i(v, x); 
  
    cout << setprecision(15) << fixed; 
    cout << "The answer by function for "
         << "Regular_Modified_Bessel_Function" << endl 
         << "(" << v << ", " << x << ") = "
         << ans_by_function << "\n"; 
  
    // calculate answer by expansion 
    long double ans_by_expansion = answer(x, v); 
  
    cout << "Absolute Error in answer by both the methods is = "; 
    cout << abs(ans_by_expansion - ans_by_function) << "\n"; 
  
    return 0; 
} 
输出:
The answer by function for Regular_Modified_Bessel_Function
(0.000000000000000, 2.798465000000000) = 4.152234090041574
ans_by_expansion till term k = 0 is 1.000000000000000
ans_by_expansion till term k = 1 is 2.957851589056250
ans_by_expansion till term k = 2 is 3.916147300248771
ans_by_expansion till term k = 3 is 4.124614053687001
ans_by_expansion till term k = 4 is 4.150123238967278
ans_by_expansion till term k = 5 is 4.152120966924739
ans_by_expansion till term k = 6 is 4.152229612892962
ans_by_expansion till term k = 7 is 4.152233953968095
ans_by_expansion till term k = 8 is 4.152234086767796
ans_by_expansion till term k = 9 is 4.152234089977698
Absolute Error in answer by both the methods is = 0.000000000063876


相关用法


注:本文由纯净天空筛选整理自AayushChaturvedi大神的英文原创作品 std::cyl_bessel_i in C++17。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。