繁体   English   中英

使用拉格朗日插值不能按我想要的方式工作。 如何使用对数函数进行插值?

[英]Interpolation using Lagrange not working how I want. How can I interpolate with a logarithmic function?

我试图插入我测量的 3 个值,每个值都与我正在考虑的直径(40mm、50mm 和 100mm)的圆形区域相关。 我想对它们进行插值,以找到 60mm、70mm 和 80mm 的值。 与100mm直径相关的值必须是最高的,例如我不希望80mm处的值高于100mm处的值。 我使用的代码如下:

import matplotlib.pyplot as plt
import scipy as sp
from scipy import interpolate
from matplotlib.ticker import (MaxNLocator, MultipleLocator)

# Estamte the Lagrange Interpolation for x squared based on three values
y = sp.interpolate.lagrange([40, 50, 100], [1.00132949, 1.01038226, 1.01108727])

print(y(60), y(70), y(80))


fig,axes=plt.subplots(1,1)
axes.xaxis.set_major_locator(MaxNLocator(8))
axes.yaxis.set_major_locator(MaxNLocator(8))
axes.plot([40., 50., 60., 70., 80., 90., 100.], y([40., 50., 60., 70., 80., 90., 100.]), 
linestyle='--', marker='o')
plt.xlabel(r"Applicator [mm]")
plt.ylabel(r"Output Factor")
plt.xlim(30,110)
plt.ylim(1, 1.03)
plt.title( r'Interpolazione $12\, MeV$ Applicatore $45°$')
axes.tick_params(direction='in', which='minor')
axes.tick_params(direction='in', length=4, width=1, grid_alpha=0.5)
#axes.xaxis.set_minor_locator(MultipleLocator())
axes.yaxis.set_minor_locator(MultipleLocator(0.001))
plt.legend()
plt.grid()
plt.show()

这是我为一组值得到的图。
这是我为一组值得到的图。

也许我应该用对数函数进行插值? 我该怎么做?

看看scipy.interpolate.PchipInterpolator 这将保持数据点之间的单调性。

import matplotlib.pyplot as plt
import scipy as sp
import numpy as np

xs = np.linspace(40,100,100)
ys = sp.interpolate.pchip_interpolate([40, 50, 100], [1.00132949, 1.01038226, 1.01108727], x=xs)
plt.plot(xs, ys, linestyle='--')
plt.plot([40, 50, 100], [1.00132949, 1.01038226, 1.01108727], linestyle="", marker="o")
plt.show()

通常,并不总是可以用对数插值三个点,您需要将问题放宽到此处的曲线拟合。 在其最灵活的形式中,这可以通过scipy.optimize.curve_fit来完成。 是一篇关于如何做到这一点的帖子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM