I am trying to write some code that does polynomial interpolation but I can't get it to work. I am a student and I am following the logic from this video https://www.youtube.com/watch?v=VpI-wC94RKw in order to recreate it it in code-form in Matlab, but whenever I try to create my own version of the matrix shown in the video I instead get a matrix almost exclusively filled with zeros (except one element). I can't understand why this is happening.
My code:
x=[150, 200, 300, 500, 1000, 2000, 99999]';
y=[2, 3, 4, 5, 6, 7, 8]';
function f = interPoly(x,y)
% Skapar en matris A var varje rad är [x_1^6, x_1^5,..., 1] till [x_n^6, x_n^5,..., 1]
A = [x.^6 x.^5 x.^4 x.^3 x.^2 x ones(numel(x),1) y];
% Gaussar matris A
R = rref(A);
% Plockar sista kolumnen ur R
c = R(:,end);
f = c(1)*x.^6+c(2)*x.^5+c(3)*x.^4+c(4)*x.^3+c(5)*x.^2+c(6)*x+c(7);
end
(The matrix 'A' is the one that's being problematic here. The function I get in the end is also just filled with zeros as values. Also sorry for the comments being in swedish)
I have 7 values in x and y and therefore a polynomial of the 6th order but I don't really know what the constant should be in the second last column so I just put a bunch of ones there (I am new to this so I am a little bit unsure about the logic).
Anyway I have tried the using the same function with some other input data and it has worked fine.
Alt input data:
x=[0, 0.5, 1, 1.5, 2, 2.99, 3]';
y=[0, 0.52, 1.09, 1.75, 2.45, 3.5, 4]';
Does it give me zeros because the elements overflow (for example 99999^6 is a very high number)? I don't really understand what is going on here and why it's working just fine with a different set of input data. Help?
Thanks!
EDIT: The entire point of this task (given by my school) is to compare the "least squares" method (which I have also written code for but not posted) with a polynomial interpolation method (the one in the code above). The last value in 'x' above is supposed to be infinity (f(inf)=8) so I just replaced it with a really high number, hence why it isn't "evenly" distributed. Is there a better way to do this?