MATLAB Answers

0

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.

Asked by Nicole Reed on 26 Jun 2018
Latest activity Edited by dpb
on 26 Jun 2018
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 Comments

Sign in to comment.

1 Answer

Answer by Ameer Hamza
on 26 Jun 2018
Edited by Ameer Hamza
on 26 Jun 2018

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 Comments

Sign in to comment.