1

I need to draw a graph in python using this function: b²x²+a²z²+2dxz²+d²z²-a²b²=0 where b, a and d will be different each time. The problem here for me is that I cannot separate X and Z. I've tried something like that.

import numpy as np
import matplotlib.pyplot as plt

z = -np.linspace(9,15,100)
x = np.linspace(-26,26,1000)

x,z = np.meshgrid(x,z)

a = 4
b = 2
d = 1

Z = a**2*z**2+2*d*z**2-a**2*b**2
X = b**2*x**2


plt.contour(x,z,(X+Z),[0])
plt.xlim([-1.5,1.5])
plt.ylim([-11.5,-8.5])
user2702405
  • 111
  • 2
  • 12
  • Your solution is right, but for your sample code `np.all(X+Z > 0)`, so there is nothing. – HYRY Apr 02 '16 at 00:15

2 Answers2

1

I don't know if matplotlib can create an implicit plot; a quick search of their documentation didn't turn up anything. But it appears you can use Sympy for that. From this SO question:

from sympy import var, Plot
var('x y')
Plot(x*y**3 - y*x**3)
Community
  • 1
  • 1
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
0

Working code here:

from functools import partial

import numpy
import scipy.optimize
import matplotlib.pyplot as pp

a = 4
b = 3
d = 0.6

def z(x, y):
    return b**2*x**2+a**2*y**2+2*d*x*y**2+d**2*y**2-a**2*b**2

x_window = 0, 5
y_window = 0, 5

xs = []
ys = []
for x in numpy.linspace(*x_window, num=200):
    try:
        # A more efficient technique would use the last-found-y-value as a 
        # starting point
        y = scipy.optimize.brentq(partial(z, x), *y_window)
    except ValueError:
        # Should we not be able to find a solution in this window.
        pass
    else:
        xs.append(x)
        ys.append(y)

pp.plot(xs, ys)
pp.xlim(*x_window)
pp.ylim(*y_window)
pp.show()
user2702405
  • 111
  • 2
  • 12