Problem with twodimensional equidistant lagrange interpolation
5 次查看(过去 30 天)
显示 更早的评论
Hello,
currently I am working on school project about 2D Lagrange Interpolation, in this case I am supposed to use the fact l_i,j=l_i*l_j. I was trying many ways to do that but I think this one should work but it isn't. I am inspired by my teacher's code for 1D interpolation. There is my code below. Firstly I calculate l_i for every point of interpolation, then l_j in the y-direction. Then i add it's product multiplied by functional value in current interpolation point to polynom which is equal to zero vector firstly. It was kind of complicated because of vector dimensions, but still it shows me the polynom vector as 201 (as it should be) dimensional vector of NaNs. I am hoping either for help or any more ellegant solution to this problem. Thank you
function polynom = TDLI(f,a,b,n)
uzlyx=a:(b-a)/n:b;
uzlyy=uzlyx;
xx=a:0.01:b;
polynom=zeros(size(xx));
for i=0:n
l_i=ones(size(xx));
for k=0:n
if k~=i
l_i=l_i.*(xx-uzlyx(k+1))/(uzlyx(i+1)-uzlyx(k+1));
end
for j=0:n
l_j=ones(size(xx));
for m=0:n
if m~=j
l_j=l_j.*(xx-uzlyy(m+1))/(uzlyy(i+1)-uzlyy(m+1));
end
L=l_i.*l_j;
polynom=polynom+f(uzlyx(i+1),uzlyy(j+1))*L;
end
end
end
end
0 个评论
回答(1 个)
John D'Errico
2020-12-12
编辑:John D'Errico
2020-12-12
Sigh. 2d Lagrange? You do realize that some time after developing Lagrange interpolation, Lagrange died? There must be some causal influence here. He is dead now, and alive before he developed it. Case closed. ;-) ;-)
Ok, that was said very much with tongue in cheek. But somewhat seriously, I want to impress upon you that Lagrange interpolation is actually not a good tool to use in the future for you, even though you have been tasked with this as a project. (I'll look at your code after this digression is done.) Why not?
We can see the problem even in 1-dimension. Consider a simple function of 1 variable, to be interpolated over a set of points using say a moving cubic Lagrange. (You REALLY don't want to consider the use of high order Lagrange, as high order polynomials oscillate terribly. Even cubic polynomials can be problematic. Worse, you cannot perform the computations in any reasonable amount of precision as the polynomial order grows at all large.)
x = linspace(0,pi,10);
y = sin(x);
plot(x,y,'o')
Now, imagine that between a pair of points, you will use the 2 points on either side of the point in question to interpolate. So as you follow along the curve between that pair of points, you get a nice, smoooth interpolant.
But once you cross a node of the function, suddnly, you are uing a DIFFERENT collection of 4 points from before. The result is the function you will see interpolated will be merely continuous, but NOT differentiable across that node of the interpolant.
The net result is a nice, smooth interpolant BETWEEN nodes, but first derivative singularities at every node. I don't want to get into too much depth here, beyond explaining why Lagrange should never be used in practice.
So you really don't have any reason to be using Lagrange interpolation beyond a first order interpolant, since splines exist, and can produce twice continuously differentiable results across the nodes. Get it? Lagrange is a bad idea, at least beyond the linear case. And high order Lagrange is REALLY a bad idea.
One caveat applies in this, in that FIRST order Lagrange interpolants are indeed a viable tool, since if you are willing to do connect the dots interpolation, then you already expect an interpolant that is piecewise linear, with derivative singularities at each break.
plot(x,y,'-o')
So when I did the connect-the-dots plot here, I effectively employed a first order moving Lagrange interpolant. As you can see, it has slope changes at each node, and this is expected.
Ok. Rant mode over. Yes, use Lagrange for your project. As long as you understand this is a tool to be used for your project, and then never again, then go for it.
So next, let me loo at what you wrote.... (give me a couple of minutes to read your code.)
Ugh, retching sound. You are building a Lagrange polynomial of order 201? And that in only ONE dimension? You need to understand that these computations cannot be performed in double precision? Do you accept just how nastily these polynomials will act, since you are effectively raising numbers to powers as large as 201?
A Lagrange interpolant makes sense only if it is low order. Thus over a span, you use nodes [1 2 3 4]. Then you swap to nodes [2 3 4 5],, then nodes [3 4 5 6], etc., moving along the curve. Seriously, if you really expect to perform tensor product high order Lagrange interpolation, where the order of the polynommial is 201 in each dimension, then your project is dead in the water immediately. You cannot do it. Tensor product LINEAR interpolation is fine. That is done all the time. Even a tensor product cubic is doable, though a bit complex. Sorry, but this is time to cut your losses. Talk to your instructor, and make sure you understand what is the goal of your task.
1 个评论
Nathan Welch
2022-6-19
tl:dr
Lagrange polynomials not too bad - simple and most issues improved by using Chebyshev nodes
Splines not a silver bullet - more complex, can still show oscillation, piecewise look-up potentially costly
I've just started using Lagrangian interpolation polynomials.
What I've found so far - they're easy to understand and quick to calculate with the right coding - probably why they were set as a coding project.
I was also concerned about the high order oscillation and problems raising a value to a high power - so I rescaled the x-axis using Chebyshev nodes which are quick and easy to calculate (just a cos function). To give it a test, I used a 201 order Lagrange polynomal. I was able to create:

No sign of dreaded oscillation or double precision failure and instead signs of improving accuracy with increasing order.
I then looked at MATLAB's example of using spline interpolation:

Splines can show noticable oscillation. With piecewise cubic polynomials doing a much better job.
Just for comparison, a 7 point Lagrange polynomial produces similar and slightly better results than the spline:

Lastly, Spline's are complex to calculate - compare the single product equation of the Lagrange polynomial with a Spline algorithm eg: https://en.wikipedia.org/wiki/Spline_(mathematics)#Algorithm_for_computing_natural_cubic_splines
Also, piecewise methods with varying gaps between data involve a costly lookup method to find which 'piece' to use - could be a killer especially if interpolation is being used to speed up interpolating inside previously created data rather than just producing more data.
So all-in-all, I don't believe it's quite as obvious that Lagrange polynomials are awful and should be killed off. I think it's often down to using a suitable tool for a certain job.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

