From Matlab to Python - Solve Function

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

I converted (.m file to .py file) almost everything using OMPC. However, I cannot get the solve() function to work (I use 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 a 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 get False instead of the result (works fine with Matlab code).

Did I miss something?

EDIT

And with this I get [] :

 # -*- 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) 
+5
source share
1 answer

It only requires one small change to work. The reason you get False is because you use == 0 in the definition of your function. Sympy usually assumes your functions are evaluated to 0 . To give an example from here :

If you want to solve the equations x+5y=2, -3x+6y=15 , you must do this 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 equations are passed so that they are evaluated as 0 .

If you run it like you did

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

then returns False .

So, for your example, the following will work:

 from sympy import * x, y, xa, xb, xc, ya, yb, yc, D12, D13 = symbols('xy 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, this does not work on my personal computer (my Python gets killed , apparently this is hard to solve), so I just tested it on a simpler version to demonstrate the principle:

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

which then gives the expected result:

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

We hope you can solve these complex functions on your machine. But this post explains why you get False .

EDIT

With the modified code, you can get a solution if you reduce the accuracy of your parameters D12 and D13 . Here is the solution you 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 they the same as for Matlab modeling?

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

 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('xy 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) 
+5
source

Source: https://habr.com/ru/post/1243245/


All Articles