python做样条差值的列子

#-*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from  mpl_toolkits.mplot3d  import Axes3D
from scipy import interpolate
import matplotlib.gridspec as gridspec

num=20
k=2
b=3
noise=np.random.rand(num)*3
x=np.arange(num)
y=np.sin(x)

c_linspl=interpolate.splrep(x,y,k=1)
x_linspl=np.arange(0,num,0.1)
y_linspl=interpolate.splev(x_linspl,c_linspl,der=0)

c_quadratic_spl=interpolate.splrep(x,y,k=2)
x_quadratic_spl=np.arange(0,num,0.1)
y_quadratic_spl=interpolate.splev(x_quadratic_spl,c_quadratic_spl,der=0)

c_cubic_spl=interpolate.splrep(x,y,k=3)
x_cubic_spl=np.arange(0,num,0.1)
y_cubic_spl=interpolate.splev(x_quadratic_spl,c_quadratic_spl,der=0)

gs=gridspec.GridSpec(2,2)
fig=plt.figure()
plt.title(u"Matplotlib 样条差值比较",fontsize=20)
p1=fig.add_subplot(gs[0,0])
p2=fig.add_subplot(gs[0,1])
p3=fig.add_subplot(gs[1,0])
p4=fig.add_subplot(gs[1,1])

p1.plot(x,y)
p2.plot(x_linspl,y_linspl)
p3.plot(x_quadratic_spl,y_quadratic_spl)
p4.plot(x_cubic_spl,y_cubic_spl)

p1.set_title(u"原图")
p2.set_title(u"一次样条差值(直线)")
p3.set_title(u"二次样条差值")
p4.set_title(u"三次样条差值")


plt.show()

Python中,可以使用scipy库中的interp1d函数来进行样条插值。interp1d函数的原型是:interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False) 。 下面是一个使用interp1d函数进行样条插值的例子: ```python import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import interp1d x = np.array([0,1,2,3,4,5,6,7]) y = np.array([1.11,2.22,3.34,4.13,3,2.25,1,1.5]) xx = np.linspace(x.min(), x.max(), 100) plt.scatter(x, y) f = interp1d(x, y, kind='cubic') ynew = f(xx) plt.plot(xx, ynew, 'b') plt.show() ``` 上述代码中,我们首先定义了一组已知的点(x, y),然后使用interp1d函数创建了一个样条插值函数f。接着,我们生成了更多的点(xx),并使用插值函数f计算了相应的函数值ynew。最后,将原始数据点(x, y)和插值曲线(xx, ynew)绘制在图像上 。 除了interp1d函数外,scipy.interpolate包中还有其他模块可以实现对已知点进行插值,例如interp1d。通过使用这些插值函数,我们可以计算其他x值对应的y值,这正是插值的主要用途 [3]。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python样条插值(二)](https://blog.csdn.net/qingchedeyongqi/article/details/116766927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [python数学建模(样条插值)](https://blog.csdn.net/m0_59309242/article/details/119493352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值