简体   繁体   English

python如何正确完成拉格朗日插值

[英]How to complete Lagrange Interpolation correctly by python

I was asked to use Lagrange Interpolation to draw a line that pass through several dots on a graph (scipy.interpolate.lagrange banned).我被要求使用拉格朗日插值法绘制一条穿过图形上几个点的线(禁止使用 scipy.interpolate.lagrange)。

But it doesn't seem that my code can give the correct result.但似乎我的代码不能给出正确的结果。 To be frank, I don't even know which part of it is wrong.说实话,我什至不知道哪一部分是错的。

And if I was asked to find the value of a specific point on the line, how should I adjust the code to do so?如果我被要求找到线上特定点的值,我应该如何调整代码来做到这一点?

The first image is the expected output. The second one is the output from my code.第一张图片是预期的 output。第二张图片是我的代码中的 output。

Can someone please tell me how to correct it?有人可以告诉我如何纠正吗?

预期产出

我的源代码输出

Source code源代码

import numpy as np
import matplotlib.pyplot as plt

m = 4
X = np.array([0, 1, 1.5, 2, 3])
Y = np.array([2, 8, 12, 10, 20])

def p(x): 
    px = 0;
    for j in range(m+1):
        Lmj = 1
        for k in range(m+1):
            if k != j:
                Lmj *= (x - X[k])/(X[j] - X[k])
            px += Y[j] * Lmj;
        return px

plt.scatter(X, Y)
Xinterp = np.linspace(min(X), max(X), 100)
plt.plot(Xinterp, p(Xinterp))
plt.show()

Also, is that plt.show() line necessary?另外, plt.show() 行是否必要? From my lecturer's notes, the code don't have this line and still can display a graph.从我讲师的笔记来看,代码没有这一行,但仍然可以显示图形。 But in my copied version of the source code, I need to add this line to show the graph.但是在我复制的源代码版本中,我需要添加这一行来显示图表。

Thank you very much for noticing this problem.非常感谢您注意到这个问题。

  1. Lagrange Interpolation produces a polynomial from a given set of points.拉格朗日插值从一组给定的点生成多项式。
  2. You can then use this polynomial to find the value of a specific point on the line.然后,您可以使用此多项式来查找线上特定点的值。
  3. plt.show() is not necessary if you are running the code inside a Jupyter notebook (or similar), but is necessary if running via the terminal.如果您在 Jupyter notebook(或类似笔记本)中运行代码,则 plt.show() 不是必需的,但如果通过终端运行则需要 plt.show() 。

Here is some python code that generates a numpy.polynomial from your set of points:这是一些 python 代码,它从您的点集生成numpy.polynomial

import numpy as np
from numpy.polynomial import Polynomial
import matplotlib.pyplot as plt

X = np.array([0, 1, 1.5, 2, 3])
Y = np.array([2, 8, 12, 10, 20])

n = len(X)
poly = Polynomial(np.zeros(n))

for j in range(n):
    k = [k for k in range(n) if k != j]
    roots = -1 * X[k]

    sub_poly = Polynomial.fromroots(X[k])
    scale = Y[j] / np.prod(X[j] - X[k])
    sub_poly.coef *= scale

    poly.coef += sub_poly.coef

You can then plot it via:然后您可以通过以下方式拨打 plot:

plt.scatter(X, Y)
Xinterp = np.linspace(min(X), max(X), 100)
plt.plot(Xinterp, poly(Xinterp))
plt.show()

在此处输入图像描述

Or to find a specific value:或查找特定值:

print(poly(0.5))

Please see here for more information: https://learn.64bitdragon.com/articles/computer-science/numerical-analysis/lagrange-interpolation更多信息请看这里: https://learn.64bitdragon.com/articles/computer-science/numerical-analysis/lagrange-interpolation

Full disclosure: I'm the author of this article.完全披露:我是这篇文章的作者。

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

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