R求解类似于Excel求解器参数函数的方程

我有一个关于在R中解决函数的可能性的问题,并且使用excel来做同样的事情。

不过我想用R来表明R对我的同事更好:)

这是等式:

f0<-1e-9 t_pw<-30e-9 a<-30.7397582453682 c<-6.60935546184612 P<-1-exp((-t_pw)*f0*exp(-a*(1-b/c)^2)) 

我想findP<-0.5b值。 在Excel中,我们可以通过selectP值列并将其设置为0.5,然后使用求解器参数函数来完成。

我不知道哪种方法是最好的? 或者有其他的办法吗?

Thankx。

我有一个强烈的怀疑,你的方程应该包括-t_pw/f0 ,而不是-t_pw*f0 ,而t_pw应该是3.0e-9 ,而不是30e-9

  Pfun <- function(b,f0=1e-9,t_pw=3.0e-9, a=30.7397582453682, c=6.60935546184612) { 1-exp((-t_pw)/f0*exp(-a*(1-b/c)^2)) } 

然后@ Lyzander的uniroot()build议工作正常:

  u1 <- uniroot(function(x) Pfun(x)-0.5,c(6,10)) 

估计值在这里是8.05。

  par(las=1,bty="l") curve(Pfun,from=0,to=10,xname="b") abline(h=0.5,lty=2) abline(v=u1$root,lty=3) 

在这里输入图像说明

如果你想解决一个等式,最简单的事情就是使用base-R中的uniroot

 f0<-1e-9 t_pw<-30e-9 a<-30.7397582453682 c<-6.60935546184612 func <- function(b) { 1-exp((-t_pw)*f0*exp(-a*(1-b/c)^2)) - 0.5 } #interval is the range of values of b to look for a solution #it can be -Inf, Inf > uniroot(func, interval=c(-1000, 1000), extendInt='yes') Error in uniroot(func, interval = c(-1000, 1000), extendInt = "yes") : no sign change found in 1000 iterations 

正如你上面看到我的unitrootfunction失败。 这是因为你的方程没有单一的解决scheme,很容易看到。 exp(-0.0000000000030 * <positive number between 0-1>)实际上(非常接近)1,所以你的等式变成1 - 1 - 0.5 = 0 ,这不成立。 你也可以看到同样的情节:

 curve(func) #same result for curve(func, from=-1000, to=1000) 

在这里输入图像说明

在这个函数中,任何b的结果都是-0.5。

所以快速做到这一点的方法是uniroot但可能是一个不同的方程式。

和一个工作的例子:

 myfunc2 <- function(x) x - 2 > uniroot(myfunc2, interval=c(0,10)) $root [1] 2 $f.root [1] 0 $iter [1] 1 $init.it [1] NA $estim.prec [1] 8