grzegorzmazur / yacas

Computer calculations made easy
http://www.yacas.org
GNU Lesser General Public License v2.1
120 stars 23 forks source link

weird solution given from oldSolve with Ryacas #344

Open esther1262 opened 2 years ago

esther1262 commented 2 years ago

Hi, I need to solve xQ = 0 where x = (x1, x2, 1-x1-x2). I use R package Ryacas.

original Q matrix:

> Q
           [,1]       [,2]       [,3]
[1,] -0.5982703  0.3528923  0.2453780
[2,]  0.2888467 -0.5776933  0.2888467
[3,]  0.4156334  0.2453780 -0.6610114

>   eq1 <- x1 * (Q[1,1]) + x2 * Q[2,1] + (1-x1-x2) * Q[3,1] 
>   eq2 <- x1 * (Q[1,2]) + x2 * Q[2,2] + (1-x1-x2) * Q[3,2]
>   eq3 <- x1 * (Q[1,3]) + x2 * Q[2,3] + (1-x1-x2) * Q[3,3]

>   tt <- paste0("OldSolve({",eq1,"==0, ", eq2,"==0, ",eq3,"==0}, {x1, x2})")
>   tt <- yacas(tt)
> tt
Yacas matrix:
     [,1]                                                   [,2]     
[1,] x1 == (-0.1267867438 * x2 + 0.4156334028)/1.0139036847 x2 == NaN

If I used the original Q matrix for the system of equation, the solution would be undefined. (Why?)

I had to round it first, and then used oldSolve in Ryacas package to solve the system:

> Q <- round(Q, 3)
> Q
       [,1]   [,2]   [,3]
[1,] -0.598  0.353  0.245
[2,]  0.289 -0.578  0.289
[3,]  0.416  0.245 -0.661

eq1 <- x1 * (Q[1,1]) + x2 * Q[2,1] + (1-x1-x2) * Q[3,1] 
eq2 <- x1 * (Q[1,2]) + x2 * Q[2,2] + (1-x1-x2) * Q[3,2]
eq3 <- x1 * (Q[1,3]) + x2 * Q[2,3] + (1-x1-x2) * Q[3,3]

tt <- paste0("OldSolve({",eq1,"==0, ", eq2,"==0, ",eq3,"==0}, {x1, x2})")
tt <- yacas(tt)
> tt
Yacas matrix:
     [,1]                              [,2]   
[1,] x1 == (-0.127 * x2 + 0.416)/1.014 x2 == 0

The answer is very weird. But if I use other online calculators e.g. wolfram to solve it, the solution = (0.3669, 0.3458, 0.2872), although xQ = (0,0,0.07) due to rounding.

How to fix this issue? Or is that possible to increase the tolerance of the command? Thx