首页 > 其他分享 >ex7.4

ex7.4

时间:2025-01-03 19:22:04浏览次数:1  
标签:plt import y0 np ex7.4 y3 ymin

import numpy as np
import pylab as plt
from scipy.interpolate import interp1d
from scipy.interpolate import lagrange

a = np.loadtxt('F:\python数学建模与算法\源程序\《Python数学建模算法与应用》程序和数据\第7章 插值与拟合\data7_4.txt')
x0 = a[0]; y0 = a[1]
x = np.linspace(0, 15, 151)

分段线性插值

yx1 = interp1d(x0, y0)
y1 = yx1(x)

lagrange插值

p2 = lagrange(x0, y0)
y2 = np.polyval(p2, x)
yx3 = interp1d(x0, y0, 'cubic')
y3 = yx3(x)
dx = np.diff(x); dy = np.diff(y3)
dyx = dy / dx; dyx0 = dyx[0]
xt = x[130:]; yt = y3[130:]
ymin = min(yt)
xmin = [xt[ind] for ind, v in enumerate(yt) if v == ymin]
print('x=0处斜率的数值解为:', dyx0)
print('xmin=', xmin); print('ymin=', ymin)
plt.rc('font', family='SimHei')
plt.rc('axes', unicode_minus=False)
plt.subplots_adjust(wspace=0.5)
plt.subplot(131); plt.plot(x, y1)
plt.title('分段线性插值')
plt.subplot(132); plt.plot(x, y2)
plt.title('拉格朗日插值')
plt.subplot(133); plt.plot(x, y3)
plt.title('三次样条插值')
plt.show()

标签:plt,import,y0,np,ex7.4,y3,ymin
From: https://www.cnblogs.com/zy05/p/18650798

相关文章