The tangent to a smooth curve at the point P is the parametric line P + tV, where V is the derivative of the curve with respect to the “parameter”. But here the parameter is just an index of the array, and numerical differentiation is a difficult task, therefore, to approximate the tangent, I would use the (weighted) least squares approximation .
In other words, select three or five points of the curve around your point of interest P (ie, P [i-2], P [i-1], P [i], P [i + 1] and P [i + 2 ], if P == P [i]) and approximate their line, in the sense of least squares. The more weight you assign to the midpoint P, the closer the line will be to P; on the other hand, the more weight you assign to extreme points, the more “tangent” the straight line will be, i.e. the more beautiful it will bring you closer to the curve in the neighborhood of point P.For example, regarding the following points:
x = [-1, 0, 1]
y = [ 0, 1, 0]
for which the tangent is not defined (as in Anders Abel's answer), this approach should give a horizontal line close to the point (0,1).
source
share