- x = np.linspace(-10,10,100)
- y = np.sin(x) + np.cos(x)
- scipy.interpolate.lagrange(x,y)
一维插值
通过定义"kind"的类型来进行不同类别的插值
三次插值 kind = "cubic"
二次插值 kind = "quadratic"
一次插值 kind = "inear"
零阶插值 kind = "zero"
nearest 插值法
- from scipy import interpolate
-
- x = np.linsapce(-1,1,20)
- y = np.sin(x)
-
- f = interpolate.interp1d(x,y,kind="?")
- def z(x,y):
- return x+y
-
- x,y = np.mgrid[-1:1:20j,-1:1:20j]
-
- fun = interpolate.interp2d(x,y,z(x,y),kind="cubic")
- xnew = np.linspace(-1,1,200)
- ynew = np.linspace(-1,1,200)
- fnew = fun(xnew,ynew)
- import matplotlib.pyplot as plt
- import scipy as sp
- import numpy as np
- from scipy.interpolate import interp1d
- from scipy.interpolate import CubicSpline
- import xlsxwriter as xls
- import random
- random.seed(666)
导入相关数据
- water = [1800,1900,2100,2200,2300,2400,2500,2600,2650,
- 2700,2720]
-
- water2 = sorted([2650,2600,2500,2300,2200,2000,1850,
- 1820,1800,1755,1500,1000,900],reverse=False)
-
- sand = [32,60,75,85,90,98,100,102,108,
- 112,115,116,118,120,118,105,80,
- 60,50,30,26,20,8,5][:len(water)]
-
- sand2 = sorted([116,118,120,118,105,80,60,50,30,26,20,8,5],reverse=False)
插值
- cubic_interp = interp1d(water,sand,kind='cubic')
- cs1 = CubicSpline(water,sand,bc_type="natural")
- cs2 = CubicSpline(water2,sand2,bc_type="natural")
-
- #两个数据点之间
- #f0 = a0 + b0*(x-x0) + c0*(x-x0)**2 + d0*(x-x0)**3
- #.
- #.
- #.
- #fn = an + bn*(x-x0) + cn*(x-x0)**2 + dn*(x-x0)**3
写入文件
- workbook1 = xls.Workbook("上升期数据.xlsx")
- workbook2 = xls.Workbook("下降期数据.xlsx")
-
- worksheet1 = workbook1.add_worksheet("Sheet1")
- worksheet2 = workbook2.add_worksheet("Sheet2")
-
- #两个数据点之间
- #f0 = a0 + b0*(x-x0) + c0*(x-x0)**2 + d0*(x-x0)**3
- #.
- #.
- #.
- #fn = an + bn*(x-x0) + cn*(x-x0)**2 + dn*(x-x0)**3
- headings = ["a0","b0","c0","d0"]
- worksheet1.write_row("A1",headings)
- worksheet2.write_row("A1",headings)
-
- a1 = [cs1.c.item(3,i) for i in range(len(water)-1)]
- b1 = [cs1.c.item(2,i) for i in range(len(water)-1)]
- c1 = [cs1.c.item(1,i) for i in range(len(water)-1)]
- d1 = [cs1.c.item(0,i) for i in range(len(water)-1)]
- a2 = [cs2.c.item(3,i) for i in range(len(water2)-1)]
- b2 = [cs2.c.item(2,i) for i in range(len(water2)-1)]
- c2 = [cs2.c.item(1,i) for i in range(len(water2)-1)]
- d2 = [cs2.c.item(0,i) for i in range(len(water2)-1)]
-
- worksheet1.write_column("A2",a1)
- worksheet1.write_column("B2",b1)
- worksheet1.write_column("C2",c1)
- worksheet1.write_column("D2",d1)
-
- worksheet2.write_column("A2",a2)
- worksheet2.write_column("B2",b2)
- worksheet2.write_column("C2",c2)
- worksheet2.write_column("D2",d2)
-
- workbook1.close()
- workbook2.close()