Search code examples
matlabvectorsurface

Calculate Normal Vector on Fitted Surface


Is there any straight way to derive a normal vector on a linear surface fitted to data. The surface is created by fit function on my X,Y,Z data points:

sf = fit([X2, Y2],Z2,'poly11');
c = coeffvalues(sf);
P0 = [0; 0; c(1)];
plot(sf,[X2,Y2],Z2)

The linear polynomial surface in the graph: enter image description here Now I need to get the normal N vector in order to calculate the distances from all point to the surface by dot function:

dot(sf-P0,N)

Any guidance? Thanks a lot!


Solution

  • OK, according to here and many other sources, if you define a plane in the form of

    Ax + By + Cz = D

    Then, the normal vector you are looking for would be [A, B, C].

    Now by using fit, you obtain a sf object, whos coeffvalues c are in such an order that

    z=c(1)+c(2)x+c(3)y

    which is the same as

    c(2)x+c(3)y-z=c(1)

    There, you get a normal vector, N=[c(2), c(3), -1].

    Then, to calculate the distance between P0 and the surface, one way is

    (P0 * (N.') + c(1)) / norm(N)