5

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

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Oh I see. Thanks about this. I just saw that this was wrote on the first lines of the Solvers part on the Sympy doc :( However, I still have a problem, since the result is [], instead of {x:…, y:…}. Any idea ?
Could you edit the question and add the code which gives you the []?! As written, I did not get a result either because my Python session gets killed after a couple of minutes; you try to solve far too difficult equations... ;)
Done ! :) My session didn't get killed, but takes like 5 seconds to "solve".
Ok, I updated your code and it is working fine now. Please check whether these are the same results you get using Matlab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.