find tangent vector at a point for discrete data points

前端 未结 2 822
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 17:09

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.29632         


        
相关标签:
2条回答
  • 2020-12-18 17:32

    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)
    

    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)
    
    0 讨论(0)
  • 2020-12-18 17:41

    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)
    
    0 讨论(0)
提交回复
热议问题