I'm trying to make an algorithm for Lagreange Interpolation Equation but the result won't show up, what did I do wrong?

2 ビュー (過去 30 日間)
I'm trying to make an algorithm for this Lagrange equation for n-order data points (in this case, there are 23 data points I'm trying to compute) with 15 different input for x. I've made the algorithm but somehow the result (there should be an array/matrix consists of 15 different y value) won't show up, it gives me an error NaN. I think I mess up with my loop and array but I don't which. Could anyone here point me where I messed it up?
In case someone's confused, xArr and yArr is the initial dataset (in the equation, it's the xi and f(xi) respectively). Whle xValue is the x input (in the eq., it's the "x" notation) and yValue is supposed to be the result.
%Generate Arrays
xArr = xlsread('TUGAS FISKOM.xlsx','Dataset','A2:A33');
yArr = xlsread('TUGAS FISKOM.xlsx','DataSet','B2:B33');
xValue = xlsread('TUGAS FISKOM.xlsx','PS','A2:A16');
yValueTemp = [];
xLen = length(xArr);
xValLen = length(xValue);
%Generate Loop to Create the Lagrange Equation
la = 0;
lb = 0;
for i = 1:1:size(xValue)
xa = 0;
xb = 0;
for n = 1:1:size(xArr)
xb = xb * (xArr(i,1) - xArr(n,1));
if i == n
;
else
xa = xa * (xValue(i,1) - xArr(n,1));
end
end
la = la * xa;
lb = lb * xb;
l = yArr(i,1)*la/lb;
yValue = [yValueTemp, l];
end
disp(yValue)

回答 (1 件)

John D'Errico
John D'Errico 2018 年 4 月 25 日
編集済み: John D'Errico 2018 年 4 月 25 日
High order Lagrange interpolation will produce numerical garbage.
xArr = xlsread('TUGAS FISKOM.xlsx','Dataset','A2:A33');
So, 33-2=32 data points? PURE GARBAGE. Virtually impossible to do without garbage resulting in double precision arithmetic.
Something that teachers sometimes forget to teach students is that what may work acceptably well for 2 or 3 or 4 points, is often complete crap on any real problem. That will surely happen here. But maybe that is the purpose of this homework assignment, to teach you that high order Lagrange is a flat out bad idea.
Lagrange interpolation is a nice way to teach students some basics about interpolation. Not hard to understand, not that hard to implement. But any teacher worth their salt should teach you that it is virtually never a good idea to use. (Ok, its always fine for a linear interpolant, given two points.)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by