【问题标题】:Solve nonlinear equation symbolically in R with `Ryacas` package or an alternative使用 Ryacas 包或替代方案在 R 中符号求解非线性方程
【发布时间】:2023-03-10 21:31:01
【问题描述】:

出于纯粹的好奇,如果有一个函数/包可以解决 R 中的简单非线性方程,我很感兴趣?

假设我想(象征性地)解决0 = C + 1/x^2。上述示例的预期结果是x = sqrt(-1/-C)


我尝试了Ryacas 包:

library("Ryacas")
Solve(yacas("C+1/x^2"))

返回错误:

Sym("Solve(", x, ",", y, ")") 中的错误:缺少参数“y”, 没有默认值

所以我做到了:

Solve(yacas("C+1/x^2"), 0)

没有返回任何有用的东西:

Yacas vector:
character(0)

我按照?yacas 上的说明安装yacas。似乎yacas 有效,demo(Ryacas) 生成输出。这是第一部分:

    demo(Ryacas)
    ---- ~~~~~~

Type  <Return>   to start : 

>   x <- -3 + (0:600)/300

>   exp0 <- expression(x ^ 3)

>   exp1 <- expression(x^2 + 2 * x^2)

>   exp2 <- expression(2 * exp0)

>   exp3 <- expression(6 * pi * x)

>   exp4 <- expression((exp1 * (1 - sin(exp3))) / exp2)

>   res1 <- yacas(exp4); print(res1)
expression(3 * (x^2 * (1 - sin(6 * (x * pi))))/(2 * x^3))

>   exp5 <- expression(Simplify(exp4))

>   res2 <- yacas(exp5); print(res2)
expression(3 * (1 - sin(6 * (x * pi)))/(2 * x))

>   plot(x, eval(res2[[1]]), type="l", col="red")

有什么提示吗?

【问题讨论】:

  • rootSolve提供了解决NLE的功能
  • 是的。我的错字。修好了
  • 包裹 Ryacas 可能会有帮助。
  • @symbolrush 对不起,我误读了您的问题(这里还早)。 RootSolve 将无法帮助您解决您的具体问题。对于您的Ryacas-问题:您安装了 yacas 吗?没有那个 Ryacs 将无法工作。有关说明,请参阅?yacas
  • Solve("C+1/x^2 == 0","x") 为我工作

标签: r symbolic-math solver equation-solving


【解决方案1】:

我们可以使用包Ryacas(感谢@Bhas 的提示)作为库yacas 的接口来求解符号方程:

library(Ryacas)

expr <- yacas("C+1/x^2 == 0")  #Generate yacas expression | note the double equals!

solv <- Solve(expr,"x") # Solve the expression for x
[1] x == root(abs(1/C), 2) * complex_cartesian(cos(argument(-1/C)/2), sin(argument(-1/C)/2))                      
[2] x == root(abs(1/C), 2) * complex_cartesian(cos((argument(-1/C) + 2 * pi)/2), sin((argument(-1/C) + 2 * pi)/2))

Yacas 显然会生成一个复数解,因为对于 C 的正值,这个方程只有复数根(负数的平方根)。由于我们有一个二次方程,因此还需要两个解。 complex_cartesian部分是指复平面内的旋转,即依赖于C的值(基本上是z=a*i + b类型的复数中a的值)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多