matlab 拉格朗日插值程序求解释n=11; %¨lb=-5; %ub=5; %step=0.01; %ìì%-----------------------------% x=lb:step:ub;y=1./(1+x.^2);plot(x,y,'r-');hold on%-------------------------
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 18:54:37
matlab 拉格朗日插值程序求解释n=11; %¨lb=-5; %ub=5; %step=0.01; %ìì%-----------------------------% x=lb:step:ub;y=1./(1+x.^2);plot(x,y,'r-');hold on%-------------------------
matlab 拉格朗日插值程序求解释
n=11; %¨
lb=-5; %
ub=5; %
step=0.01; %ìì
%-----------------------------
%
x=lb:step:ub;
y=1./(1+x.^2);
plot(x,y,'r-');
hold on
%-----------------------------
%
for i=1:n+1
xi(i)=lb+(ub-lb)*(i-1)/n;
yi(i)=1/(1+xi(i)^2);
end
%------------------------------
count=1;
for x=lb:step:ub
fl=0;
%--------------------------
%¨pn(xk)
for k=1:n+1
up=1;
dn=1;
%----------------------
%¨f(xk)
for i=1:n+1
if k~=i
up=up*(x-xi(i));
dn=dn*(xi(k)-xi(i));
end
end
%----------------------
fl=fl+yi(k)*up/dn;
end
pn(count)=fl;
%--------------------------
fi(count)=1/(1+x^2);%¨
count=count+1;
end
%------------------------------
% L
x=lb:step:ub;
plot(x,pn,'b-')
%------------------------------
num=(ub-lb)/step+1;
for i=1:num
p_f(i)=pn(i)-fi(i);
end
center=fix(num/2);
scale=fix(num/10);
a=center-scale;
b=center+scale;
disp ' pn(i)-fi(i) '
p_f(a:b)
matlab 拉格朗日插值程序求解释n=11; %¨lb=-5; %ub=5; %step=0.01; %ìì%-----------------------------% x=lb:step:ub;y=1./(1+x.^2);plot(x,y,'r-');hold on%-------------------------
% 为了说明language高次差值的龙格现象
n=11; % n次language差值
lb=-5; %区间下限
ub=5; %区间上限
step=0.01; %绘图步长
%-----------------------------
% 绘制 1./(1+x.^2) 图形 (红)
x=lb:step:ub;
y=1./(1+x.^2);
plot(x,y,'r-');
hold on
%-----------------------------
% 获得进行language差值的已知点
for i=1:n+1
xi(i)=lb+(ub-lb)*(i-1)/n; %将区间分为11等份
yi(i)=1/(1+xi(i)^2); %计算区间端点的精确函数值
end
%------------------------------
% language 差值计算
count=1; % count 为向量元素的下标
for x=lb:step:ub
fl=0;
%--------------------------
%¨pn(xk)
for k=1:n+1 % 24-36行是差值函数的计算
up=1;
dn=1;
%----------------------
%¨f(xk)
for i=1:n+1
if k~=i
up=up*(x-xi(i));
dn=dn*(xi(k)-xi(i));
end
end
%----------------------
fl=fl+yi(k)*up/dn; % 得到由language差值函数在x点处的值
end
pn(count)=fl;
%--------------------------
fi(count)=1/(1+x^2);%计算x点处的精确函数值
count=count+1;
end
%------------------------------
% L
x=lb:step:ub;
plot(x,pn,'b-') % 绘制由language差值函数的图像(蓝)
%------------------------------
num=(ub-lb)/step+1; % 绘制函数图像所取的点数
for i=1:num
p_f(i)=pn(i)-fi(i); % 计算language差值函数的误差
end
center=fix(num/2); % center=[1001/2]=500
scale=fix(num/10); % scale=100
a=center-scale;
b=center+scale;
disp ' pn(i)-fi(i) ' % 显示 pn(i)-fi(i)
p_f(a:b) % 显示 i=400-600 数据点