例7.3
import numpy as np
import pylab as plt
from scipy. interpolate import lagrange
yx=lambda x: 1/(1+x**2)
def fun(n):
x=np.linspace(-5,5,n+1)
p=lagrange(x,yx(x))
return p
x0=np.linspace(-5,5,100)
plt.rc('text',usetex=True)
plt.rc('font',size=15);
plt.style.use('default')
N=[6,8,10]
s=['--*b','-.','-p']
for k in range(3):
p=fun(N[k]);plt.plot(x0,np.polyval(p,x0),s[k])
plt.plot(x0,yx(x0));
plt.legend(['$n=6$','$n=8$','$n=10$','$1/(1+x^2)$'])
plt.show()
例7.4
import numpy as np
from scipy.interpolate import interp1d
from scipy.interpolate import lagrange
import pylab as plt
a = np.loadtxt('C:/Users/qazws/OneDrive/python代码/python建模作业/data7_4.txt')
x0=a[0] ;y0=a[1]
x = np.linspace(0, 15, 151)
yx1=interp1d(x0,y0)
y1=yx1(x)
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()
习题7.3
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata
from scipy.optimize import least_squares, curve_fit
from scipy.integrate import quad
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
t0 = np.linspace(700, 780, 5)
v0 = np.array([0.0977, 0.1218, 0.1406, 0.1551, 0.1664])
f1 = interp1d(t0, v0)
f2 = interp1d(t0, v0, 'cubic')
tp = np.array([750, 770])
vp1 = f1(tp)
vp2 = f2(tp)
print("线性插值的预测结果为:", vp1)
print("三次样条插值的预测结果为:", vp2)
t = np.linspace(700, 780, 200)
v1 = f1(t)
v2 = f2(t)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t, v1, '--', label='线性插值')
ax.plot(t, v2, '-', label='三次样条插值', color='#ff9900')
ax.scatter(t0, v0, label='数据', zorder=10, marker='.', s=15, color='r')
ax.legend(fontsize=8)
fig.show()
print("3035")
习题7.4
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata
from scipy.optimize import least_squares, curve_fit
from scipy.integrate import quad
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
np.random.seed(114514)
x0 = np.random.uniform(-3, 3, 50)
y0 = np.random.uniform(-4, 4, 50)
f = lambda x, y: (x**2 - 2*x)*np.exp(-x**2 - y**2 - x*y)
z0 = f(x0, y0)
xy0 = np.vstack([x0, y0]).T
x = np.linspace(x0.min(), x0.max(), 300)
y = np.linspace(y0.min(), y0.max(), 300)
X, Y = np.meshgrid(x, y)
z = griddata(xy0, z0, (X,Y), 'cubic')
zl = griddata(xy0, z0, (X,Y), 'linear')
z[np.isnan(z)] = zl[np.isnan(z)]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, z, cmap='hot')
fig.show()
print("3035")
标签:plt,y0,第七章,rcParams,np,import,习题,例题,x0
From: https://www.cnblogs.com/miling/p/18552337