簡體   English   中英

為什么我不能將 uniroot 與 plot 一起使用?

[英]Why can't I use uniroot with plot?

我正在研究使用uniroot function 來近似方程的根的代碼。 我正在嘗試 plot function 的行為通過uniroot作為自由變量的值更改:

library(Deriv)

f1 <- function(s) {
  (1 - 2*s)^(-3/2)*exp((8*s)/(1-2*s))
}

f2 <- function(s) {
  log(f1(s))
}

f3 <- Deriv(f2, 's')
f4 <- Deriv(f3, 's')
f5 <- Deriv(f4, 's')

upp_s <- 1/2 - 1e-20

f_est <- function(x) {
  f3a <- function(s) {f3(s = s) - x}
  
  s_ <- uniroot(f3a,
                lower = -9,
                upper = upp_s)$root
  
  return(s_)
}

plot(f_est, from = 0, to=100, col="red", main="header")

f_est 的f_est按預期工作。 但是,當通過plot function 時,uniroot 似乎會中斷:

> plot(f_est, from = 0, to=100, col="red", main="header")
Error in uniroot(f3a, lower = -9, upper = upp_s) : 
  f() values at end points not of opposite sign
In addition: Warning messages:
1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
  the condition has length > 1 and only the first element will be used
2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
 
 Error in uniroot(f3a, lower = -9, upper = upp_s) : 
f() values at end points not of opposite sign

function 的設置使得uniroot中指定的端點始終具有相反的符號,並且始終只有一個實根。 我還檢查以確認當f_est運行時端點沒有丟失。 我嘗試對所涉及的功能進行矢量化,但無濟於事。

為什么會這樣?

我能夠到達那里的大部分路

upp_s <- 0.497
plot(Vectorize(f_est), from = 0.2, to = 100)
  • 對於太小的 epsilon 值(由於浮點錯誤), 1/2 - epsilon不僅完全等於 1/2,而且我發現f3()對於 >= 0.498 的值給出 NaN。 upp_s設置為 0.497 工作正常。
  • plot()應用於 function 調用curve() ,它需要一個可以采用x向量的 function 。
  • 如果我從 0.1 開始曲線,則曲線以“端點處的 f() 值不是相反符號”中斷; 我沒有進一步挖掘並嘗試診斷出了什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM