Lagrange Interpolating Polynomial Function

5 views (last 30 days)
Robin McManus
Robin McManus on 20 Nov 2020
Answered: Alan Stevens on 20 Nov 2020
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

Answers (1)

Alan Stevens
Alan Stevens on 20 Nov 2020
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).

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!