Equivalent of `polyfit` for a 2D polynomial in Python

后端 未结 3 2124
误落风尘
误落风尘 2020-11-29 05:45

I\'d like to find a least-squares solution for the a coefficients in

z = (a0 + a1*x + a2*y + a3*x**2 + a4*x**2*y + a5*x**2*y**2 + a6*y**2 +
            


        
3条回答
  •  一整个雨季
    2020-11-29 06:42

    Excellent answer by Saullo Castro. Just to add the code to reconstruct the function using the least-squares solution for the a coefficients,

    def poly2Dreco(X, Y, c):
        return (c[0] + X*c[1] + Y*c[2] + X**2*c[3] + X**2*Y*c[4] + X**2*Y**2*c[5] + 
               Y**2*c[6] + X*Y**2*c[7] + X*Y*c[8])
    

提交回复
热议问题