简体   繁体   English

有两个未知数时,R中的uniroot

[英]uniroot in R when there are two unknowns

Consider a function f of two arguments x and a . 考虑两个参数xa的函数f First I take integration of f with respect to x , which becomes a function g of a . 第一予取的集成f相对于x ,这成为一个功能ga Second, I want to find the root of the resulting function g of a . 其次,我想找到a的结果函数g的根。 Can I do this using uniroot and integrate in R ? 我可以使用unirootintegrateR吗? If so, how? 如果是这样,怎么办? If not, is there a way to do this at all? 如果没有,有没有办法做到这一点? Thanks. 谢谢。

b <- 2

truncfn <- function(x) pmin(b, pmax(x, -b))

# thetashape and thetascale are constants
# x and a are arguments
f <- function(x, thetashape, thetascale, a){
  term1 <- -1/thetascale
  term2 <- (1-thetashape)/thetascale
  term3 <- x/(thetascale-thetashape*x)
  term1 + term2*term3 - a
}

# First, integrate f with respect to x
g <- integrate(truncfn(f), lower=0, upper=Inf)

# Second, find root of g
uniroot(g, ...)

You can define a function (I call it truncfn2 ) that calls truncfn on the result of the call to f , and then g integrates truncfn2 . 您可以定义一个函数(我称其为truncfn2 ),该truncfn在对f的调用结果上调用truncfn ,然后g集成truncfn2 Finally uniroot searches for the root of g : 最后, uniroot搜索g的根:

b <- 2
truncfn <- function(x) pmin(b, pmax(x, -b))

# thetashape and thetascale are constants
# x and a are arguments
f <- function(x, thetashape, thetascale, a){
  term1 <- -1/thetascale
  term2 <- (1-thetashape)/thetascale
  term3 <- x/(thetascale-thetashape*x)
  term1 + term2*term3 - a
}
truncfn2 <- function(x, thetashape, thetascale, a) truncfn(f(x, thetashape, thetascale, a))

g <- function(a) integrate(truncfn2, thetascale=1, thetashape=0.6, a=a, lower=0, upper=10)$value
uniroot(g, lower=-10, upper=10)
# $root
# [1] -1.867932
# 
# $f.root
# [1] 1.134733e-07
# 
# $iter
# [1] 7
# 
# $init.it
# [1] NA
# 
# $estim.prec
# [1] 6.103516e-05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM