无意中在知网发现了一篇关于高维拉格朗日插值法研究的论文,故将之分享给大家~
以下是我根据该篇论文所编写的MATLAB代码:
%realize Lagrange polynomial-2
function [x,v,y,u,z,w] = lagpol2(a,b,n)
s = [a:(b-a)/(3*(n-1)):b];
t = [a:(b-a)/(4*(n-1)):b];
[x,y] = meshgrid(s);
[v,u] = meshgrid(t);
z = cos(x).*cos(y);
lens = length(s);
lent = length(t);
w = zeros(lent);
for i0 = 1:lent
for j0 = 1:lent
%二维插值开始
g = linspace(0,0,lens);
for k = 1:lens
%对x的一维插值开始
for i1 = 1:lens
temp1 = 1;
for j1 = 1:lens
if j1~= i1
temp1 = temp1 * (v(i0,j0) - x(1,j1))/(x(1,i1) - x(1,j1));
end
end
g(k) = g(k) + temp1 * z(k,i1);
end
%对x的一维插值结束
end
%对y的一维插值开始
for i2 = 1:lens
temp2 = 1;
for j2 = 1:lens
if j2 ~= i2
temp2 = temp2 * (u(i0,j0) - y(j2,1))/(y(i2,1) - y(j2,1));
end
end
w(i0,j0) = w(i0,j0) + temp2 * g(i2);
end
%对y的一维插值结束
%二维插值结束
end
end