function a = Coef(n, x, y) % This procedure requires: % x = horizontal array of x values % y = horizontal array of y values % n = degree of interpolation polynomial % [This means that there must be (n + 1) elements in % x and y.] % % My example was: % x = [1 2 4 5] % y = [3 4 0 -5] % a = coef(3, x, y) % [Type the above into the Command Window!] % MATLAB will copy the coefficients into the horizontal % array "a". % % First, copy y into a. a = y; % Now calculate the coefficients. % We only run the j loop a total of n times. (Each loop is % successively shorter.) % The starting and stopping values for i were modifed to % accomodate positive indices in MATLAB. for j=1:n for i=(n+1):-1:(j+1) a(i) = (a(i) - a(i-1))/(x(i) - x(i-j)); end end