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.
11 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!