title: Fit 3D Polynomial Surface with Python
body: <p>I have a python code that calculates z values dependent on x and y values. Overall, I have 7 x-values and 7 y-values as well as 49 z-values that are arranged in a grid (x and y correspond each to one axis, z is the height).</p>
  <p>Now, I would like to fit a polynomial surface of degree 2 in the form of <code>z = f(x,y)</code>.</p>
  <p>I found a Matlab command that does this calculation.
  (<a href="https://www.mathworks.com/help/curvefit/fit.html" rel="nofollow noreferrer">https://www.mathworks.com/help/curvefit/fit.html</a>)</p>
  <pre class="lang-matlab prettyprint-override"><code>load franke
  sf = fit([x, y],z,'poly23')
  plot(sf,[x,y],z)
  </code></pre>
  <p>I want to calculate the parameters of my 2 degree function in Python. I tried to use the scipy <code>curve_fit</code> function with the following fit function:</p>
  <pre class="lang-py prettyprint-override"><code>def func(a, b, c, d ,e ,f ,g ,h ,i ,j, x, y):
      return a + b * x**0 * y**0 + c * x**0 * y**1 + d * x**0 * y**2 
               + e * x**1 * y**0 + f * x**1 * y**1 + g * x**1 * y**2
               + h * x**2 * y**0 + i * x**2 * y**1 + j * x**2 * y**2
      
  guess = (1,1,1,1,1,1,1,1,1,1)
  params, pcov = optimize.curve_fit(func, x, y, guess)
  </code></pre>
  <p>But at this point I am getting confused and I am not sure, if this is the right approach to get the parameters for my fit function. Is there possibly another solution for this problem? Thank's a lot!</p>