matlab newton插值问题
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/17 07:23:41
matlab newton插值问题
给定[0,1.2]内的被插函数f(x)=cosx,插值点
x=[0 0.4 0.8 1.2], y=[1 0.921061 0.696707 0.362358],
编写Newton插值函数,求xi=[0.45 0.58 0.02]处的函数值yi,绘制f(x) 的图形且标出(xi,yi)
不会编写
绘制f(x) 的图形且标出(xi,yi)
求帮助啊
给定[0,1.2]内的被插函数f(x)=cosx,插值点
x=[0 0.4 0.8 1.2], y=[1 0.921061 0.696707 0.362358],
编写Newton插值函数,求xi=[0.45 0.58 0.02]处的函数值yi,绘制f(x) 的图形且标出(xi,yi)
不会编写
绘制f(x) 的图形且标出(xi,yi)
求帮助啊
function f = Newton(x,y,x0)
%本程序为Newton插值,其中x,y
%为插值节点和节点上的函数值,输出为插值点x0的函数值,
%x0可以是向量.
syms t;
if(length(x) == length(y))
n = length(x);
c(1:n) = 0.0;
else
disp('x和y的维数不相等!');
return;
end
f = y(1);
y1 = 0;
l = 1;
for(i=1:n-1)
for(j=i+1:n)
y1(j) = (y(j)-y(i))/(x(j)-x(i));
end
c(i) = y1(i+1);
l = l*(t-x(i));
f = f + c(i)*l;
simplify(f);
y = y1;
if(i==n-1)
if(nargin == 3)
f = subs(f,'t',x0);
else
f = collect(f); %将插值多项式展开
f = vpa(f,6);
end
end
end
x=[0 0.4 0.8 1.2];
y=[1 0.921061 0.696707 0.362358];
xi=[0.45 0.58 0.02];
yi = Newton(x,y,xi);
xx=0:0.01:1.2;
plot(xx,cos(xx),xi,yi,'or')
%本程序为Newton插值,其中x,y
%为插值节点和节点上的函数值,输出为插值点x0的函数值,
%x0可以是向量.
syms t;
if(length(x) == length(y))
n = length(x);
c(1:n) = 0.0;
else
disp('x和y的维数不相等!');
return;
end
f = y(1);
y1 = 0;
l = 1;
for(i=1:n-1)
for(j=i+1:n)
y1(j) = (y(j)-y(i))/(x(j)-x(i));
end
c(i) = y1(i+1);
l = l*(t-x(i));
f = f + c(i)*l;
simplify(f);
y = y1;
if(i==n-1)
if(nargin == 3)
f = subs(f,'t',x0);
else
f = collect(f); %将插值多项式展开
f = vpa(f,6);
end
end
end
x=[0 0.4 0.8 1.2];
y=[1 0.921061 0.696707 0.362358];
xi=[0.45 0.58 0.02];
yi = Newton(x,y,xi);
xx=0:0.01:1.2;
plot(xx,cos(xx),xi,yi,'or')