Last active
September 18, 2015 05:09
-
-
Save normalhuman/6ae2a9ac1682fa749acd to your computer and use it in GitHub Desktop.
Interpolating polynomial, Vandermonde method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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