拉格朗日插值

发布于 2024-12-16 00:22:34 字数 729 浏览 1 评论 0原文

我检查了有关拉格朗日插值的答案,但找不到适合我的问题的答案。我正在尝试使用 matlab 对曲面进行拉格朗日插值。假设我有 ax 和 y 向量并且 f=f(x,y)。我想插值这个 f 函数。我认为,我所做的在数学上是正确的:

function q = laginterp(x,y,f,ff)

n = length(x);
m = length(y);
v = zeros(size(ff));
for k = 1:n
    for l = 1:m
        w1 = ones(size(ff));
        w2 = ones(size(ff))
        for j = [1:k-1 k+1:n]
            for j = [1:l-1 l+1:n]
                w1 = (x-x(j))./(x(k)-x(j)).*w1;
                w2 = (y-y(i))./(y(l)-y(i)).*w2;
            end
        end
        ff = ff + w1.*w2.*f(k,l);
    end
end

这是我的函数,然后我正在等待任何给定的 x,y,f 的答案,就像

x= 0:4;
y = [-6 -3 -1 6];
f=[2 9 4 25 50];

v = laginterp(x,y,f,ff);
plot3(x,y,'o',f,q,'-')

我总是感谢任何帮助一样!

I checked the answers about Lagrange interpolation, but I couldn't find a suitable one to my question. I'm trying to use Lagrange interpolation for a surface with matlab. Let's say I have a x and y vector and f=f(x,y). I want to interpolate this f function. I think, what I did is mathematically correct:

function q = laginterp(x,y,f,ff)

n = length(x);
m = length(y);
v = zeros(size(ff));
for k = 1:n
    for l = 1:m
        w1 = ones(size(ff));
        w2 = ones(size(ff))
        for j = [1:k-1 k+1:n]
            for j = [1:l-1 l+1:n]
                w1 = (x-x(j))./(x(k)-x(j)).*w1;
                w2 = (y-y(i))./(y(l)-y(i)).*w2;
            end
        end
        ff = ff + w1.*w2.*f(k,l);
    end
end

It is my function and then I'm waiting for an answer for any given x,y,f like

x= 0:4;
y = [-6 -3 -1 6];
f=[2 9 4 25 50];

v = laginterp(x,y,f,ff);
plot3(x,y,'o',f,q,'-')

I'm always grateful for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后知后觉 2024-12-23 00:22:34

拉格朗日插值本质上从来都不是插值的好选择。是的,许多讨论插值的文本的第一章都使用了它。这样好吗?不。这只是让它变得方便,是介绍插值思想的好方法,有时也可以证明一些简单的结果。

一个严重的问题是,用户决定尝试这种插值方法的悲惨借口,并发现你瞧,它确实对 2 或 3 个点有效。哇,看看那个!因此,明显的延续是在具有 137 个点或 10000 个数据点或更多的真实数据集上使用它,其中一些点通常是重复的。发生了什么?为什么我的代码没有给出好的结果?或者,也许他们会盲目地认为它确实有效,然后发表一篇包含无意义结果的论文。

是的,文件交换上有一个拉格朗日工具。是的,它甚至可能得到了一些很好的评论,这些评论是由一年级学生写的,他们并不真正知道自己在看什么,而且遗憾的是他们没有数值分析的概念。不要使用它。

如果您需要 MATLAB 中的插值工具,可以从 griddata 或 TriScatteredInterp 开始。这些将产生相当合理的结果。其他方法是径向基函数插值,其中FEX上也有一个工具,还有各种各样的样条线,我个人最喜欢。请注意,在不了解或不认识陷阱的情况下盲目使用任何插值都可能并且将会产生毫无意义的结果。但几乎所有数值方法都是如此。

Lagrange interpolation is essentially NEVER a good choice for interpolation. Yes, it is used in the first chapter of many texts that discuss interpolation. Does that make it good? No. That just makes it convenient, a good way to INTRODUCE ideas of interpolation, and sometimes to prove some simple results.

A serious problem is that a user decides to try this miserable excuse for an interpolation method, and finds that lo and behold, it does work for 2 or 3 points. Wow, look at that! So the obvious continuation is to use it on their real data sets with 137 points, or 10000 data points or more, some of which points are usually replicates. What happened? Why does my code not give good results? Or, maybe they will just blindly assume that it did work, and then publish a paper containing meaningless results.

Yes, there is a Lagrange tool on the File Exchange. Yes, it even probably got some good reviews, written by first year students who had no real idea what they were looking at, and who sadly have no concept of numerical analysis. Don't use it.

If you need an interpolation tool in MATLAB, you could start with griddata or TriScatteredInterp. These will yield quite reasonable results. Other methods are radial basis function interpolations, of which there is also a tool on the FEX, and a wide variety of splines, my personal favorite. Note that ANY interpolation, used blindly without understanding or appreciation of the pitfalls can and will produce meaningless results. But this is true of almost any numerical method.

ヤ经典坏疍 2024-12-23 00:22:34

这并没有直接解决您的问题,但是有一个 拉格朗日插值函数Matlab File Exchange 这似乎很受欢迎。

This doesn't address your question directly, but there's a Lagrange interpolation function on the Matlab File Exchange which seems pretty popular.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文