Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - find tangent vector at a point for discrete data points

I have a vector with a min of two points in space, e.g:

A = np.array([-1452.18133319  3285.44737438 -7075.49516676])
B = np.array([-1452.20175668  3285.29632734 -7075.49110863])

I want to find the tangent of the vector at a discrete points along the curve, g.g the beginning and end of the curve. I know how to do it in Matlab but I want to do it in Python. This is the code in Matlab:

A = [-1452.18133319  3285.44737438 -7075.49516676];
B = [-1452.20175668  3285.29632734 -7075.49110863];
points = [A; B];
distance = [0.; 0.1667];
pp = interp1(distance, points,'pchip','pp');
[breaks,coefs,l,k,d] = unmkpp(pp);
dpp = mkpp(breaks,repmat(k-1:-1:1,d*l,1).*coefs(:,1:k-1),d);
ntangent=zeros(length(distance),3);
for j=1:length(distance)
    ntangent(j,:) = ppval(dpp, distance(j));
end

%The solution would be at beginning and end:
%ntangent =
%   -0.1225   -0.9061    0.0243
%   -0.1225   -0.9061    0.0243    

Any ideas? I tried to find the solution using numpy and scipy using multiple methods, e.g.

tck, u= scipy.interpolate.splprep(data)

but none of the methods seem satisfy what I want.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Give der=1 to splev to get the derivative of the spline:

from scipy import interpolate
import numpy as np
t=np.linspace(0,1,200)
x=np.cos(5*t)
y=np.sin(7*t)
tck, u = interpolate.splprep([x,y])

ti = np.linspace(0, 1, 200)
dxdt, dydt = interpolate.splev(ti,tck,der=1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...