%%%程序编写者 西北工业大学自动化学院 Email: yincwxa2013@mail.nwpu.edu.cn
%% All rights reserved
clear
clc
x1=input('输入节点坐标x=')
y=input('输入节点坐标函数值f(x)=')
x2=input('输入所要计算的节点x2=')
syms x
n=length(x1);
l=0;
for i=1:n
t1=1;
for j=1:n
if j==i t1=t1;
else t1=t1*(x-x1(j));
end
end
t2=1;
for j=1:n
if j==i t2=t2;
else t2=t2*(x1(i)-x1(j));
end
end
l=l+t1/t2*y(i);
end
fprintf('拉格朗日插值函数为n')
expand(l)
x=x2;
p=eval(l);
fprintf('插值函数在所求点x2的函数值为n')
p
输入节点坐标x=[2 4 6 8]
x1 =
2 4 6 8
输入节点坐标函数值f(x)=[4 6 10 14]
y =
4 6 10 14
输入所要计算的节点x2=3
x2 =
3
拉格朗日插值函数为
ans =
-1/24*x^3+3/4*x^2-7/3*x+6
插值函数在所求点x2的函数值为
p =
4.6250
转载本文请联系原作者获取授权,同时请注明本文来自殷春武科学网博客。
收藏
分享
分享到: