在离散数据点的点处找到切向量 [英] find tangent vector at a point for discrete data points

查看:230
本文介绍了在离散数据点的点处找到切向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量,在空间中至少有两个点,例如:

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])

我想在沿着曲线的离散点(例如曲线的起点和终点)找到向量的切线.我知道如何在Matlab中做到这一点,但我想在Python中做到这一点.这是Matlab中的代码:

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    

有什么想法吗?我试图通过多种方法使用numpy和scipy找到解决方案,例如

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.

推荐答案

好的,我找到了上面"pv"的一点修改的解决方案(请注意,splev仅适用于一维矢量) 我最初遇到的一个问题是"tck,u = scipy.interpolate.splprep(data)",它需要至少4点才能工作(Matlab需要2点).我当时使用了两点.增加数据点后,它可以按我的要求工作.

ok, I found the solution which is a little modification of "pv" above (note that splev works only for 1D vectors) One problem I was having originally with "tck, u= scipy.interpolate.splprep(data)" is that it requires a min of 4 points to work (Matlab works with two points). I was using two points. After increasing the data points, it works as i want.

这是完整性的解决方案:

Here is the solution for completeness:

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
data = np.array([[-1452.18133319 , 3285.44737438, -7075.49516676],
                 [-1452.20175668 , 3285.29632734, -7075.49110863],
                 [-1452.32645025 , 3284.37412457, -7075.46633213],
                 [-1452.38226151 , 3283.96135828, -7075.45524248]])

distance=np.array([0., 0.15247556, 1.0834, 1.50007])

data = data.T
tck,u = interpolate.splprep(data, u=distance, s=0)
yderv = interpolate.splev(u,tck,der=1)

和切线是(如果使用相同的数据,则匹配Matlab结果):

and the tangents are (which matches the Matlab results if the same data is used):

(-0.13394599723751408, -0.99063114953803189, 0.026614957159932656)
(-0.13394598523149195, -0.99063115868512985, 0.026614950816003666)
(-0.13394595055068903, -0.99063117647357712, 0.026614941718878599)
(-0.13394595652952143, -0.9906311632471152, 0.026614954146007865)

这篇关于在离散数据点的点处找到切向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆