我编写了一个递归程序来计算给定输入x
的球形贝塞尔函数的前26个值。该程序似乎开始在j_8(x)
积累相对较大的错误。以下是一些代码的输出,它们根据前25个SBF(l=0 to l=24
)的某些已知高精度球面贝塞尔函数值找到了相对误差:
5.27753443514687e-16
9.21595688945492e-16
3.02006896635059e-15
7.62720601251909e-14
4.57824286846449e-12
4.42939097671948e-10
6.23673401270476e-08
1.20281992792456e-05
0.00304187901758528
0.976196008573827
387.486227014647
186360.170424407
106776056.913571
71856241567.0684
56117385601216.0
5.03357079061797e+16
5.13915094291702e+19
5.92532834322625e+22
7.6613338261931e+25
1.10398479254694e+29
1.76304629085447e+32
3.10469930465156e+35
6.00134333269024e+38
1.26807670217943e+42
2.91783063679757e+45
如您所见,在j_8(x)
值之后,错误开始增加。问题只是说我应该调整"我的程序将错误降低到E-10
,但我该怎么做?我完全失去了......
代码:
import Foundation
var x: Double = 0.0
var j_up_val: Double = 0.0
func j_up(x_val: Double, l_val: Double) -> Double {
if l_val == 0 {
j_up_val = sin(x_val)/x_val
} else if l_val == 1 {
j_up_val = (sin(x_val)/pow(x_val,2.0)) - (cos(x_val)/x_val)
} else if l_val == 2 {
j_up_val = ((2*(l_val-1)+1)/x_val) * ((sin(x_val)/pow(x_val,2.0)) - cos(x_val)/x_val) - sin(x_val)/x_val
} else {
let l2: Double = l_val - 1
let l3: Double = l_val - 2
j_up_val = ((2*(l_val-1)+1)/x_val)*j_up(x_val: x, l_val: l2) - j_up(x_val: x, l_val: l3)
}
return j_up_val
}
x = 1
print("x:", x)
for i in 0...25 {
var i_double: Double = 0.0
i_double = Double(i)
print("l:", i)
print(j_up(x_val: x, l_val: i_double))
print(" \n")
}
print("**********************")