简体   繁体   English

如何解决给定 x 的 function?

[英]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, funBNaN ,对于 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.

相关问题 在执行seq函数时如何解决错误? - How do I solve error in executing seq function? 我怎么写这个函数x ^ .3 *(1-x)^。7 - How do I write this function x^.3*(1-x)^.7 在 Ubuntu 中安装 OpenBlender 时,当它可用时,如何解决“package 'httr' is not available for R version XXX”? - While installing OpenBlender in Ubuntu, how do I solve “package 'httr' is not available for R version X.X.X” when it IS available? 在 R 中,给定 y = f(x),如何找到 y/x 的全局最大值? - In R, given y = f(x), how do I find the global max of y/x? 如何创建返回给定数据框直方图的 function? - How do I create a function that returns the histogram of a given data frame? 在r中,如何使函数将特定字母识别为我要解决的未知值? - In r, how do I make my function recognize a specific letter as the unknown value that I want it to solve for? 如何执行模拟以在 R 中找到具有给定概率的 z-score (x) - How do I perform a simulation to find a z-score (x) with a given probability in R 如何在滚动回归中解决此错误? - How do I solve this error in a rolling regression? 如何在 R 函数中找到给定参数的可接受选项列表? - How do I find a list of the acceptable options for a given argument in an R function? R-如何使用向量给定的参数运行函数列表中的每个函数 - R - how do I run each function in a list of functions with parameters given by a vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM