Trying to do polynomial interpolation but I get a matrix full of zeros (almost)

4 views (last 30 days)
Hi, 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?

Accepted Answer

Jan
Jan on 23 Jan 2019
Why do you assume, that the matrix A contains zeros? It does not:
x = [150, 200, 300, 500, 1000, 2000, 99999]';
y = [2, 3, 4, 5, 6, 7, 8]';
A = [x.^6, x.^5, x.^4, x.^3, x.^2, x, ones(numel(x),1), y];
nnz(A)
This replies 56 non-zero elements, exactly what it is expected. But maybe you have overseen the "10^29 * ..." in front of the displayed values. The 0.000... occur only, because all numbers are displayed in the same magnitude. You can change this by the format command:
format long g
Now you will see the values.
The solution will suffer from rounding. It will be more stable to transform the input such that x is in the range of [0, 1], or mean(x) is 0 and std(x) is 1.
  2 Comments
Edin Suta
Edin Suta on 23 Jan 2019
Ah... I see (I didn't know about the 'nnz' function in Matlab). Is there an easy way to normalize the input so that it is in the range [0,1]?
Also, I added an edit to my post that gives a bit of context to this task. You should read it... it might clear some things up (sorry). After reading the edit, do you think there is a better way to do this interpolation or an ever better way to handle the input data?
Thanks!
Jan
Jan on 25 Jan 2019
Normalize e.g. by: xx = (x - mean(x)) / std(x)
Then you have to remember these values of mean(x) and std(x) to apply the transformation again when evaluating the polynomial. See the help and source of polyfit and ployval.

Sign in to comment.

More Answers (0)

Categories

Find more on Polynomials in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!