Skip to content

Instantly share code, notes, and snippets.

@normalhuman
Last active September 18, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save normalhuman/6ae2a9ac1682fa749acd to your computer and use it in GitHub Desktop.
Save normalhuman/6ae2a9ac1682fa749acd to your computer and use it in GitHub Desktop.
Interpolating polynomial, Vandermonde method
x = [1 2 3 4]'; % transpose to make a column
y = [3 1 4 1]';
n = length(x); % number of elements
M = zeros(n,n); % initialize a matrix n by n
for k=1:n
M(:,k) = x.^(k-1); % fill the matrix with powers of x-values
end
coeff = M\y; % find the coefficients
t = linspace(1,4);
poly = coeff(n)*ones(size(t)); % prepare for evaluation
for k=1:n-1
poly=poly.*t + coeff(n-k); % nested multiplication
end
plot(t, poly)
hold on
plot(x, y, 'r*') % add original points to show the curve passes through them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment