我们必须使用WHILE循环来解决这个问题(将cos的值近似为-或+1-E10),我相信我已经完成了所有正确的设置,但我一直收到错误消息"需要真/假的地方缺少值".

问题说明

函数的泰勒展开式是函数作为无穷项和的一种表示.我们可以通过取无穷和的前几项来近似函数.我们包含的术语越多,我们的近似值就越好.

余弦函数$cos(X)$的泰勒展开为: 1-((x^2)/(2!))+(x^4)/(4!)...

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)
}
approximation

这是我try 的代码,但就像我说的,它一直给我上面提到的错误.

推荐答案

Solution

为了避免这个错误,只需初始化n <- 1,它将您的项与泰勒级数对齐.

Diagnosis

初始化n <- 0时,级数从2开始,而不是从1开始,这与泰勒级数不同.这导致到第11次迭代时,approximation1上收敛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-10limit.因此条件始终为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

R相关问答推荐

如何删除R中除某些特定名称外的所有字符串?

在ggplot中为不同几何体使用不同的 colored颜色 比例

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

如何基于两个条件从一列中提取行

计算直线上点到参考点的总距离

如何使用前缀作为匹配来连接数据帧?

在gggraph中显示来自不同数据帧的单个值

如何计算增加10米(0.01公里)的行?

将摘要图添加到facet_WRAP gglot的末尾

Conditional documentr::R中数据帧的summarize()

如何判断代码是否在R Markdown(RMD)上下文中交互运行?

变异以按组从其他列创建具有最大和最小值的新列

ggplot斜体轴刻度标签中的单个字符-以前的帖子建议不工作

需要一个函数来在第一行创建一个新变量,然后用新变量替换一个不同的变量(对于多行)

计算多变量的加权和

使用LAG和dplyr执行计算,以便按行和按组迭代

有没有办法更改ggplot2中第二个y轴的比例限制?

将某个阈值以下的列中的值分类到不同的列中,否则保持该列的原样

Package emMeans:如果emmip模型中包含的变量较少,emMeans模型中的其他变量设置为什么?

Ggplot2水平线和垂直线的图例图标不匹配