简体   繁体   中英

Solving nonlinear equation in R

I have system of equations

x + y - xy = c1
x + z - xz = c2
ay + bz = c3

(a,b, c1,c2 and c3 are known) I want to know if this set of equation have a closed form solution. Or is optim best way to solve it accurately in R?

I'd recommend rSymPy , the R port of the awesome python library for symbolic math.

library(rSymPy)
sympy("var('x,y,a,b,z,c1,c2,c3')") # declare vars
sympy("solve([Eq(x+y-x*y,c1),Eq(x+z-x*z,c2),Eq(a*y-b*z,c3)],[x,y,z])",
      retclass="Sym")

If you're too lazy to do the algebra:

Wolfram Alpha says in the most general case (assuming none of these denominators are zero):

x=(a*c1+b*c2-c3)/(a+b-c3)
y=(b*c1-b*c2-c1*c3+c3)/(-a*c1+a-b*c2+b)
z=(a*(c1-c2)+(c2-1)*c3)/(a*(c1-1)+b*(c2-1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM