如何在Matlab中的曲线中的指定点绘制切线

时间:2013-09-18 12:42:15

标签: matlab matlab-figure curve

我在这些指定点处有一个点x,y和各个角度的数组。我想在这些点上绘制一条切线,我无法弄清楚如何继续。

http://postimg.org/image/s2y1pqqaj/

如命令窗口所示,第1列包含x个点,第2列包含y个点,第3列包含相应的切线角度。图1是x和y点之间的图。 我知道斜率,即每个点的切线角度,你可以在第3列看到它。但是无法理解如何实现它以在这些点上绘制切线。对于切线'y = mx + b'的等式,其中m - 斜率和b是y截距。感谢你。

这是代码

% Fill in parr a list of points and tangents to be used for display.
% @param parr (x,y) Array of points.
% @param tan Array of tangents.
% @param lengthStep Distance between points.

 function [x y tan] = GetPointListForDisplay(m_Length,r1,r2,count)

   global nn ca ce length; 

   GetLength = m_Length;
   length = GetLength;   

   ca = GetCurvatureAtDeltaLength(0.0);     
   ce = GetCurvatureAtDeltaLength(length);

   %if ((abs(ca) < 1.0/10000.0) && (abs(ce) < 1.0/10000.0))
   %end

   radius = 1.0 ./ max(abs(ca), abs(ce));

   %if (radius < 0.1)
   %end

   nn = 3 + (180.0 * length/(2*pi*radius));  % Using modified formula of arc here

   lengthStep = length/nn; 
   currLen = -lengthStep;

   while (1) 

       currLen = currLen + lengthStep;
       if (currLen > m_Length)

           currLen = m_Length; 
       end

       [x,y] = GetPointAtDeltaLength(currLen);
       [tan] = GetTangentGridBearingAtDeltaLength(currLen); 

       z(count,1) = x;
       z(count,2)= y;
       z(count,3)= tan;

       figure(1);

       %plot(z(count,1).*sin(z(count,3)),z(count,2).*cos(z(count,3)),'b*');
       %plot(z(1,count).*cos(z(3,count)),z(2,count).*sin(z(3,count)),'b*');
       plot(z(count,1),z(count,2),'b*');
       %plot(z(1,count),z(2,count),'b*');
       hold on;

       %pause(0.1);

       count=count+1;
       axis equal;     

       if (currLen >= m_Length)
          z(count,1)= tan
          break;
       end

   end

 end 

此致

Mrinal

1 个答案:

答案 0 :(得分:2)

ii = index of the point where you want to get the tangent
x1 = left bound of your tangent
x2 = right bound of your tangent
xT = z(ii,1)  %argument where you want to get the tangent
yT = z(ii,2)  %corresponding y
mT = tan(z(ii,3))   % corresponding slope

我假设您只想要一个切线,然后将其绘制成这样。

plot( [x1,xT,x2] , [yT-mT*(xT-x1), yT, yT+mT*(x2-xT)] )

否则,只需使用循环来获取更多切线,使用ii作为迭代变量。

等式的字符串是

eq = strcat('y = ',num2str(mT),'*x + ',num2str(yT-mT*xT))