MATLAB: Display surface normal using quiver3

quiver3surfnorm

I have an MxN matrix Z after some processing, and wanted to retrieve the surface normals (as well as view it on a plot)
I've tried surfnorm(Z), but since it had looked pretty messy I thought of simply using quiver3, with the surface normals from the return value of surfnorm. Since the documentation states that quiver3(x,y,z,u,v,n) usage is such that the (u, v, n) vectors would be at location(x,y,z), I tried the following:
% I checked that surf(Z) and surf(X, Y, Z) gave the same plot
X = zeros(M, N);
Y = zeros(M, N);
for k = 1:M
X(k, :) = k;
end
for k = 1:N
Y(:, k) = k;
end
[U, V, W] = surfnorm(Z);
quiver3(X, Y, Z, U, V, W);
but I simply got a "cylindrical" vector plot (seemingly as if the origin of the vectors are the same (center of cylinder)). I tried with different Z matrices but the quiver3 plot was always similar looking.
Am I understanding any of the functions wrong? Would really appreciate some help.
surf result:
surfnorm result:
quiver3 result:

Best Answer

1. Use
[U,V,W] = surfnorm(X,Y,Z);
instead of
[U,V,W] = surfnorm(Z);
This will make sure that the quiver3 vectors are mapped to the right coordinates.
2. If you look at the last plot, the X/Y axes scales are 14 orders of magnitude larger than in your surf and surfnorm plots. The vectors from quiver3 are too long, which gives the illusion of a cylindrical plot. This might be fixed after step 1, but if not you can rescale them using
quiver3(X,Y,Z,U,V,W,0.5); % Half scale
Related Question