How to find the normal vector at a point on a curve in MatLab

Sagar picture Sagar · Jun 26, 2013 · Viewed 15.6k times · Source

I have a curve and I want to find the normal vector at a given point on this curve, later I have to find the dot product of this normal vector with another vector.

I tried the gradient function of MatLab, but I guess it doesnt work when we need to find the gradient at a specific point still I am not sure if I am wrong.

Please guide me how can I achieve this in MatLab.

Thanks in advance.

Answer

macduff picture macduff · Jun 26, 2013

Using the explanation from this incredible SO question:

if we define dx=x2-x1 and dy=y2-y1, then the normals are (-dy, dx) and (dy, -dx).

Here's an example using an analytic curve of y = x^2

x = 0:0.1:1;
y = x.*x;
dy = gradient(y);
dx = gradient(x);
quiver(x,y,-dy,dx)
hold on; plot( x, y)

which gives:

Quiver

PS: Sorry about the tangential example!!! Got in a hurry. Thanks to Schorsch and Shawn314!