Lagrange Interpolating Polynomial Function

2 visualizaciones (últimos 30 días)
Robin McManus
Robin McManus el 20 de Nov. de 2020
Respondida: Alan Stevens el 20 de Nov. de 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

Respuestas (1)

Alan Stevens
Alan Stevens el 20 de Nov. de 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).

Categorías

Más información sobre Polynomials en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by