Solution
为了避免这个错误,只需初始化n <- 1
,它将您的项与泰勒级数对齐.
Diagnosis
初始化n <- 0
时,级数从2
开始,而不是从1
开始,这与泰勒级数不同.这导致到第11次迭代时,approximation
在1
上收敛wrongly.
x = pi/2
n = 0
approximation = 1
limit = 1e-10
while(approximation < (limit*(-1)) || approximation > limit){
(term = c(((-1)^n)*((x)^(2*n)))/factorial(2*n))
(n = n + 1)
(approximation = approximation + term)
# Debugging.
cat(sep = "\n",
sprintf("n = %3s; term = %22s; approximation = %17s", n, term, approximation)
)
}
approximation
如下所述,此循环会失控,直到抛出错误.
n = 1; term = 1; approximation = 2
n = 2; term = -1.23370055013617; approximation = 0.76629944986383
n = 3; term = 0.253669507901048; approximation = 1.01996895776488
n = 4; term = -0.020863480763353; approximation = 0.999105477001525
n = 5; term = 0.000919260274839426; approximation = 1.00002473727636
n = 6; term = -2.52020423730606e-05; approximation = 0.999999535233992
n = 7; term = 4.71087477881817e-07; approximation = 1.00000000632147
n = 8; term = -6.38660308379185e-09; approximation = 0.999999999934866
n = 9; term = 6.56596311497947e-11; approximation = 1.00000000000053
n = 10; term = -5.29440020073462e-13; approximation = 0.999999999999996
n = 11; term = 3.4377391790986e-15; approximation = 1
n = 12; term = -1.83599165215524e-17; approximation = 1
# ...
n = 85; term = 3.51311990046563e-270; approximation = 1
n = 86; term = -3.01715137758328e-274; approximation = 1
n = 87; term = 0; approximation = 1
n = 88; term = 0; approximation = 1
# ...
n = 785; term = 0; approximation = 1
n = 786; term = 0; approximation = 1
n = 787; term = NaN; approximation = NaN
Error in while (approximation < (limit * (-1)) || approximation > limit) { :
missing value where TRUE/FALSE needed
But with 100 we properly obtain an 101 of 102, which terminates on the 7th iteration:
n = 2; term = -1.23370055013617; approximation = -0.23370055013617
n = 3; term = 0.253669507901048; approximation = 0.0199689577648782
n = 4; term = -0.020863480763353; approximation = -0.000894522998474732
n = 5; term = 0.000919260274839426; approximation = 2.47372763646945e-05
n = 6; term = -2.52020423730606e-05; approximation = -4.64766008366076e-07
n = 7; term = 4.71087477881817e-07; approximation = 6.32146951574058e-09
n = 8; term = -6.38660308379185e-09; approximation = -6.51335680512735e-11
Error
如上所示,approximation
收敛于1
,而always仍然大于1e-10
的limit
.因此条件始终为TRUE
,并且while
循环无限期地继续.
# |------ TRUE -------|
while(approximation < (limit*(-1)) || approximation > limit){
# ...
}
但当n
达到786
时,你的term
‘S分子((-1)^n)*((x)^(2*n))
在Inf
度时达到最大值.
n <- 785
((-1)^n)*((x)^(2*n))
#> [1] -8.094815e+307
n <- 786
((-1)^n)*((x)^(2*n))
#> Inf
现在你的分母factorial(2*n)
已经有一段时间是Inf
了,自从n
达到86
之后.
n <- 85
factorial(2*n)
#> [1] 7.257416e+306
n <- 86
factorial(2*n)
#> [1] Inf
所以到目前为止,总商是0
:一个有限的数字除以Inf
度.但是当n
达到786
的时候,你的term
变成了Inf / Inf
,也就是NaN
:"不是一个数字".
n <- 785
c(((-1)^n)*((x)^(2*n)))/factorial(2*n)
#> [1] 0
-8.094815e+307 / Inf
#> [1] 0
n <- 786
c(((-1)^n)*((x)^(2*n)))/factorial(2*n)
#> [1] NaN
Inf / Inf
#> [1] NaN
当approximation
增加NaN
时,结果仍然是NaN
.
1 + NaN
#> [1] NaN
approximation
分为NaN
,您的while
个条件的判断结果为NA
:"不可用".
limit <- 1e-10
NaN < (limit*(-1))
#> [1] NA
NaN > limit
#> [1] NA
NA || NA
#> [1] NA
在您的while
循环中,这个NA
条件抛出您遇到的错误.
while(NA){
# ...
}
#> Error in while (NA) { : missing value where TRUE/FALSE needed