# Polyfit coeffs weird output

3 ビュー (過去 30 日間)
Chaos 2022 年 12 月 3 日
コメント済み: Chaos 2022 年 12 月 4 日
%Importing Data
%Curve Fitting and finding R^2
[p,s] = polyfit(x1,y,15);
R2 = 1 - (s.normr/norm(y - mean(y)))^2;
m = polyval(p,x1);
% Plotting Original vs Curve Fit
figure(1)
subplot(2,1,1)
plot(x1,y)
xlabel('X')
ylabel('Original')
subplot(2,1,2)
plot(x1,m)
xlabel('X')
ylabel('Fitted') %Extracting the Equation of the Curve-Fit
syms x;
sympref('FloatingPointOutput',true);
i=1;
Throttle = 0;
while i<=length(p) %p is 1x16 double
Throttle = Throttle + p(i)*x^(16-i); % (Coeff)*(input)^(16-i) , 16-i because Coeffs are in decreasing order.
i=i+1;
end
disp(['R-squared = ', num2str(R2)])
disp('Model Eq : Throttle = ')
Throttle
%%
Throttle = - 2.3673e-20*x^15 + 2.5403e-17*x^14 - 1.2612e-14*x^13 + 3.8441e-12*x^12 - 8.0455e-10*x^11 + 1.2250e-07*x^10 - 1.4019e-05*x^9 + 0.0012*x^8 - 0.0830*x^7 + 4.3320*x^6 - 173.0548*x^5 + 5.1978e+03*x^4 - 1.1363e+05*x^3 + 1.7069e+06*x^2 - 1.5755e+07*x + 6.7369e+07 (This is the output in the terminal, Notice how Coeffs have very high " 10^ ")
%%
%%converting x from sym to double and plotting the above equation versus the original imported data "x1". This is bsically the same as subplot(2,1,2) above.%%
figure(2)
thr = double(subs(Throttle,x,x1));
plot(x1,thr) %Testing
%----------------------------
y1 = -2.3673e-20*x1.^15 + 2.5403e-17*x1.^14 - 1.2612e-14*x1.^13 + 3.8441e-12*x1.^12 - 8.0455e-10*x1.^11 + 1.2250e-07*x1.^10 - 1.4019e-05*x1.^9 + 0.0012*x1.^8 - 0.0830*x1.^7 + 4.3320*x1.^6 - 173.0548*x1.^5 + 5.1978e+03*x1.^4 - 1.1363e+05*x1.^3 + 1.7069e+06*x1.^2 - 1.5755e+07*x1 + 6.7369e+07;
figure(3)
plot(x1,y1) %% I literally just copy pasted the Equation from the terminal output and changed ^ to .^ so i can compute it. then i plotted it versus the same original data x1. This time however i got a very weird result. What is wrong?? if i try to use the equation in other applications, i get the results in figure 3. I need the results in Figure 2 though. Are the coeffs wrong? and if so then why Fig2 and subplot(2,1,2) gave a correct output??
%% ##### 2 件のコメント表示非表示 1 件の古いコメント
Chaos 2022 年 12 月 3 日
Code and data are attached now

サインインしてコメントする。

### 採用された回答

John D'Errico 2022 年 12 月 3 日

You don't really need to show anything more than this:
[p,s] = polyfit(x1,y,15);
A degree 15 polynomial. That means the equations will be terribly near to singular. It also means the coefficients will probably vary over many orders of magnitude. Finally, it means that even the TINIEST variation in the value of those coefficients will cause the polynomial to generate complete crap for results. Honestly, I don't even need to see more than the number 15 to know essentially all of that. You CANNOT do this:
y1 = -2.3673e-20*x1.^15 + 2.5403e-17*x1.^14 - 1.2612e-14*x1.^13 + 3.8441e-12*x1.^12 - 8.0455e-10*x1.^11 + 1.2250e-07*x1.^10 - 1.4019e-05*x1.^9 + 0.0012*x1.^8 - 0.0830*x1.^7 + 4.3320*x1.^6 - 173.0548*x1.^5 + 5.1978e+03*x1.^4 - 1.1363e+05*x1.^3 + 1.7069e+06*x1.^2 - 1.5755e+07*x1 + 6.7369e+07;
PERIOD. CRAP will result. You used only 5 significant digits there in each coefficient. GARBAGE. Complete, utter garbage can and probably will result.
Was it an absurdly risky thing, to even try to fit a degree 15 polynomial? Probably, yes. In my opinon, polyfit should have been written to return a warning that the result of such a high degree polynomial should rarely be trusted no matter what. But the problem is, how high a degree will result in crap can vary. I can easily give examples where even cubic polynomials start to show numerical problems. I've seen that happen. Since I don't think you have attached your original data, I can't really go into more depth to show the difference.
##### 6 件のコメント表示非表示 5 件の古いコメント
Chaos 2022 年 12 月 4 日
Got it know! Thank you for everyone who explained it to me!

サインインしてコメントする。

### カテゴリ

Find more on Spline Postprocessing in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!