Clear Filters
Clear Filters

Curve Fitter Polynomial Plots Strangely In Script

23 views (last 30 days)
Michelle Tiner
Michelle Tiner on 24 Jun 2024 at 21:27
Commented: Steven Lord on 25 Jun 2024 at 13:28
Hello,
I am trying to analyze some doppler shifts on a spectrogram. I am using the Matlab Curve Fitter Tool to fit a curve to very specific data. The curve fitter works well. It generates polynomial parameters but when I try to graph this polynomial in a normal script, the output looks nothing like the graph in the curve fitter.
Here is the curve fitter:
Here is the curve fitter polynomial output:
And when I try taking these values (p1, ... p10) and plotting the polynomial in a regular script, the output looks like this:
Which looks nothing like the curve in the curve fitter app. Additionally, the y-axis for the script graph is way off. I have tried playing around with the scaling outputs that the polyfit() function gives me but I haven't made any progress. I have also tried to use the "center and scale" option but haven't gotten that to work either. I clearly don't understand something fundamental with this tool.
Thanks in advance for any help!
Here is my script's code: (p values pulled straight from the curve fitter app)
p1 = 0.0067;
p2 = -1.7909;
p3 = 155.6940;
p4 = 1.1965e+03;
p5 = -1.2672e+06;
p6 = 1.1048e+08;
p7 = -4.9186e+09;
p8 = 1.258e+11;
p9 = -1.7629e+12;
p10 = 1.0551e+13;
x = 40:0.01:50;
p = [p1 p2 p3 p4 p5 p6 p7 p8 p9 p10];
plot(x, polyval(p, x));

Answers (1)

John D'Errico
John D'Errico on 24 Jun 2024 at 23:39
Edited: John D'Errico on 25 Jun 2024 at 0:23
CONGRATULATIONS! You win an award for the one millionth person to make this mistake! Your award is a free trip to the fabled vacation spot of Newark New Jersey. All you need to do is pay for your travel expenses, and hotel bills, plus food, and any entertainment, etc. Don't even ask what is the second place award. ;-)
What have you done wrong? You copied and used those coefficients to only 5 significant digits. The problem is, you NEED every single digit in those numbers. Just because you saw only those coefficients rounded to 5 digits does not mean that is the correct number. And if you use even slightly the wrong coefficient, you will get complete crapola. The errors will be most obvious at either end of the curve.
(Hint: Next time, do not fit order 10 polynomials, as high order polynomials will see this behavior get worse.)
Why is this an issue? Consider that your x vector has numbers on the order of 40 to 50. Now, raise those numbers to the 9th power.
50^9
ans = 1.9531e+15
Do you see that now if you multiply that number by something that has an error of even 1 part in 100000, the result can vary by 10^10? Now, add and subtract those terms, each time, where the result is in error by an amount on the order of 1e10. Yep, You get complete, utter, total crapola. As I said, this is a very common mistake we see made.
  2 Comments
Michelle Tiner
Michelle Tiner on 25 Jun 2024 at 13:05
Ah thank you very much. Now, before I pack for Newark, is there a way to get more significant digits? I've tried exporting to the workplace but it keeps the same 5 significant digits. Or maybe a better question is how would you suggest finding the maximum slope of this line? Maybe there's a better way to do this than using this tool? Again, thank you very much.
Steven Lord
Steven Lord on 25 Jun 2024 at 13:28
If you're exporting to the workspace so you can evaluate the fit, don't try to copy the coefficients out of the fit object. Just evaluate it directly.
But note that in addition to what John wrote, there's a little warning icon in the Results section of the Curve Fitting app. And when you look at the confidence intervals for your fit, they are pretty wide especially for the lower order coefficients!
To paraphrase the hint John gave in the words of Jeff Goldblum's character in Jurassic Park, "Yeah, yeah, but your scientists were so preoccupied with whether or not they could [fit a 10th degree polynomial] that they didn't stop to think if they should."
Outside the interval [42, 48] your fit takes off like a rocket and doesn't fit that outlying data very well at all. How sure are you, based on that plot and your understanding of whatever process generated the data you collected, that a polynomial is even the right curve to fit to this data? Might something more like a sigmoid function (or some constant minus a sigmoid function, so it starts high and ends low) better align with your data? Or maybe a Gaussian?

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!