program for lagranges interpolation method using single for loop
조회 수: 3 (최근 30일)
이전 댓글 표시
how to program for lagranges interpolation method using single for loop
댓글 수: 4
Geoff Hayes
2018년 8월 23일
Please rename your local variable sum since it conflicts with the MATLAB built-in function of the same name.
Out of curiosity, why do you want to eliminate the inner for loop? For performance reasons?
답변 (1개)
Geoff Hayes
2018년 8월 23일
PJS - if we assume that x is a vector/array then u is simply
u = (a - x(1)) * (a - x(2)) * ... * (a - x(i-1)) * (a - x(i+1)) * ... * (a - x(n));
where n is the number of elements in x. If this is true, then couldn't we do something like
for i = 1:length(x)
u = 1;
l = 1;
xt = x;
xt(i) = []; % remove the ith element
u = prod(a - xt);
% etc.
end
l = (x(i) - x(1)) * (x(i) - x(2)) * ... * (x(i) - x(i-1)) * (x(i) - x(i+1)) * ... * (ax(i) - x(n));
which means that the above can be replaced with
l = prod(x(i) - xt));
Try the above and see what happens!
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!