title: Fitting a polynomial function for a vector field in python
body: <p>At first, thank you everybody for the amazing work on stackoverflow... you guys are amazing and have helped me out quite some times already. Regarding my problem: I have a series of vectors in the format (VectorX, VectorY, StartingpointX, StartingpointY)</p>
  
  <pre><code>data = [(-0.15304757819399128, -0.034405679205349315, -5.42877197265625, 53.412933349609375), (-0.30532995491023485, -0.21523935094046465, -63.36669921875, 91.832427978515625), (-0.15872430479453215, -0.077999419482978283, -67.805389404296875, 81.001983642578125), (-0.36415549211687903, -0.33757147194808113, -59.015228271484375, 82.976226806640625), (0.0, 0.0, 0.0, 0.0), (-0.052973530805275004, 0.098212384392411423, 19.02667236328125, -13.72125244140625), (-0.34318724086483599, 0.17123742336019632, 80.0394287109375, 108.58499145507812), (0.19410169197834648, -0.17635303976555861, -55.603790283203125, -76.298828125), (-0.38774018337716143, -0.0824692384322816, -44.59942626953125, 68.402496337890625), (0.062202543524108478, -0.37219011831012949, -79.828826904296875, -10.764404296875), (-0.56582988168383963, 0.14872365390732512, 39.67657470703125, 97.303192138671875), (0.12496832467900276, -0.12216653754859408, 24.65948486328125, -30.92584228515625)]
  </code></pre>
  
  <p>When I plot the vectorfield it looks like this: </p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  def main():
      # Format Data...
      numdata = len(data)
      x = np.zeros(numdata)
      y = np.zeros(numdata)
      u = np.zeros(numdata)
      v = np.zeros(numdata)
      for i,el in enumerate(data):
          x[i] = el[2]
          y[i] = el[3]
          # length of vector 
          z[i] = math.sqrt(el[0]**2+el[1]**2)
          u[i] = el[0]
          v[i] = el[1]
  
      # Plot
      plt.quiver(x,y,u,v )
      # showing the length with color 
      plt.scatter(x, y, c=z)
      plt.show()
  
  
  main()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/fF01t.png" alt="Vector plot"></p>
  
  <p>I want to create a polynomial function to fit a continous vector field for the whole area. After some research I found the following functions for fitting polynoms in two dimensions. The problem is, that it only accepts one value for the value that is fitted.</p>
  
  <pre><code>def polyfit2d(x, y, z, order=3):
      ncols = (order + 1)**2
      G = np.zeros((x.size, ncols))
      ij = itertools.product(range(order+1), range(order+1))
      for k, (i,j) in enumerate(ij):
          G[:,k] = x**i * y**j
      m, _, _, _ = np.linalg.lstsq(G, z)
      return m
  
  def polyval2d(x, y, m):
      order = int(np.sqrt(len(m))) - 1
      ij = itertools.product(range(order+1), range(order+1))
      z = np.zeros_like(x)
      for a, (i,j) in zip(m, ij):
          z += a * x**i * y**j
      return z
  </code></pre>
  
  <p>Also when I tried to fit the one dimensional length of the vectors, the values returned from the polyval2d were completely off. Does anybody know a method to get a fitted function that will return a vector (x,y) for any point in the grid?</p>
  
  <p>Thank you!</p>