Solving systems of equations in R

感情迁移 提交于 2019-12-03 12:38:16

问题


Solving an equation symbolically can be achieved in R using the Ryacas library. For example

library(Ryacas)
yacas("Solve(x/(1+x) == a, x)")

gives

expression(list(x == a/(1 - a)))

Does anybody know how to (symbolically) solve a system of equations?

Thank you.


回答1:


Well, i use the excellent python library, sympy, for symbolic computation.

Using sympy, solving systems of equations straightforward:

>>> from sympy import *
>>> x,y = symbols('x y')
>>> solve([Eq(x + 5*y, 2), Eq(-3*x + 6*y, 15)], [x, y])
{y: 1, x: -3}

So that's how to solve a system of equations using symbolic algebra, except through a python package.

The good news is that there's an R port to sympy, called rsympy, which is available on CRAN, or Google Code, here.

I have never used rsympy, other than downloading/installing it and working through a couple of the simplest examples in the rsympy Manual. I have used the original python library a lot during the past three years and i can recommend it highly.




回答2:


Try this:

yacas( "OldSolve({a*x+y==0,x+z==0},{x,y})" )


来源:https://stackoverflow.com/questions/4075983/solving-systems-of-equations-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!