给定一组坐标,实现三次hemite差值曲线绘制的具体python代码,坐标自拟
时间: 2023-06-04 10:05:05 浏览: 117
好的,关于您的问题,以下是代码实现:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import CubicHermiteSpline
# 自定义坐标点,可以更改
x = np.array([0, 1, 2, 3, 4])
y = np.array([1, 2, 0, 3, 2])
# 三次Hermite插值
dx = np.gradient(x)
dy = np.gradient(y)
f = CubicHermiteSpline(x, y, dy/dx)
# 生成画图用坐标
X = np.linspace(0, 4, 200)
Y = f(X)
# 绘图
plt.plot(x, y, 'o', label='data')
plt.plot(X, Y, label='Cubic Hermite spline')
plt.legend(loc='best')
plt.show()
```
以上是给定一组自定义的坐标,实现三次Hermite差值曲线绘制的Python代码。希望对您有所帮助。
相关问题
Python利用给定的一组坐标,实现三次Hermite差值曲线绘制
三次Hermite差值曲线是一种光滑的曲线,可以通过给定的点和切线来构造。实现三次Hermite差值曲线的步骤如下:
1. 定义给定的点和切线。假设有n个点,每个点有一个坐标(x,y)和两个切线的斜率(m1,m2)。
2. 计算每个点之间的距离,并计算每个点之间的参数t(t=0表示第一个点,t=1表示最后一个点,t在0和1之间表示曲线上的中间点)。
3. 对于每个中间点,计算其曲线上的坐标。使用公式:
P(t) = (2t^3 - 3t^2 + 1) * P0 + (t^3 - 2t^2 + t) * m0 + (-2t^3 + 3t^2) * P1 + (t^3 - t^2) * m1
其中,P0和P1是相邻的两个点,m0和m1是这两个点的切线斜率。
4. 将计算出来的曲线上的点连接起来,即可绘制出三次Hermite差值曲线。
下面是一个Python实现的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义给定的点和切线
points = np.array([(0, 2, 1, -1), (1, 3, 2, 1), (2, 0, 1, -1), (3, 1, -1, 1)])
n = len(points)
# 计算每个点之间的距离
distances = np.zeros(n - 1)
for i in range(n - 1):
distances[i] = np.sqrt((points[i + 1][0] - points[i][0]) ** 2 + (points[i + 1][1] - points[i][1]) ** 2)
# 计算每个点之间的参数t
t = np.zeros(n)
t[0] = 0
for i in range(1, n):
t[i] = t[i - 1] + distances[i - 1] / np.sum(distances)
# 计算曲线上的点
curve_points = []
for i in range(n - 1):
P0 = np.array([points[i][0], points[i][1]])
P1 = np.array([points[i + 1][0], points[i + 1][1]])
m0 = np.array([points[i][2], points[i][3]]) * distances[i] / np.sum(distances)
m1 = np.array([points[i + 1][2], points[i + 1][3]]) * distances[i] / np.sum(distances)
for j in range(10):
t_ = t[i] + j * (t[i + 1] - t[i]) / 10
if j == 0:
curve_points.append(P0)
elif j == 9:
curve_points.append(P1)
else:
p = (2 * t_ ** 3 - 3 * t_ ** 2 + 1) * P0 + (t_ ** 3 - 2 * t_ ** 2 + t_) * m0 + (-2 * t_ ** 3 + 3 * t_ ** 2) * P1 + (t_ ** 3 - t_ ** 2) * m1
curve_points.append(p)
# 绘制曲线
curve_points = np.array(curve_points)
plt.plot(curve_points[:, 0], curve_points[:, 1], 'b-')
plt.plot(points[:, 0], points[:, 1], 'ro')
plt.show()
```
上述代码中,我们假设给定的点和切线都是以(x,y,m1,m2)的形式保存在一个numpy数组中,然后计算每个点之间的距离和参数t,并使用公式计算曲线上的点。最后,将曲线上的点连接起来并与给定的点一起绘制出来。
Python利用给定的一组坐标,实现三次Hermite差值曲线绘制的函数
下面是利用Python实现三次Hermite差值曲线绘制的函数:
```python
import numpy as np
import matplotlib.pyplot as plt
def hermite_cubic_interpolation(x, y, m):
"""
:param x: 数据点的横坐标,array_like
:param y: 数据点的纵坐标,array_like
:param m: 数据点处的导数,array_like
:return: 返回插值函数 f(x)
"""
n = len(x)
h = np.diff(x)
alpha = np.zeros(n)
for i in range(1, n-1):
alpha[i] = 3/h[i]*(y[i+1]-y[i]) - 3/h[i-1]*(y[i]-y[i-1])
l = np.zeros(n)
mu = np.zeros(n)
z = np.zeros(n)
l[0] = 1
mu[0] = 0
z[0] = 0
for i in range(1, n-1):
l[i] = 2*(x[i+1]-x[i-1])-h[i-1]*mu[i-1]
mu[i] = h[i]/l[i]
z[i] = (alpha[i]-h[i-1]*z[i-1])/l[i]
l[n-1] = 1
z[n-1] = 0
c = np.zeros(n)
b = np.zeros(n-1)
d = np.zeros(n-1)
for j in range(n-2, -1, -1):
c[j] = z[j]-mu[j]*c[j+1]
b[j] = (y[j+1]-y[j])/h[j] - h[j]*(c[j+1]+2*c[j])/3
d[j] = (c[j+1]-c[j])/(3*h[j])
def f(t):
"""
:param t: 自变量,scalar or array_like
:return: 因变量 f(t),scalar or array_like
"""
idx = np.searchsorted(x, t)
if idx == 0:
return y[0] + m[0]*(t-x[0])
elif idx == n:
return y[-1] + m[-1]*(t-x[-1])
else:
i = idx - 1
return y[i] + m[i]*(t-x[i]) + b[i]*(t-x[i])**2 + c[i]*(t-x[i])**3 + d[i]*(t-x[i])**3*(t-x[i+1])
return f
# 示例
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 3, 2, 1, 2])
m = np.array([1, 2, 3, 4, 5, 6])
f = hermite_cubic_interpolation(x, y, m)
t = np.linspace(0, 5, 1000)
plt.plot(x, y, 'o', t, f(t), '-')
plt.show()
```
其中,`x` 是数据点的横坐标,`y` 是数据点的纵坐标,`m` 是数据点处的导数。函数返回一个函数对象 `f`,通过调用 `f(t)` 可以得到自变量为 `t` 时的因变量值。示例中的数据点和导数都是随意构造的,可以根据具体需求进行修改。