Got wrong normal vector
Afficher commentaires plus anciens
I wrote a code presented the scatter points on a peak function, and now I want to solve for unit normal vectors at those points.
ok...maybe this is a math problem...why I got a result looks like tangetial vector...
The arrow on the surrounding flat plant looks good but the peak's normal vectors obviously wrong.

% function handler
f = @(x,y)2*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 5*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2); % z
rng('default');
ds = 0.0005;
range = (-2:ds:2)';
width = 4;
x = -width+(width-(-width))*rand(length(range),1); %
y = -width+(width-(-width))*rand(length(range),1); % r = a + (b-a).*rand(N,1)
z = f(x,y);
p = [x y z];
p_origin = p;
num_pt = size(p,1);
syms s t
f1 = 2*(1-s).^2.*exp(-(s.^2) - (t+1).^2) ...
- 5*(s/5 - s.^3 - s.^5).*exp(-s.^2-t.^2) ...
- 1/3*exp(-(s+1).^2 - t.^2);
dfs = diff(f1,s);
dft = diff(f1,t);
normal_x = matlabFunction(dfs);
normal_y = matlabFunction(dft);
normal = [normal_x(x,y),normal_y(x,y),-ones(num_pt,1)];
norm_nor = sqrt(sum(normal.*normal,2));
normal = bsxfun(@rdivide,normal,norm_nor);
% draw arrow
x=p(:,1);
y=p(:,2);
z=p(:,3);
u=normal(:,1);
v=normal(:,2);
w=normal(:,3);
plot3(x,y,z,'b.');
axis equal;
grid on;
view(45,15);
hold on;
quiver3(x,y,z,u,v,w,'r','LineWidth',1,'MaxHeadSize',5);
6 commentaires
Bruno Luong
le 10 Août 2022
"I got a result looks like tangetial vector."
May be it just looks like. Hard to tell with your graphic representation.
AI-CHI Chang
le 10 Août 2022
Modifié(e) : AI-CHI Chang
le 10 Août 2022
Bruno Luong
le 10 Août 2022
Outward => inward, just flip the sign from
normal = [normal_x(x,y),normal_y(x,y),-ones(num_pt,1)]
to
normal = -[normal_x(x,y),normal_y(x,y),-ones(num_pt,1)]
AI-CHI Chang
le 10 Août 2022
Modifié(e) : AI-CHI Chang
le 10 Août 2022
Bruno Luong
le 10 Août 2022
Your normal is alright just your graphic representation tricks your brain.
AI-CHI Chang
le 10 Août 2022
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Mathematics and Optimization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


