'solving systems of equations with Ryacas package
How to use R package Ryacas to solve the following system of equations? I have tried oldSolve and solve but the syntax was incorrect.
library(Ryacas)
library(Ryacas0)
x1 <- ysym("x1")
x2 <- ysym("x2")
> q12
[1] 1.631145
> q13
[1] 2.179819
> q21
[1] 1.088441
> q23
[1] 2.720127
> q31
[1] 2.93306
> q32
[1] 2.288752
> q11
[1] -3.810965
> q22
[1] -3.808569
> q33
[1] -5.221812
> eq <- c(x1* (q11) + x2* q21 + (1-x1-x2) * q31,
+ x1* (q12) + x2* q22 + (1-x1-x2) * q32,
+ x1* (q13) + x2* q23 + (1-x1-x2) * q33)
> solve(eq, c(0,0,0), c('x1','x2'))
Error in yac_core(x) :
Yacas returned this error: CommandLine(1) : Invalid argument
If I use
js <- yacas("OldSolve({
x1*-3.8109 + x2*1.088 + (1-x1-x2)*2.933 == 0,
x1*1.6311 + x2*-3.8086 + (1-x1-x2)*2.720 == 0,
x1*2.1798 + x2*2.720 + (1-x1-x2)*-5.2218 == 0}, {x1,x2})")
> js
Yacas matrix:
[,1] [,2]
[1,] x1 == (-1.845 * x2 + 2.933)/6.7439 x2 == 15.1496643/42.01920504
How to get x2 = 0.3605414, x1 = 0.3362744?
Solution 1:[1]
Only OldSolve can solve a system of equations. Note that I rounded your coefficients. The result of the third equation is a bit far from zero. But since the two first ones form a system of 2 equations with 2 unknown variables, the solution to these two equations should be unique.
> yac("OldSolve({(-3.811)*x1+1.09*x2+2.93*(1-x1-x2)==0,1.63*x1-3.81*x2+2.29*(1-x1-x2)==0,2.18*x1+2.72*x2-5.22*(1-x1-x2)==0},{x1,x2})")
[1] "{{x1==((-1.84)*x2+2.93)/6.741,x2==13.50309/0.399057e2}}"
> x2=13.50309/0.399057e2
> x1=((-1.84)*x2+2.93)/6.741
> (-3.811)*x1+1.09*x2+2.93*(1-x1-x2)
[1] 0
> 1.63*x1-3.81*x2+2.29*(1-x1-x2)
[1] 2.220446e-16
> 2.18*x1+2.72*x2-5.22*(1-x1-x2)
[1] -0.000342292
Take for example
> yac("OldSolve({x-y==0, x==1, x+y==3}, {x, y})")
[1] "{{x==y,y==1}}"
The third equation is not satisfied, and you are in a similar situation.
Note that you can also use Where to substitute your coefficients:
> yac_str("OldSolve({x1*q11 + x2*q21 + (1-x1-x2)*q31==0, x1*q12 + x2*q22 + (1-x1-x2)*q32==0, x1*q13 + x2*q23 + (1-x1-x2)*q33==0} Where q11==-3.811 And q21==1.09 And q31==2.93 And q12==1.63 And q22==-3.81 And q32==2.29 And q13==2.18 And q23==2.72 And q33==-5.22, {x1, x2})")
[1] "{{x1==((-1.84)*x2+2.93)/6.741,x2==13.50309/0.399057e2}}"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
