[英]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)
1/2 - epsilon
不僅完全等於 1/2,而且我發現f3()
對於 >= 0.498 的值給出 NaN。 將upp_s
設置為 0.497 工作正常。plot()
應用於 function 調用curve()
,它需要一個可以采用x
值向量的 function 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.