fitting a linear surface with numpy least squares

前端 未结 2 813
再見小時候
再見小時候 2021-01-20 17:51

So I want to solve the equation z= a + b*y +c*x,. getting a,b,c. ie: making a (plane) surface fit to a load of scatter points in 3D space.

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 18:40

    The constants a, b, and c are the unknowns you need to solve for.

    If you substitute your N (x, y, z) points into the equation, you'll have N equations for 3 unknowns. You can write that as a matrix:

    [x1 y1 1]{ a }   { z1 }
    [x2 y2 1]{ b }   { z2 }
    [x3 y3 1]{ c } = { z3 }
        ...
    [xn yn 1]        { zn }
    

    Or

    Ac = z
    

    where A is an Nx3 matrix, c is a 3x1 vector and z is a 3xN vector.

    If you premultiply both sides by the transpose of A, you'll have an equation with a 3x3 matrix that you can solve for the coefficients you want.

    Use LU decomposition and forward-back substitution.

提交回复
热议问题