0

I have a polynomial y = 0.230 + -0.046*x + -0.208*x^2 . I want to calculate the perpendicular to this line cutting another line at (X,Y).

Dan
  • 45,079
  • 17
  • 88
  • 157
  • possible duplicate of [How do I calculate the normal vector of a line segment?](http://stackoverflow.com/questions/1243614/how-do-i-calculate-the-normal-vector-of-a-line-segment) – Schorsch Aug 29 '13 at 14:53
  • 1
    or a possible duplicate of [How to find the normal vector at a point on a curve in Matlab](http://stackoverflow.com/questions/17324936/how-to-find-the-normal-vector-at-a-point-on-a-curve-in-matlab) – Schorsch Aug 29 '13 at 14:54
  • @Schorsch this is admittedly a basic math problem but those links don't provide a complete solution, only half of it... – Buck Thorn Aug 30 '13 at 06:59

2 Answers2

1

An alternative is to compute the analytical result which is not terribly difficult. (you could use the symbolic toolbox for that but the NN sitting on your head will do):

%Example data
x=0:0.1:10;
y = 0.230 + -0.046*x + -0.208*x.^2 ;
plot(x,y);

%Find the tangent and normals at all points (*edited*)
slope = (-0.046 + -2*0.208*x);
py = -1./slope;            % <-- modified from Dan's expression 
                           %     to use analytical derivative


%Choose a point
n = 60;
X = x(n);
Y = y(n);
hold on
plot(X, Y, 'or')

% Copying @Dan: Find the equation of the straight line normal to that point. You can do this in one step (yn = py(n)*(x - X)  + Y) but I've done it in two to illustrate where this comes from
c = Y - py(n)*X;
yn = py(n)*x + c;
plot(x, yn, 'g')
axis tight equal

Using axis equal is also a good idea in this example to see that you really have orthogonal curves.

Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
  • thanks you If my equation have higher order with x.^3 and x.^4 ,how can I modify your code that works fine for me with a seconde order. – user2724407 Sep 02 '13 at 06:19
  • 1
    @user2724407 you need to compute the *analytical* derivative (slope) of your polynomial and substitute where I edited by introducing variable `slope`. – Buck Thorn Sep 02 '13 at 06:34
  • Thanks you done it . With the equation found I want to find the intersect with another curve plotted from an array of double . – user2724407 Sep 02 '13 at 08:58
0
%Example data 
    x=0:0.1:10;
    y = 0.230 + -0.046*x + -0.208*x.^2 ;    
    plot(x,y);

%Find the tangent and normals at all points
    dy = [0 diff(y)./diff(x)];
    py = -1./dy;

%Choose a point
    n = 60;
    X = x(n);
    Y = y(n);
    hold on
    plot(X, Y, 'or')

%Find the equation of the straight line normal to that point. You can do this in one step (yn = py(n)*(x - X)  + Y) but I've done it in two to illustrate where this comes from
    c = Y - py(n)*X;
    yn = py(n)*x + c;
    plot(x, yn, 'g')
Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    Your derivative needs a fix: `dx = x(2)-x(1); py = -dx./dy;` – Buck Thorn Aug 29 '13 at 15:46
  • 1
    @TryHard Thanks, I corrected it. Now `dy` is found using `diff(y)./diff(x)` – Dan Aug 30 '13 at 07:31
  • Hi Dan and Try Hard thanks you for you help.One more question to finish this post , you're example xy is done with <1x101 double> horizontal [link](http://pro.ellip6.com/SebastienForum/Tab1.jpg) and mine is vertical [link](http://pro.ellip6.com/SebastienForum/Tab2.jpg) how can I convert from mine to yours or modify your code. Thanks you – user2724407 Sep 01 '13 at 11:43