我有以下功能:
% function file: lagrange.m
function Yint = lagrange( x, y, Xint )
n = length( x );
for i = 1 : n
L(i) = 1;
for j = 1 : n
if j ~= i
L(i) = L(i) * (Xint - x(j)) / (x(i) - x(j));
end
end
end
Yint = sum( y .* L );
end
然后我尝试使用这个脚本:
% script file: roteiro_lagrange.m
clear all
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28];
y = [2.682942 3.818595 3.28224 2.486395 3.082151 5.441169 8.313973 9.978716 9.824237 8.911958 9.00002 10.92685 13.84033 15.98121 16.30058 15.42419 15.7721 16.49803$
xi = [16.8 18.2 2.8 15.1 19.2 5.6 13.1 19.8 8.2 10.9 20.0 10.7 8.4 19.8 13.0 5.8 19.2 15.0 3.0 18.3 16.7 0.2 16.9 18.1 2.6 15.3 19.1 5.4];
n = length(x)
ny = length(y)
YI = [];
for i = 1 : n
xp = xi(i);
for k = 2 : n
if x(k) > xp
xx = [x(k-1) x(k)];
yy = [y(k-1) y(k)];
yp = lagrange( xx, yy, xp );
YI = [YI yp];
break
endif
endfor
endfor
y_interp = YI'
plot( x, y, 'r', xi, YI, 'k*' )
但我在尝试运行它时遇到以下错误:
lagrange
error: 'x' undefined near line 3 column 10
error: called from
lagrange at line 3 column 2
error: evaluating argument list element number 1
error: called from
lagrange at line 3 column 2
(请参阅此处的问题截图:https://i.stack.imgur.com/FONU6.jpg(
您直接调用函数,不带任何参数(注意,在matlab/倍频程中,lagrange
与执行lagrange()
完全相同(。
你应该调用你的脚本。