Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From Matlab to Python - Solve function

I made a Matlab function and I would like to convert it to Python to use with my web application.

I converted (.m file to .py file) almost everything using OMPC. However, I can't make the solve() function to work (I am using the sympy library).

This is the Matlab line:

SBC = solve(sqrt((xa-x)^(2)+(ya-y)^(2))-sqrt((xb-x)^(2)+(yb-y)^(2))-D12==0,sqrt((xa-x)^(2)+(ya-y)^(2))-sqrt((xc-x)^(2)+(yc-y)^(2))-D13==0,[x,y]);

And this is the Python line where x and y are symbols (with x = Symbol('x') and y = Symbol('y')) :

sbc = solve(
            sqrt((xa - x) ** (2) + (ya - y) ** (2))
            - sqrt((xb - x) ** (2) + (yb - y) ** (2))
            - D12 == 0,
            sqrt((xa - x) ** (2) + (ya - y) ** (2))
            - sqrt((xc - x) ** (2) + (yc - y) ** (2))
            - D13 == 0,
            [x, y]
        )

With this Python code, I am getting False instead of a result (it works great with the Matlab code).

Am I missing something?

EDIT:

And with this, I am getting [] :

# -*- coding: utf-8 -*-

from sympy import *

def alg(xa=None, ya=None, za=None, Ta=None, xb=None, yb=None, zb=None, Tb=None, xc=None, yc=None, zc=None, Tc=None, xd=None, yd=None, zd=None, Td=None, RSSIA=None, RSSIB=None, RSSIC=None, RSSID=None, txPower=None, n=None):
    n = 2
    c = 3 * 10 ** 8
    TOA12 = Ta - Tb
    TOA13 = Ta - Tc
    TOA14 = Ta - Td

    D12 = TOA12 * c
    D13 = TOA13 * c
    D14 = TOA14 * c
    x, y = symbols('x y')

    eqs = [sqrt((xa - x) ** (2) + (ya - y) ** (2)) - sqrt((xb - x) ** (2) + (yb - y) ** (2)) - D12,
   sqrt((xa - x) ** (2) + (ya - y) ** (2)) - sqrt((xc - x) ** (2) + (yc - y) ** (2)) - D13]

    print solve(eqs, [x, y])

alg(3,1,0,21.8898790015,4,6,0,21.8898790005,10,4,0,21.88987900009,9,0.5,0,21.889879000105,23.9,23.85,23.9,23.95,24,1)
like image 426
Anthony Avatar asked Feb 17 '16 17:02

Anthony


1 Answers

There is only one small change required in order to make it work. The reason why you receive False is that you use the == 0 in your function definition. In sympy, it is generally assumed that your functions evaluate to 0. To give an example taken from here:

If you want to solve the equations x+5y=2, -3x+6y=15 then you would do it as follows:

from sympy import *
x, y  = symbols('x y')
solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])

which gives you

{x: -3, y: 1}

Note that the equations are passed in a way that they evaluate to 0.

If you run it like you did

solve([x + 5*y - 2 == 0, -3*x + 6*y - 15 == 0], [x, y])

then also False is returned.

So for your example, the following would work:

from sympy import *

x, y, xa, xb, xc, ya, yb, yc, D12, D13 = symbols('x y xa xb xc ya yb yc D12 D13')

eqs = [sqrt((xa - x) ** (2) + (ya - y) ** (2)) - sqrt((xb - x) ** (2) + (yb - y) ** (2)) - D12,
       sqrt((xa - x) ** (2) + (ya - y) ** (2)) - sqrt((xc - x) ** (2) + (yc - y) ** (2)) - D13]

solve(eqs, [x, y])

Unfortunately, that does not run through on my private computer (my Python gets killed; apparently that is hard to solve) so I just tested it for a simpler version to demonstrate the principle:

eqs2 = [sqrt(xa - x) - D12,
       (yc - y) ** (2) - D13]
solve(eqs2, [x, y])

which then gives you the expected output:

[(-D12**2 + xa, -sqrt(D13) + yc), (-D12**2 + xa, sqrt(D13) + yc)]

Hope you have more luck on your machine to solve these complicated functions. But this post explains why you receive the False.

EDIT

With your modified code, you can get a solution if you decrease the precision of your parameters D12 and D13. Here is the solution you then get:

[sqrt((-x + 3)**2 + (-y + 1)**2) - sqrt((-x + 4)**2 + (-y + 6)**2) - 0.3, sqrt((-x + 3)**2 + (-y + 1)**2) - sqrt((-x + 10)**2 + (-y + 4)**2) - 0.42]
[{x: 6.45543078993649, y: 3.14390310591109}, {x: 6.67962865117349, y: 2.61399193301427}]

Are these the same you receive for the Matlab simulations?

Here is the modified code; please note that I force the output to be in form of a dictionary and also print the equations (I round to two decimals but it also works with 4; you can play with that):

from sympy import *

def alg(xa=None, ya=None, za=None, Ta=None, xb=None, yb=None, zb=None, Tb=None, xc=None, yc=None, zc=None, Tc=None, xd=None, yd=None, zd=None, Td=None, RSSIA=None, RSSIB=None, RSSIC=None, RSSID=None, txPower=None, n=None):
    n = 2
    c = 3 * 10 ** 8
    TOA12 = Ta - Tb
    TOA13 = Ta - Tc
    TOA14 = Ta - Td

    D12 =  round(TOA12 * c, 2)
    D13 =  round(TOA13 * c, 2)
    # D14 = TOA14 * c
    # x, y, D12, D13 = symbols('x y D12 D13')
    x, y = symbols('x y')

    eqs = [sqrt((xa - x) ** (2) + (ya - y) ** (2)) - sqrt((xb - x) ** (2) + (yb - y) ** (2)) - D12,
   sqrt((xa - x) ** (2) + (ya - y) ** (2)) - sqrt((xc - x) ** (2) + (yc - y) ** (2)) - D13]

    print eqs

    print solve(eqs, x, y, dict=True)

alg(3,1,0,21.8898790015,4,6,0,21.8898790005,10,4,0,21.88987900009,9,0.5,0,21.889879000105,23.9,23.85,23.9,23.95,24,1)
like image 115
Cleb Avatar answered Sep 28 '22 13:09

Cleb