Substract fitted values from original data

1 回表示 (過去 30 日間)
Serhii Tetora
Serhii Tetora 2020 年 9 月 29 日
コメント済み: Ameer Hamza 2020 年 9 月 29 日
I want to substract fit values from original data. Here is my code. What is wrong? Why Z and fitresult values are not equal? (see plot)
[xData, yData] = prepareCurveData(xdata,zz);
ft = fittype( 'sin4' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Normalize = 'on';
[fitresult, ~] = fit(xData, yData, ft, opts );
warning('off','curvefit:fit:equationBadlyConditioned')
x = xData;
y = yData;
varnames = coeffnames(fitresult)
varvalues = coeffvalues(fitresult)
for i = 1:length(varnames)
eval([varnames{i},'=',num2str(varvalues(i)),';']);
end
Z = a1*sin(b1*x+c1) +a2*sin(b2*x+c2) +a3*sin(b3*x+c3) + a4*sin(b4*x+c4);
figure
plot(x,y)
hold on
plot(fitresult)
plot(x,Z)
legend('data','fit function','fit exact values')
%%
z_INT = Z_int-Z;

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 29 日
編集済み: Ameer Hamza 2020 年 9 月 29 日
First, eval is evil; avoid it as much as possible: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval . Also, in your case, there is no need, you can directly evaluate fitresult
y_predictions = fitresult(xData)
For the issue in your question. This seems to be a bug in the implementation of fit() function (unless I am overlooking something). For some reason, the fitresult does not report the coefficient value correctly. The issue resolves if you don't pass the opts structure to fit()
[fitresult, ~] = fit(xData, yData, ft);
You may consider filing a bug report: https://www.mathworks.com/support/bugreports/report_bug
  2 件のコメント
Serhii Tetora
Serhii Tetora 2020 年 9 月 29 日
Thanks! It's working fine!
Ameer Hamza
Ameer Hamza 2020 年 9 月 29 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by