拉格朗日插值多项式
Lagrange interpolation polynomial
我有分段线性函数
我尝试为 2、6、14 台主机构建拉格朗日多项式。但是对于我的主机,我原来的功能变得不正确:
我认为我的多项式向右转,但我在计算和建立不准确度方面遇到困难
a = 1;
b = -1;
c = -1;
d = 1;
x = -1:0.33:1;
y = [];
for i = 1 : length(x)
if (x(i) <= c/2)
y(i) = x(i)+ 1;
elseif(x(i) >= d/2)
y(i) = x(i) - 1;
else
y(i) = -x(i);
end
end
plot(x, y);
hold on
nx = -1:0.01:1;
ny=lagrange(x,y,nx);
plot(x,y,'bo');
plot(nx,ny,'r');
function ny=lagrange(x,y,nx)
N = length(x);
ny=zeros(size(nx));
for k=1:N
t=ones(size(nx));
for j=[1:k-1, k+1:N]
t=t.*(nx-x(j))/(x(k)-x(j));
end
ny = ny + y(k)*t;
end
好吧,我犯了一些错误。首先,我传递了错误的参数来运行。另外,我没有正确计算原始函数的值。 "original" 函数计算原始图形的值。 "lagrange" 函数计算值多项式。
x = -1:0.01:1;
y = [];
y = original(x);
subplot (2, 1, 1);
plot(x, y);
xlabel('x');
ylabel('y');
hold on;
grid on;
%hosts
xx = -1:0.5:1;
%values of original func in hosts
yy = original(xx);
plot(xx,yy,'o');
nx = -1:0.01:1;
ny=lagrange(xx,yy,nx);
plot(nx,ny,'r');
subplot (2, 1, 2);
%measure of inaccuracy
plot(x,ny-y, '');
grid on;
我有分段线性函数
我尝试为 2、6、14 台主机构建拉格朗日多项式。但是对于我的主机,我原来的功能变得不正确:
我认为我的多项式向右转,但我在计算和建立不准确度方面遇到困难
a = 1;
b = -1;
c = -1;
d = 1;
x = -1:0.33:1;
y = [];
for i = 1 : length(x)
if (x(i) <= c/2)
y(i) = x(i)+ 1;
elseif(x(i) >= d/2)
y(i) = x(i) - 1;
else
y(i) = -x(i);
end
end
plot(x, y);
hold on
nx = -1:0.01:1;
ny=lagrange(x,y,nx);
plot(x,y,'bo');
plot(nx,ny,'r');
function ny=lagrange(x,y,nx)
N = length(x);
ny=zeros(size(nx));
for k=1:N
t=ones(size(nx));
for j=[1:k-1, k+1:N]
t=t.*(nx-x(j))/(x(k)-x(j));
end
ny = ny + y(k)*t;
end
好吧,我犯了一些错误。首先,我传递了错误的参数来运行。另外,我没有正确计算原始函数的值。 "original" 函数计算原始图形的值。 "lagrange" 函数计算值多项式。
x = -1:0.01:1;
y = [];
y = original(x);
subplot (2, 1, 1);
plot(x, y);
xlabel('x');
ylabel('y');
hold on;
grid on;
%hosts
xx = -1:0.5:1;
%values of original func in hosts
yy = original(xx);
plot(xx,yy,'o');
nx = -1:0.01:1;
ny=lagrange(xx,yy,nx);
plot(nx,ny,'r');
subplot (2, 1, 2);
%measure of inaccuracy
plot(x,ny-y, '');
grid on;