How to plot radical equations in python

Question:

I have three equations as shown below:

Refer this image for equations

How do I plot the graphs of them for a given X1,Y1,X2,Y2,X3,Y3,t1,t2,t3,S in python?

I tried removing square roots and finally at an equation in 4th degree and then tried to plot, but still I get 4 solutions out of which I have to again validate for two correct roots. Is there any straight-forward method or library to plot these type of equations?

Asked By: Narsimha Reddy

||

Answers:

It is not that easy, because what you have here are implicit equations.
You don’t have a y=f(x) formula to just plot it. What you have is something that tells you if a pair (x,y) is on the curve or not, but can’t tell you directly how to find x if you know y, or y if you know x (I assume you know what "implicit" and "explicit" means, but if not, it is that. y=f(x) gives explicitly y from x. f(x,y)=0 gives implicitly y from x… once you’ve solved the equation f(x,y)=0)

So, I can see 3 options to solve that.

  1. Transform the implicit formula into an explicit one. That is solve the equations. That is what you’ve already tried. That might be possible. I don’t know. And since this is stackoverflow, and that would be pure maths, I assume it is not, and we don’t want to.

  2. Use sympy to do it. Either by using sympy to solve the equation and obtain an explicit form. That is just a computer aided version of the 1st one. You could also do it using Maple of Wolfram alpha, etc.

  3. More on the spot on SO, where we are looking for code version, assume we can’t solve the equation, and therefore try to plot the thin area for which the equation is true.

I assume that what we see here is a localisation of a impact in a 2d area, given the time of its detection at 3 different points (like a seismic event detected by 3 seismograph, or like a tap on a touch screen detected by 3 microphones — I published, 2 decades ago a paper on how to make precise and cheap touch screen that way; at the time I was pretty sure that is was the most practical way… and now we all have cheap capacitive screens everywhere; but I still tend to think of these equation as a way to build such touch screens)

So, for practical application, let say that we have a speed of S=1, and 3 detectors at (5,5), (10,10) and (10,2). And that user touched point (6,6).
So (t1-t2)S=√2-√32≈-4.24, (t2-t3)S=0, (t3-t1)S≈4.24.

3.a Compute image of area where equation are almost true

import numpy as np
impost matplotlib.pyplot as plt
X1,Y1=5,5
X2, Y2=10,10
X3,Y3=10,2

# Creates 200x200 images of x and y
xx,yy=xx,yy=np.meshgrid(np.linspace(0,15,200), np.linspace(0,15,200))

# And a black image for the result
res=np.zeros((200,200,3), dtype=np.uint8)

# Helper values: distances d1, d2 and d3 of (x,y) from (Xk,Yk)
d1=np.sqrt((xx-X1)**2+(yy-Y1)**2)
d2=np.sqrt((xx-X2)**2+(yy-Y2)**2)
d3=np.sqrt((xx-X3)**2+(yy-Y3)**2)

# Compute image of area where d1-d2 is almost -4.24, so |d1-d2+4.24|<ε
eps=0.1
# Pixels where d1-d2 is almost -4.24 are red
res[np.abs(d1-d2+4.24)<eps,0]=255
# Pixels where d2-d3 is almost 0 are green
res[np.abs(d2-d3)<eps,1]=255
# Pixels where d3-d1 is almost 4.24 are blue
res[np.abs(d3-d1-4.24)<eps,2]=255

# Show this
plt.imshow(res)
plt.show()

enter image description here

Solution of all 3 equations is at the intersection.
Note that (0,0) is top left, and y axis is downward. Plus, scale in in pixels. Since we know that solution is a (6,6), and that those 200 pixels represent values from 0 to 15, we should expect solution to be at 6×200/15 → (80,80)

We could try to change the scale, to reverse the image, and then to thinner eps to have lines. Or try to find the middle of the area. But we won’t since that was just a way for me to explain that there is already a function that does all that in matplotlib : contour.

So, in reality, you’ll never use solution 3a. But I wanted to make clear what contour does in reality: it does not solve the equation, nor does it magically draw a 2d implicit curve. What it does is working on meshgrid and areas. But find the best "line" in that area.

3b. Contour

import numpy as np
import matplotlib.pyplot as plt
X1,Y1=5,5
X2, Y2=10,10
X3,Y3=10,2

# Creates 200x200 images of x and y
xx,yy=xx,yy=np.meshgrid(np.linspace(0,15,200), np.linspace(0,15,200))

# And a black image for the result
res=np.zeros((200,200,3), dtype=np.uint8)

# Helper values: distances d1, d2 and d3 of (x,y) from (Xk,Yk)
d1=np.sqrt((xx-X1)**2+(yy-Y1)**2)
d2=np.sqrt((xx-X2)**2+(yy-Y2)**2)
d3=np.sqrt((xx-X3)**2+(yy-Y3)**2)

# Plot contour for the 3 equations
plt.contour(xx,yy,d1-d2, [-4.24])
plt.contour(xx,yy,d2-d3, [0])
plt.contour(xx,yy,d3-d1, [4.24])
plt.show()

enter image description here

3c. sympy plot_implicit

Lastly, sympy also have a function dedicated to this task. But that means that you have to create a symbolic version of those equations. And sympy won’t really solve them: it is still a meshgrid based, thin area drawing.

import sympy
x,y=sympy.var('x y')
cur1=sympy.plot_implicit(sympy.sqrt((x-X1)**2 + (y-Y1)**2) - sympy.sqrt((x-X2)**2 + (y-Y2)**2) + 4.24, x_var=(x,0,15), y_var=(y,0,15), show=False)
cur2=sympy.plot_implicit(sympy.sqrt((x-X2)**2 + (y-Y2)**2) - sympy.sqrt((x-X3)**2 + (y-Y3)**2), x_var=(x,0,15), y_var=(y,0,15), show=False)
cur3=sympy.plot_implicit(sympy.sqrt((x-X3)**2 + (y-Y3)**2) - sympy.sqrt((x-X1)**2 + (y-Y1)**2) - 4.24, x_var=(x,0,15), y_var=(y,0,15), show=False)
cur1.append(cur2[0]) # Add line of cur2 to cur1 plot
cur1.append(cur3[0]) # Likewise for cur3
cur1.show()

enter image description here

Note that it is not exactly the same meshgrid I’ve used in my first solution. Because it is adaptative, so you get a more precise area. And yet, you can see that it is an area, not a curve: it has a thickness.

Only solution 3b is uniformly thin. And that is just because it draws a contour, yet it is also an area.

So, none of those 3 solution strictly speaking draw curve of the equations. Unless you solve those equations, you can’t draw them. What they do it to find, among all possible combination of x and y, those that are closer to be solutions of the equations. They look like some 1d-curve y=f(x) drawing. But in reality they are 2d-curve z=f(x,y) drawing, z being as much as possible a binary value meaning "on the curve or not".

Note, to give a positive conclusion: my favorite option is 3b. Just, do not let it deceive you and make you thinking that it draws a curve. It is just the same solution as 3a and 3c, but with some smart contour detection to end up with a thin uniform line.

Answered By: chrslg
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.