Got wrong normal vector

3 views (last 30 days)
AI-CHI Chang
AI-CHI Chang on 10 Aug 2022
Commented: AI-CHI Chang on 14 Aug 2022
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
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
axis equal;
grid on;
hold on;
Bruno Luong
Bruno Luong on 10 Aug 2022
Your normal is alright just your graphic representation tricks your brain.
AI-CHI Chang
AI-CHI Chang on 10 Aug 2022
yeah...then i think my problem is solved
thank you very much !! :D

Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 12 Aug 2022
Edited: David Goodmanson on 12 Aug 2022
Hi AC,
You were quite right in your suspicions of the 3d plot, which looks fishy. You might call this a configuration control issue. You have two independent functions, f and f1, that are supposed to represent the same thing. That carries some risk in programming. The two functions are supposed to be identical with x <--> s, y <--> t. But if you compare their second lines,
- 5*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... % f
- 5*(s/5 - s.^3 - s.^5).*exp(-s.^2-t.^2) ... % f1
you can see that there is a problem with the y.^5 term. I'm not sure which version is correct, but if you change y.^5 to x.^5, or if you change s.^5 to t.^5, in both cases the normal vectors in the plot are pretty clearly normal.
  1 Comment
AI-CHI Chang
AI-CHI Chang on 14 Aug 2022
OMG so embarrassing .... David Goodmanson thank you very much ><!!!
I should be more careful

Sign in to comment.

More Answers (0)


Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!