简体   繁体   English

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

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

I am trying to interpolate 3 values that I have measured, each one is related to a circular area of which I am considering the diameter (40mm, 50mm, and 100mm).我试图插入我测量的 3 个值,每个值都与我正在考虑的直径(40mm、50mm 和 100mm)的圆形区域相关。 I would like to interpolate them in order to find the values for 60mm, 70mm, and 80mm.我想对它们进行插值,以找到 60mm、70mm 和 80mm 的值。 The value related to the 100mm diameter must be the highest one, I don't want for example that the value at 80mm is higher than the one at 100mm.与100mm直径相关的值必须是最高的,例如我不希望80mm处的值高于100mm处的值。 The code I use is the following:我使用的代码如下:

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()

This is the plot I get for a set of values.这是我为一组值得到的图。
这是我为一组值得到的图。

Maybe I should interpolate with a logarithmic function?也许我应该用对数函数进行插值? How should I do that?我该怎么做?

Have a look at scipy.interpolate.PchipInterpolator .看看scipy.interpolate.PchipInterpolator This will preserve monotonicity between your datapoints.这将保持数据点之间的单调性。

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()

In general, it is not always possible to interpolate three points with a logarithm, you need to relax your problem to curve fitting here.通常,并不总是可以用对数插值三个点,您需要将问题放宽到此处的曲线拟合。 In its most flexible form, this can be done with scipy.optimize.curve_fit .在其最灵活的形式中,这可以通过scipy.optimize.curve_fit来完成。 Here is a post about how to do that. 是一篇关于如何做到这一点的帖子。

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

相关问题 如何在我的拉格朗日插值算法中获得拉格朗日多项式的 output 而不是其近似值 - How do I get output of Lagrange Polynomials instead of its approximation value on my lagrange interpolation algorithm 我总是得到最终的印刷品,甚至写下我想要的 class。 我该如何解决? - I always get the final print, even writing the class that I want. How do I fix this? 我如何计算拉格朗日多项式 Li Function 的导数? - How do i calculate the derivative of Lagrange polynomial Li Function? python如何正确完成拉格朗日插值 - How to complete Lagrange Interpolation correctly by python “扩展”不会做我想做的。 如何在 Snakemake 中为规则生成自定义输入列表? - 'expand' won't do what I want. How do I generate a custom list of inputs to a rule in Snakemake? 如何使用网格网格为地理坐标插入具有未知值的列? - How can I interpolate columns with unknown values for geocoordinates using meshgrid? 如何在python中插入地理配准数据? - How can I interpolate georeferenced data in python? 如何在python中插入数据? - How can I interpolate data in python? 如何在 Python heredocs 中插入变量? - How can I interpolate variables in Python heredocs? 如何在python数据框中插入值? - How can I interpolate values in a python dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM