使用scipy.interpolate.splprep和splev固定一端,另一端自由立方样条 [英] One end clamped and other end free cubic spline using scipy.interpolate.splprep and splev

查看:93
本文介绍了使用scipy.interpolate.splprep和splev固定一端,另一端自由立方样条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

  x_old = [0.00000000e + 00,-5.96880765e-24,-8.04361605e-23,-2.11167774e-22,-2.30386081e-22,-7.86854147e-23,1.17548440e-22,1.93009272e-22,1.49906866e-22,9.66877465e-23,1.48495705e-23]y_old = [0.,0.03711505、0.03780602、0.02524459、0.01349815,0.00964215,0.00972842,0.0168793,0.02577024,0.02761626,0.02141961]z_old = [0.,0.29834302,0.59805918,0.89773519,1.19755092,1.49749325,1.79750314,2.09741402,2.39727031,2.69726787,2.99719479] 

我想找到这些点之间的 3-D 样条,以便初始坐标(0,0,0)保持固定(固定),另一端是 free .

我做到了:

来自scipy.interpolate的

 导入splprep,splev将numpy导入为np#找到结点tckp,u = splprep([x_old,y_old,z_old],s = 3.0,k = 3,nest = -1)#评估样条曲线.xnew,ynew,znew = splev(np.linspace(0,1,400),tckp) 

图:

来自mpl_toolkits.mplot3d的

 导入Axes3D导入matplotlib.pyplot作为pltax.plot(xnew,ynew,znew,label ='第一次迭代')plt.scatter(x_old,y_old,z_old,color ='blue',label ='given')ax.legend()plt.show() 

问题1 .在上图中,起始点肯定是不固定的.在数学上,我知道需要指定边界条件,以便获得所需的3-D样条曲线.如何在 scipy 中执行此操作?我可以在 splprep splev 中使用可指定的可选参数来实现此目的,还是需要一种全新的方式来实现?

问题2 :如果我希望将两端都钳住,那我该如何实现?

一些数学:'固定在初始点'表示在初始点的一阶导数为零,'在端点处自由'表示在该点的二阶导数为零.

解决方案

似乎您想要一个插值样条线,这意味着平滑参数s应该设置为0.

  tckp,u = splprep([x_old,y_old,z_old],s = 0.0,k = 3,nest = -1) 

可以使用

夹紧条件显然在该端附近起到了一定作用.

I have the following data:

x_old = [  0.00000000e+00,  -5.96880765e-24,  -8.04361605e-23,
    -2.11167774e-22,  -2.30386081e-22,  -7.86854147e-23,
     1.17548440e-22,   1.93009272e-22,   1.49906866e-22,
     9.66877465e-23,   1.48495705e-23]
y_old = [ 0.        ,  0.03711505,  0.03780602,  0.02524459,  0.01349815,
    0.00964215,  0.00972842,  0.0168793 ,  0.02577024,  0.02761626,
    0.02141961]


z_old = [ 0.        ,  0.29834302,  0.59805918,  0.89773519,  1.19755092,
    1.49749325,  1.79750314,  2.09741402,  2.39727031,  2.69726787,
    2.99719479]

I want to find the 3-D spline between these points so that the initial coordinate (0, 0, 0) remains fixed (clamped) and the other end is free.

I did:

 from scipy.interpolate import splprep, splev
 import numpy as np

 # find the knot points
 tckp,u = splprep([x_old,y_old,z_old],s=3.0,k=3,nest=-1)
 # evaluate spline.
 xnew,ynew,znew = splev(np.linspace(0,1,400),tckp)

Graph:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

ax.plot(xnew, ynew, znew, label='first iteration')
plt.scatter(x_old, y_old, z_old, color='blue', label='given')
ax.legend()

plt.show()

Question 1. In the above graph, the initial point is certainly not fixed. Mathematically, I know that I need to specify boundary conditions so that I get the 3-D spline I want. How can I do this in scipy?. Is there any optional arguments I can use in splprep and splev that I can specify to achieve this or do I need a completely new way to do this?

Question 2 : If I wanted both ends to be clamped then how do I achieve that?

Some Math : 'Clamped at the initial point' means that the first derivative at the initial point is zero and 'free at the terminal' point means that the second derivative there is zero.

解决方案

It seems you want an interpolating spline, which means the smoothing parameter s should be set to 0.

tckp, u = splprep([x_old,y_old,z_old], s=0.0, k=3, nest=-1)

A clamped spline (or a spline with other boundary conditions) can be made with make_interp_spline. Below, the parameters l, r are the boundary conditions at the left and right end. I prescribe zero first derivative at the left end, and zero second derivative at the right.

l, r = [(1, (0, 0, 0))], [(2, (0, 0, 0))]
clamped_spline = make_interp_spline(u, np.array([x_old, y_old, z_old]).T, bc_type=(l, r))
xnew2, ynew2, znew2 = clamped_spline(np.linspace(0,1,400)).T

Notice that I used the parameter u from the first spline, expecting it to perform better than a random linearly-spaced array. (u is computed based on the data points.)

Plotting both for comparison:

from mpl_toolkits.mplot3d import Axes3D as ax
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(xnew, ynew, znew, label='first iteration')
ax.plot(xnew2, ynew2, znew2, color='red', label='second iteration')
ax.scatter(x_old, y_old, z_old, color='blue', label='given')
ax.legend()
plt.show()

The clamping condition clearly had some effect near that end.

这篇关于使用scipy.interpolate.splprep和splev固定一端,另一端自由立方样条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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