Lagrange Interpolating Polynomial Function

2 次查看(过去 30 天)
I'm trying to write MATLAB code that outputs a Lagrange Interpolating Polynomial. I'll include my code below, although its definitely not refined yet. I was planning on making an array of functions, but I just noticed that matlab doesn't accept functions in arrays. Is there a way around this?
x = [-1 2 3 5]
y = [0 1 1 2]
n = length(x)
f = @(x) x;
array = ones(1,(n-1));
for k = 0: n-1
for i = 0 : n-1
if i ~= k
array(k) = array(k) * (f(x)-x(i))/(x(k)-x(i))
end
end
end
array

回答(1 个)

Alan Stevens
Alan Stevens 2020-11-20
Matlab indexing starts from 1 not zero. Also, in
array(k) = array(k) * (f(x)-x(i))/(x(k)-x(i))
you should have f(x(i)) not f(x).

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by