日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python开三次方_python实现三次样条插值

發(fā)布時(shí)間:2024/9/19 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python开三次方_python实现三次样条插值 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文實(shí)例為大家分享了python實(shí)現(xiàn)三次樣條插值的具體代碼,供大家參考,具體內(nèi)容如下

函數(shù):

算法分析

三次樣條插值。就是在分段插值的一種情況。

要求:

在每個(gè)分段區(qū)間上是三次多項(xiàng)式(這就是三次樣條中的三次的來(lái)源)

在整個(gè)區(qū)間(開(kāi)區(qū)間)上二階導(dǎo)數(shù)連續(xù)(當(dāng)然啦,這里主要是強(qiáng)調(diào)在節(jié)點(diǎn)上的連續(xù))

加上邊界條件。邊界條件只需要給出兩個(gè)方程。構(gòu)建一個(gè)方程組,就可以解出所有的參數(shù)。

這里話,根據(jù)第一類(lèi)樣條作為邊界。(就是知道兩端節(jié)點(diǎn)的導(dǎo)數(shù)數(shù)值,然后來(lái)做三次樣條插值)

但是這里也分為兩種情況,分別是這個(gè)數(shù)值是隨便給的一個(gè)數(shù),還是說(shuō)根據(jù)函數(shù)的在對(duì)應(yīng)點(diǎn)上數(shù)值給出。

情況一:兩邊導(dǎo)數(shù)數(shù)值給出

這里假設(shè)數(shù)值均為1。即 f′(x0)=f′(xn)=f′(xn)=1的情況。

情況一圖像

情況一代碼

import numpy as np

from sympy import *

import matplotlib.pyplot as plt

def f(x):

return 1 / (1 + x ** 2)

def cal(begin, end, i):

by = f(begin)

ey = f(end)

I = Ms[i] * ((end - n) ** 3) / 6 + Ms[i + 1] * ((n - begin) ** 3) / 6 + (by - Ms[i] / 6) * (end - n) + (

ey - Ms[i + 1] / 6) * (n - begin)

return I

def ff(x): # f[x0, x1, ..., xk]

ans = 0

for i in range(len(x)):

temp = 1

for j in range(len(x)):

if i != j:

temp *= (x[i] - x[j])

ans += f(x[i]) / temp

return ans

def calM():

lam = [1] + [1 / 2] * 9

miu = [1 / 2] * 9 + [1]

# Y = 1 / (1 + n ** 2)

# df = diff(Y, n)

x = np.array(range(11)) - 5

# ds = [6 * (ff(x[0:2]) - df.subs(n, x[0]))]

ds = [6 * (ff(x[0:2]) - 1)]

for i in range(9):

ds.append(6 * ff(x[i: i + 3]))

# ds.append(6 * (df.subs(n, x[10]) - ff(x[-2:])))

ds.append(6 * (1 - ff(x[-2:])))

Mat = np.eye(11, 11) * 2

for i in range(11):

if i == 0:

Mat[i][1] = lam[i]

elif i == 10:

Mat[i][9] = miu[i - 1]

else:

Mat[i][i - 1] = miu[i - 1]

Mat[i][i + 1] = lam[i]

ds = np.mat(ds)

Mat = np.mat(Mat)

Ms = ds * Mat.I

return Ms.tolist()[0]

def calnf(x):

nf = []

for i in range(len(x) - 1):

nf.append(cal(x[i], x[i + 1], i))

return nf

def calf(f, x):

y = []

for i in x:

y.append(f.subs(n, i))

return y

def nfSub(x, nf):

tempx = np.array(range(11)) - 5

dx = []

for i in range(10):

labelx = []

for j in range(len(x)):

if x[j] >= tempx[i] and x[j] < tempx[i + 1]:

labelx.append(x[j])

elif i == 9 and x[j] >= tempx[i] and x[j] <= tempx[i + 1]:

labelx.append(x[j])

dx = dx + calf(nf[i], labelx)

return np.array(dx)

def draw(nf):

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False

x = np.linspace(-5, 5, 101)

y = f(x)

Ly = nfSub(x, nf)

plt.plot(x, y, label='原函數(shù)')

plt.plot(x, Ly, label='三次樣條插值函數(shù)')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.savefig('1.png')

plt.show()

def lossCal(nf):

x = np.linspace(-5, 5, 101)

y = f(x)

Ly = nfSub(x, nf)

Ly = np.array(Ly)

temp = Ly - y

temp = abs(temp)

print(temp.mean())

if __name__ == '__main__':

x = np.array(range(11)) - 5

y = f(x)

n, m = symbols('n m')

init_printing(use_unicode=True)

Ms = calM()

nf = calnf(x)

draw(nf)

lossCal(nf)

情況二:兩邊導(dǎo)數(shù)數(shù)值由函數(shù)本身算出

這里假設(shè)數(shù)值均為1。即 f′(xi)=S′(xi)(i=0,n)f′(xi)=S′(xi)(i=0,n)的情況。

情況二圖像

情況二代碼

import numpy as np

from sympy import *

import matplotlib.pyplot as plt

def f(x):

return 1 / (1 + x ** 2)

def cal(begin, end, i):

by = f(begin)

ey = f(end)

I = Ms[i] * ((end - n) ** 3) / 6 + Ms[i + 1] * ((n - begin) ** 3) / 6 + (by - Ms[i] / 6) * (end - n) + (

ey - Ms[i + 1] / 6) * (n - begin)

return I

def ff(x): # f[x0, x1, ..., xk]

ans = 0

for i in range(len(x)):

temp = 1

for j in range(len(x)):

if i != j:

temp *= (x[i] - x[j])

ans += f(x[i]) / temp

return ans

def calM():

lam = [1] + [1 / 2] * 9

miu = [1 / 2] * 9 + [1]

Y = 1 / (1 + n ** 2)

df = diff(Y, n)

x = np.array(range(11)) - 5

ds = [6 * (ff(x[0:2]) - df.subs(n, x[0]))]

# ds = [6 * (ff(x[0:2]) - 1)]

for i in range(9):

ds.append(6 * ff(x[i: i + 3]))

ds.append(6 * (df.subs(n, x[10]) - ff(x[-2:])))

# ds.append(6 * (1 - ff(x[-2:])))

Mat = np.eye(11, 11) * 2

for i in range(11):

if i == 0:

Mat[i][1] = lam[i]

elif i == 10:

Mat[i][9] = miu[i - 1]

else:

Mat[i][i - 1] = miu[i - 1]

Mat[i][i + 1] = lam[i]

ds = np.mat(ds)

Mat = np.mat(Mat)

Ms = ds * Mat.I

return Ms.tolist()[0]

def calnf(x):

nf = []

for i in range(len(x) - 1):

nf.append(cal(x[i], x[i + 1], i))

return nf

def calf(f, x):

y = []

for i in x:

y.append(f.subs(n, i))

return y

def nfSub(x, nf):

tempx = np.array(range(11)) - 5

dx = []

for i in range(10):

labelx = []

for j in range(len(x)):

if x[j] >= tempx[i] and x[j] < tempx[i + 1]:

labelx.append(x[j])

elif i == 9 and x[j] >= tempx[i] and x[j] <= tempx[i + 1]:

labelx.append(x[j])

dx = dx + calf(nf[i], labelx)

return np.array(dx)

def draw(nf):

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False

x = np.linspace(-5, 5, 101)

y = f(x)

Ly = nfSub(x, nf)

plt.plot(x, y, label='原函數(shù)')

plt.plot(x, Ly, label='三次樣條插值函數(shù)')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.savefig('1.png')

plt.show()

def lossCal(nf):

x = np.linspace(-5, 5, 101)

y = f(x)

Ly = nfSub(x, nf)

Ly = np.array(Ly)

temp = Ly - y

temp = abs(temp)

print(temp.mean())

if __name__ == '__main__':

x = np.array(range(11)) - 5

y = f(x)

n, m = symbols('n m')

init_printing(use_unicode=True)

Ms = calM()

nf = calnf(x)

draw(nf)

lossCal(nf)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的python开三次方_python实现三次样条插值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。