MATLAB: How to calculate the angle between two surfaces

anglecalculateplane angleplanes

I have two planes with 50×50 points and I want to find the angle between them. The first is a reference plane at z = 0 and the second is a measured surface sample (see graph). I can get matlab to display the surface normal using surfnorm but it doesn't seem to output that data anywhere.
Any help would be very appreciated!

Best Answer

The command
[Nx,Ny,Nz] = surfnorm(Z)
will return surface normals to surface Z. See:
https://www.mathworks.com/help/matlab/ref/surfnorm.html
However these normals will change from point to point. You need the equation of the best fit plane to obtain a single over-all normal to your points. For a plane with equation
a*x+b*y+c*z+d = 0
its normal is the vector v = [a,b,c]. To make it of unit length do:
v = v/norm(v);
As I think you are aware, the angle between the normals to two planes is the same as the angle between those planes. The angle between two 3D vectors v1 and v2 is:
ang = atan2(norm(cross(v1,v2)),dot(v1,v2));