Search code examples
pythonplotnonlinear-functions

python plot non linear equation


What is the easiest way to plot a non linear equation in python.

For example:

0 = sqrt((-6 - x) ** 2 + (4 - y) ** 2) - sqrt((1 - x) ** 2 + y ** 2) - 5

I would like to plot that equation for x in [0, 10] and look like a continuous curve.

Thanks!


Solution

  • Here's a simple way to plot implicit equations by using numpy+matplotlib:

    import matplotlib.pyplot
    from numpy import arange, meshgrid, sqrt
    
    delta = 0.025
    x, y = meshgrid(
        arange(0, 10, delta),
        arange(0, 10, delta)
    )
    
    matplotlib.pyplot.contour(
        x, y,
        sqrt((-6 - x) ** 2 + (4 - y) ** 2) - sqrt((1 - x) ** 2 + y ** 2) - 5,
        [0]
    )
    matplotlib.pyplot.show()
    

    Output:

    enter image description here

    This method is really handy to analize closed forms, for instance, a circle of radius 3 would look like:

    matplotlib.pyplot.contour(
        x, y,
        x**2+y**2-9,
        [0]
    )
    

    enter image description here