【问题标题】:How to draw tangent line at specified points in a curve in Matlab如何在Matlab中的曲线中的指定点处绘制切线
【发布时间】:2013-09-18 12:42:14
【问题描述】:

我有一组点 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 

问候,

矿泉水

【问题讨论】:

    标签: matlab matlab-figure curve


    【解决方案1】:
    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)) 
    

    【讨论】:

    • 谢谢,但我无法理解如何在我的代码中使用上述等式。这是我的代码,因为我正在使用各种功能,因此我无法上传整个代码,但我正在使用的功能如下。希望你能指导。我正在上面的问题中上传我的代码。
    • 您上面的代码与您的问题无关。我刚刚使用了你的矩阵z。您必须自己定义iix1x2。试着理解我发布的内容,你只需要把它放在你的代码下。我实际上没有得到你上面的空 if 条件。
    • 好的。我会按照你的建议做,抱歉我忘了删除如果有条件,我现在就做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多