[英]How do I solve a function for a given x?
I have been browsing through quite some help pages, but I did not find the solution for my - probably - simple problem.我一直在浏览相当多的帮助页面,但我没有找到解决我的 - 可能 - 简单问题的解决方案。 I defined a function
我定义了一个 function
funB <- function(x) (0.8042851 +
((3.9417843-0.8042851)/(1+((x/0.4039609)^(-3.285016)))))
and would like to solve it for a given x (say, x = 0.2).并想为给定的 x 解决它(例如,x = 0.2)。 How do I do that?
我怎么做? I have looked at
uniroot()
and polyroot()
, but they did not seem to fit my function.我看过
uniroot()
和polyroot()
,但它们似乎不适合我的 function。
If you wanted to find the value of x
such that funB(x)
was equal to 0.2, you would do something like this:如果你想找到
x
的值使得funB(x)
等于 0.2,你可以这样做:
funB <- function(x) (0.8042851 +
((3.9417843-0.8042851)/(1+((x/0.4039609)^(-3.285016)))))
target <- 0.2
uniroot(function(x) funB(x)-target, interval=c(-5,10))
but there's a problem.但有一个问题。 It's up to you to pick an
interval
value that brackets a root (ie funB(x)<0.2
for the lower value and >0.2 for the upper value, or vice versa. funB
is NaN
for x<0, 0.8042851 for x==0, and increasing for x>0 (try curve(funB, from=-5, to=100, n=1001)
for example). So the solution you want (if I've guessed right about the meaning of your question) doesn't seem to exist.您可以选择一个包含根的
interval
值(即funB(x)<0.2
表示较低的值,>0.2 表示较高的值,反之亦然。对于 x<0, funB
是NaN
,对于 x== 是 0.8042851 0,并增加 x>0 (例如尝试curve(funB, from=-5, to=100, n=1001)
)。所以你想要的解决方案(如果我猜对了你的问题的含义)似乎不存在。
note : in general a negative value raised to a negative power is NaN
in R (even in cases where the answer "should" be defined, eg (-8)^(1/3) is the cube root of -8, which is -2...).注意:一般来说,在 R 中,负幂的负值是
NaN
(即使在“应该”定义答案的情况下,例如 (-8)^(1/3) 是 -8 的立方根,即-2...)。 If you're sure you know what you're doing you could replace (x/a)^b
with sign(x)*(abs(x)/a)^b)
... (if you make this change, the function appears well-behaved for x>-0.4 and funB(x)-0.2
does have a root between -0.3 and -0.2... but I have no idea if this makes sense for your application or not )如果您确定自己知道自己在做什么,则可以将
(x/a)^b
替换为sign(x)*(abs(x)/a)^b)
...(如果您进行此更改,则function 对于 x>-0.4 表现良好,而funB(x)-0.2
确实在 -0.3 和 -0.2 之间有根...但我不知道这对您的应用程序是否有意义)
Just to be sure that there is a root where you are expecting it, plot the graph of funB
.只是为了确保在您期望的地方有一个根,plot
funB
的图。
curve(funB)
Define an auxiliary function, f
, taking an extra argument and solve this new function for a = <target_value>
.定义一个辅助 function,
f
,采用一个额外的参数并为a = <target_value>
解决这个新的 function 。
f <- function(x, a) funB(x) - a
uniroot(f, interval = c(0, 1e3), a = 2)
#$root
#[1] 0.3485097
#
#$f.root
#[1] -0.0001305644
#
#$iter
#[1] 12
#
#$init.it
#[1] NA
#
#$estim.prec
#[1] 6.103516e-05
Well, I guess I must like doing things the hard way.好吧,我想我一定喜欢用艰难的方式做事。 I just rearranged your function to find its inverse:
我刚刚重新排列了您的 function 以找到它的倒数:
funC <- function(y) (((3.137499)/(y - 0.8042851) - 1)^(-1/3.285016)) * 0.4039609
So if I want to know when funB(x) == 3.7
I can do:因此,如果我想知道
funB(x) == 3.7
的时间,我可以这样做:
funC(3.7)
#> [1] 0.860193
and sure enough果然
funB(0.860193)
#> [1] 3.7
or indeed或者确实
funB(funC(1))
#> [1] 1
And as others have pointed out, x doesn't have a real value at funB(x) == 0.2
as you can see in this plot:正如其他人指出的那样,x 在
funB(x) == 0.2
处没有实际价值,正如您在此 plot 中看到的那样:
curve(funC, 0, 4)
Now, if you really want to know the complex root where funB(x) == 0.2
then you can modify funC
like so:现在,如果你真的想知道
funB(x) == 0.2
的复根,那么你可以像这样修改funC
:
funC <- function(y) (((3.137499)/(as.complex(y) - 0.8042851) - 1)^(-1/3.285016)) * 0.4039609
So now:所以现在:
funC(0.2)
#> [1] 0.1336917+0.1894797i
And therefore the answer to your question is 0.1336917 +/- 0.1894797i因此,您的问题的答案是0.1336917 +/- 0.1894797i
funB(complex(real = 0.133691691, imaginary = 0.1894797))
[1] 0.1999996+0i
Close enough.足够接近。
funB <- function(x) (0.8042851 + ((3.9417843-0.8042851)/(1+((x/0.4039609)^(-3.285016)))))
# call the function with desired input
funB(0.2)
...and the output: ...和 output:
> funB(0.2)
[1] 1.087758
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.