简体   繁体   English

如何在 gnuplot 中绘制到数据点的切线?

[英]How to draw tangent line to a data point in gnuplot?

I am wondering how to draw a tangent line to a data point from csv data file in gnuplot.我想知道如何在 gnuplot 中从 csv 数据文件绘制一条到数据点的切线。 Thanks for help感谢帮助

I tried some sort of fitting but it does not work.我尝试了某种配件,但它不起作用。

How do you specify your data point for the tangent?您如何指定切线的数据点? By x-value?按 x 值? Are there unique y-values for all x-values?所有 x 值是否都有唯一的 y 值? You give very little information and no example data, so I assume something.您提供的信息很少,也没有示例数据,所以我做了一些假设。

Get the current and previous x-values into the variables x1 and x0 , respectively.将当前和先前的 x 值分别放入变量x1x0中。 If your x-value for the tangent xt is between x0 and x1 calculate the slope m and the y-axis-offset c .如果切线xt的 x 值介于x0x1之间,则计算斜率m和 y 轴偏移c Your tangent line then is y = m*x + c .你的切线是y = m*x + c

If you want to plot a tangent to a function you could use this approach .如果你想 plot 与 function 相切,你可以使用这种方法

Script:脚本:

### plot a tangent to a dataset
reset session

# create some test data
$RawData <<EOD
 0   0
 1   1
 2   4
 3   2
 4   1
 5   2
EOD
set table $Data
    plot $RawData u 1:2 smooth mcsplines
unset table

xt = 2.1

getTangent(colX,colY) = (x0=x1, y0=y1, x1=column(colX), y1=column(colY), \
                    sgn(xt-x0)!=sgn(xt-x1) ? (m=(y1-y0)/(x1-x0), c=y0-x0*m) : 0)

plot x1=y1=NaN $Data u 1:(getTangent(1,2),$2) w l lc "red" ti "Data", \
        '+' u 1:(m*$1+c) w l lc "blue" ti sprintf("Tangent at x=%g",xt)
### end of script

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM