The variable appears to change size in every loop iteration, I have no idea how to fix. I get this for divDIF Line 13 and a line 21.

17 ビュー (過去 30 日間)
Nicole Reed
Nicole Reed 2018 年 6 月 26 日
編集済み: dpb 2018 年 6 月 26 日
function Yint = NewtonsINT(x,y,Xint)
% NewtonsINT fits a Newtons polynomial to a set of given points and
% uses the polynomial to determines the interpolated value of a point.
% Input variables:
% x A vector with the x coordinates of the given points.
% y A vector with the y coordinates of the given points.
% Xint The x coordinate of the point to be interpolated.
% Output variable:
% Yint The interpolated value of Xint.
n = length (x);
a(1) = y(1);
for i = 1:n - 1
divDIF(i,l)=(y(i+l)-y(i))/(x(i+ 1)-x(i));
end
for j = 2 :n - 1
for i = l:n - j
divDIF(i,j)=(divDIF(i+l,j-l)-divDIF(i,j-1))/(x(j+i)-x(i));
end
end
for j = 2 :n
a(j) = divDIF(l,j - 1);
end
Yint = a(1);
xn = 1;
for k = 2:n xn = xn*(Xint - x (k - 1));
Yint = Yint + a (k) *xn;
end

回答 (1 件)

Ameer Hamza
Ameer Hamza 2018 年 6 月 26 日
編集済み: Ameer Hamza 2018 年 6 月 26 日
This is not an error, this is a warning which mlint produce to increase the speed and efficiency of your code. It is better to pre-allocate variable's memory to increase speed. For more details, see here: https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html. However, this warning should not stop the code from execution. The code should work as expected, although slower then pre-allocation. The actual error which I can see in your code is that the variable l is not defined. You are using it at several places of your code but its value is not assigned anywhere. This will be actually causing an error.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by