Lagrange Interpolating Polynomial Function

조회 수: 2 (최근 30일)
Robin McManus
Robin McManus 2020년 11월 20일
답변: Alan Stevens 2020년 11월 20일
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