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).
Now, I would like to fit a polynomial surface of degree 2 in the form of z = f(x,y)
.
I found a Matlab command that does this calculation. (https://www.mathworks.com/help/curvefit/fit.html)
load franke
sf = fit([x, y],z,'poly23')
plot(sf,[x,y],z)
I want to calculate the parameters of my 2 degree function in Python. I tried to use the scipy curve_fit
function with the following fit function:
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)
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!