vinash85
vinash85

Reputation: 431

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?

Upvotes: 0

Views: 448

Answers (2)

chepyle
chepyle

Reputation: 996

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")

Upvotes: 3

Ben Bolker
Ben Bolker

Reputation: 226097

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))

Upvotes: 2

Related Questions