z = [6377 6378 6379 6380 6381 6382 6383 6385 6387 6392 6397 6402];
rho = [1.225 1.112 1.007 0.9093 0.8194 0.7364 0.6601 0.5258 0.4135 0.1948 0.08891 0.04008];
That is the data. z is in km, and it goes no at all as high as 7000km, but you apparently want to extrapolate out that far. First, PLOT YOUR DATA. ALWAYS PLOT EVERYTHING.
Ok. Now you want to extrapolate that out as far as 7000. But you used a quadratic polynomial. SERIOUSLY? Really, be serious, and think about what shape a quadratic polynomial would have. First, I'll fit a quadratic to your data. Since you used polyfit, I'll do that.
P2 = polyfit(z,rho,2)
P2 =
1.0e+04 *
0.0000 -0.0029 9.1639
rho7000 = polyval(P2,7000)
So the quadratic polynomial prediction out that far is over 800. But again, think about the fact that a quadratic polynomial has the shape of a parabola. It will certainly turn around, and start to shoot upwards. Even if I extrapolate out only as far as 6500, look at the shape you see.
zpred = linspace(6375,6500);
rhopred = polyval(P2,zpred);
plot(z,rho,'o',zpred,rhopred,'r-')
By the time it gets out as far as 7000, what do you expect? You have data over a terribly narrow rangee, and you want to extrapolate out where? Do I need to post my favorite quote by Mark Twain and the dangers of extrapolation?
Mark Twain, Life on the Mississippi (1884):
“In the space of one hundred and seventy six years the Lower Mississippi has shortened itself two hundred and forty-two miles. That is an average of a trifle over a mile and a third per year. Therefore, any calm person, who is not blind or idiotic, can see that in the Old Oölitic Silurian Period, just a million years ago next November, the Lower Mississippi was upwards of one million three hundred thousand miles long, and stuck out over the Gulf of Mexico like a fishing-pole. And by the same token any person can see that seven hundred and forty-two years from now the Lower Mississippi will be only a mile and three-quarters long, and Cairo [Illinois] and New Orleans will have joined their streets together and be plodding comfortably along under a single mayor and a mutual board of aldermen. There is something fascinating about science. One gets such wholesale returns of conjecture out of such a trifling investment of fact.”
A good rule is never extrapolate far beyond the support of your data. How far is far? Sigh, that kind of depends on how noisy is your data, how nonlinear is it, and what model I was using to extrapolate. Does the model make physical sense? That is, some models are used that are based physical reasonaing about the data and where the relationship comes from. These models might be trusted more, than say a polynomial model. A polynomial model has no reason to be correct, or to be trusted beyond the region of support.
Now maybe you were told to use such a model by your teacher. How can we know?
Just looking at your data, it appears to be something that is approaching an asymptote, perhaps eventually flattening out. So you might use a model that has that property. Such a model may have been chosen for no good reason of course, but it MAY be a better choice than a quadratic. For example, better might be to use a simple negative exponential model. Something of this form:
rho = a*exp(-b*z)
If that is the case, then a semilogy plot will be a straight line.
So not terrible, but not terribly great either. But at least it will predict something rational if you extrapolate it. And that model is easy to generate from polyfit. Just log z, then use a linear model.
P1 = polyfit(z,log(rho),1)
so it predicts a value that is essentially zero if we go out that far. And that may be unreasonably low, but at least it is far more meaningful that 806. Actually, you might have decided to use a quadratic polynomial for log(rho). At least that polynomial will not turn around and start predicting completely meaningless results, because this data has negative curvature, even in log(rho).
P2 = polyfit(z,log(rho),2)
P2 =
1.0e+04 *
-0.0000 0.0022 -6.8786
As expected, that predicts a value even far closer to zero when you go that far out. Still better than 806 though.