如何使用R解决方程中的未知数?

huangapple go评论88阅读模式
英文:

How to use R to solve the unknowns in an equation?

问题

我有一个方程:

2/(1+exp(-4.292*x))-1 = 0.95

我想在不改变公式的情况下找到未知的 x。这是否可以在 R 或 Excel 中完成?
先谢谢!

英文:

I have an equation here:

2/(1+exp(-4.292*x))-1 = 0.95

I want to find the unknown x without changing the formula. Can this be done in R or Excel?
Thank you in advance!

答案1

得分: 2

尝试在Ryacas0中使用Solve得到一个符号结果,或者使用它并将其转换为数字结果:

library(Ryacas0)
library(readr)

x <- Sym("x")
res <- Solve(2/(1+exp(-4.292*x))-1 == 0.95, x); res
## Yacas向量:
## [1] x == -(log(0.05/1.95)/4.292)

res2 <- Eval(res); res2
## [1] "( x == 0.853579134699358 )"

parse_number(res2)
## [1] 0.8535791

或者,直接使用Ryacas0的Newton获得数值结果。参数包括要求解其根的表达式、变量名称、初始值和精度:

x <- Sym("x")
Eval(Newton(2/(1+exp(-4.292*x))-1-0.95, x, 1, 0.0001))
## [1] 0.8535791
英文:

Try Solve in Ryacas0 for a symbolic result or use that and convert it to a numeric result:

library(Ryacas0)
library(readr)

x &lt;- Sym(&quot;x&quot;)
res &lt;- Solve(2/(1+exp(-4.292*x))-1 == 0.95, x); res
## Yacas vector:
## [1] x == -(log(0.05/1.95)/4.292)

res2 &lt;- Eval(res); res2
## [1] &quot;( x == 0.853579134699358 )&quot;

parse_number(res2)
## [1] 0.8535791

Alternatively for a numeric result directly use Ryacas0 Newton. The arguments are the expression to solve for its root, the variable name, the starting value and the accuracy.

x &lt;- Sym(&quot;x&quot;)
Eval(Newton(2/(1+exp(-4.292*x))-1-0.95, x, 1, 0.0001))
## [1] 0.8535791

答案2

得分: 0

Apart from the great solutions already provided here if you are just interested in a "convenient" way to solve such equations the (mathematical) search engine "WolframAlpha" might be interesting, too:

https://www.wolframalpha.com/input?i=solve%282%2F%281%2Bexp%28-4.292*x%29%29-1+%3D+0.95%3B+x%29

It comes to the same final result for x = 0.853579, but can even write it in a closed form: x = (250 * (log(3) + log(13)))/1073 (with log() using exp(1) as base)

英文:

Apart from the great solutions already provided here if you are just interested in a "convenient" way to solve such equations the (mathematical) search engine "WolframAlpha" might be interesting, too:

https://www.wolframalpha.com/input?i=solve%282%2F%281%2Bexp%28-4.292*x%29%29-1+%3D+0.95%3B+x%29

It comes to the same final result for x = 0.853579, but can even write it in a closed form : x = (250 * (log(3) + log(13)))/1073 (with log() using exp(1) as base)

huangapple
  • 本文由 发表于 2023年3月10日 01:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定