[英]Solve a function in R similar to Goal Seeker in Excel
I saw already a similar question, but I can´t figure out, how to do. 我看到了一个类似的问题,但我不知道,该怎么做。 Maybe you can help. 也许你可以帮忙。
Back solving a function or goal seek in R 在R中回复求解函数或目标
ES <- function(y, z){-y * (1-pnorm(y/z)) + z * dnorm(y/z)} # = x
ES(906.19, 707.1) #33.47587
What I want is to solve for y, so z and x (33.4) is known. 我想要的是为y求解,所以z和x(33.4)是已知的。 I have seen the solve and optim function, but I am not able to get the expected result. 我已经看到了solve和optim函数,但是我无法获得预期的结果。
Thanks! 谢谢!
We can use uniroot
to find the root of ES(y, z) - x
for y
given values z = 707.1
and x = 33.4
. 我们可以用uniroot
找到的根ES(y, z) - x
为y
给定的值z = 707.1
和x = 33.4
。
ES <- function(y, z) -y * (1 - pnorm(y / z)) + z * dnorm(y / z)
res <- uniroot(function(x, y, z) ES(y, z) - x, c(0, 1000), z = 707.1, x = 33.4)
The solution for y
is then 那么y
的解决方案就是
res$root
#[1] 906.9494
We confirm that E(y, z) = x
我们确认E(y, z) = x
ES(res$root, 707.1)
#[1] 33.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.