user1018331
user1018331

Reputation: 143

lagrange interpolation

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!

Upvotes: 0

Views: 9535

Answers (2)

user85109
user85109

Reputation:

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.

Upvotes: 4

eykanal
eykanal

Reputation: 27027

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

Upvotes: 1

Related Questions