Why the result in curve fitting tool shows a different graph in plot?

19 views (last 30 days)
result in curve fitting tool:
General model Rat45:
f(x) = (p1*x^4 + p2*x^3 + p3*x^2 + p4*x + p5) /
(x^5 + q1*x^4 + q2*x^3 + q3*x^2 + q4*x + q5)
Coefficients (with 95% confidence bounds):
p1 = 4.027 (3.073, 4.982)
p2 = -59.67 (-71.99, -47.35)
p3 = 329.9 (270.7, 389.2)
p4 = -806.9 (-934.3, -679.4)
p5 = 737.2 (631.6, 842.7)
q1 = -19.21 (-19.77, -18.65)
q2 = 142.4 (134.1, 150.7)
q3 = -497.1 (-540.9, -453.3)
q4 = 776.1 (681.9, 870.4)
q5 = -368.5 (-431.8, -305.2)
Goodness of fit:
SSE: 0.161
R-square: 0.9993
Adjusted R-square: 0.9991
RMSE: 0.07868
And the graph shows:
Then, I copy the curve equation and use plot to draw the curve
x = linspace(1,5.5,100);
y = (4.027*x.^4 + -59.67*x.^3 + 329.9*x.^2 + -806.9*x + 737.2)./(x.^5 + -19.21*x.^4 + 142.4*x.^3 + -497.1*x.^2 + 776.1*x + -368.5)
plot(x,y)
xlim([1,5.5])
Why these two curve is so different from each other?

Accepted Answer

Steven Lord
Steven Lord on 20 Jun 2022
The values that are displayed are not the same as the values that are stored in the object. The displayed values only show four digits, while the values are stored to double precision.
format
x = 1/3 % Is x exactly 0.3333?
x = 0.3333
y = x - 0.3333 % No, it is not
y = 3.3333e-05
z = x - 0.3333 - 3.3333e-05 % Is it 0.3333 + 3.3333e-05? No.
z = 3.3333e-10
format longg
x
x =
0.333333333333333
You could extract the coefficients using coeffvalues to create a double precision array, or if you just want to evaluate the fit just do that.
x = (1:0.25:5).';
y = (1/3)*x.^2 + (1/4)*x - (1/5)
y = 17×1
0.383333333333333 0.633333333333333 0.925 1.25833333333333 1.63333333333333 2.05 2.50833333333333 3.00833333333333 3.55 4.13333333333333
f = fit(x, y, 'poly2')
f =
Linear model Poly2: f(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 0.3333 (0.3333, 0.3333) p2 = 0.25 (0.25, 0.25) p3 = -0.2 (-0.2, -0.2)
y2 = f(x) % Evaluate the fit at the points in x
y2 = 17×1
0.383333333333333 0.633333333333333 0.925 1.25833333333333 1.63333333333333 2.05 2.50833333333333 3.00833333333333 3.55 4.13333333333333
  1 Comment
Hao Tu
Hao Tu on 20 Jun 2022
I dublicate the curve by the method you related. Such a tiny different in number precision would cause huge difference on the rational funciton result. I never consider that.
Thank you for your answer.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 20 Jun 2022
(This is only a followup, and since you already understand the issue, merely a clarification to Steven's accurate answer.)
Remember that when x is large, you are raising x to the 4th power. That creates a sufficiently large number that when multiply it by a coefficient that is now reproduced only to within a tolerance becomes a problem.
We see this output:
f(x) = (p1*x^4 + p2*x^3 + p3*x^2 + p4*x + p5) /
(x^5 + q1*x^4 + q2*x^3 + q3*x^2 + q4*x + q5)
Coefficients (with 95% confidence bounds):
p1 = 4.027 (3.073, 4.982)
p2 = -59.67 (-71.99, -47.35)
p3 = 329.9 (270.7, 389.2)
p4 = -806.9 (-934.3, -679.4)
p5 = 737.2 (631.6, 842.7)
q1 = -19.21 (-19.77, -18.65)
q2 = 142.4 (134.1, 150.7)
q3 = -497.1 (-540.9, -453.3)
q4 = 776.1 (681.9, 870.4)
q5 = -368.5 (-431.8, -305.2)
where x varies from 0 to over 5, when I look at the plot.
Consider the term in the numerator, for x^4. p1 is goven as 4.027. But in fact, it was only reported on the output as 4.027, but in reality, the number could have been anything in the interval [4.0265,4.0275]. When x is large enough, raising it to the 4th power is sufficiently large that those apparently small errors in the values that you used to plot the function start to cause problems. But worse, all of the coefficients have similar issues. Your use of only 4 significant digit approximations to those coefficients becomes a significant problem.
This is not the same thing as the uncertainty reported by fit, but is now an uncertainty in the coefficient as used, created only because you used the rounded value, as reported to 4 significant digits.
First, what does one single term from that rational polynomial look like, if we assume that its coefficient is actually unknown? That is, suppose we take the numerator term p(1)*x^4, where p(1) is known only to within a tolerance?
p1error = @(x,p1value) p1value.*x.^4;
fsurf(p1error,[0,5.6,-0.0005,0.0005])
xlabel x
ylabel 'rounding uncertainty'
Do you seee that the coefficent uncertainty (due to rounding) is sufficiently large to start playing havoc with your function? Now add in exactly the same sort of problems with those other terms. For example, we have p2*x^3. And while x^3 is not growing as large as x^4, the uncertainty in p2 is larger, because p2 now effectively lives in the interval [-59.675,-59.665].
p2error = @(x,p2value) p2value.*x.^3;
fsurf(p2error,[0,5.5,-0.005,0.005])
xlabel x
ylabel 'rounding uncertainty'
So again, we have perturbations in the shape of your function that can grow significantly, depending on the value that should have been used instead of the rounded value. When you add up all of the crap that could happen, you should see the resulting curve will start to deviate rather strangely.
I'll just pick some random perturbations to the coefficients.
p = [4.027 -59.67 329.9 -806.9 737.2];
q = [-19.21 142.4 -497.1 776.1 -368.5];
f = @(x,p,q) (p(1)*x.^4 + p(2)*x.^3 + p(3)*x.^2 + p(4)*x + p(5))./ ...
(x.^5 + q(1)*x.^4 + q(2)*x.^3 + q(3)*x.^2 + q(4)*x + q(5));
ppert = p + (rand(1,5) - 0.5).*[1e-4 1e-3 1e-2 1e-2 1e-2]
ppert = 1×5
4.0270 -59.6701 329.9018 -806.9018 737.2007
qpert = q + (rand(1,5) - 0.5).*[1e-3 1e-2 1e-2 1e-2 1e-2]
qpert = 1×5
-19.2096 142.3957 -497.0988 776.0953 -368.4995
fplot(@(x) f(x,ppert,qpert),[1,5.5])
As you should see, while we would round each coefficient in ppert and qpert to the reported values, the resulting curve can have serious problems for large x if you use the subtly wrong coefficients.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!