Matlab 2018a fit function wrong coefficients

Hi,
I tried to use the fit function for a rational fitting (rat24) and I got very strange results although the fitted curve was similar to my function.
So I tried to use it with a known function: y=2t and did the fitting and the output again was wrong.
So in the following example I expected p1=0 and p2=2 and I get different results.
What am I doing wrong?
Thanks,
Avihai

1 件のコメント

James Tursa
James Tursa 2021 年 2 月 8 日
Please post code as normal text highlighted by the CODE button. We can't copy & run images.

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

 採用された回答

Star Strider
Star Strider 2021 年 2 月 6 日

0 投票

The 'Normalize','on' name-value pair tells the function to centre and scale the data. This is appropriate for some situations where the independent variable does not begin at 0, and the dependent variable is much greater than 0. (To then plot it, that information must be included or the result will be significantly different from that expected.) Changing to 'Normalize','off', will produce the anticipated result for the parameter estimates.

9 件のコメント

Steven Lord
Steven Lord 2021 年 2 月 6 日
If you look in the description of the fit result there's a line that says "when x is normalized by ..."
If we look at the equation of the fit taking the normalization into account:
syms x
normalizedX = (x-0.5)/0.2891
normalizedX = 
yInTermsOfUnnormalizedX = 0.5782*normalizedX + 1
yInTermsOfUnnormalizedX = 
So it's giving you the correct coordinates, just in a different form than you expected.
avihai
avihai 2021 年 2 月 8 日
OK, I understand.
So I followed your suggestion, turned off the normalization and applied on a rat24 case (one of my iterations) and got something absolutely wrong:
using this code (you can check there that p and q are completely different):
m=1
R = [1];
L = [5.6]*1e-3;
C = [5.6]*1e-6;
p(m,:) = [(R(m)/(L(m)^2)), 0, 0];
q(m,:) = [1, 0, (1/(L(m)*C(m))^2)*((R(m)*C(m))^2-2*L(m)*C(m)),0,1/(L(m)*C(m))^2];
f=linspace(700,1100,1000);
w = 2*pi*f;
Yr(m,:) = (p(1)*w.^2)./(q(1)*w.^4+q(2)*w.^3+q(3)*w.^2+q(4)*w+q(5));
fit_out = fit(f',Yr','rat24','Normalize','off','Robust','Bisquare')
figure;
plot(fit_out,f,Yr)
legend(["original", "fitted"]);
Star Strider
Star Strider 2021 年 2 月 8 日
Note that in this instance, ‘x’ begins at 700 and ends at 1100. These data need to be centred and scaled.
Unlike the first example, in this one it would likely be best to have 'Normalize','on'!
avihai
avihai 2021 年 2 月 8 日
Thanks.
Is there a way to convert the coeffs easily?
I get:
Star Strider
Star Strider 2021 年 2 月 8 日
編集済み: Star Strider 2021 年 2 月 8 日
My pleasure!
Is there a way to convert the coeffs easily?
Not really. An alternative option is what I used to fit it:
fcn = @(b,x) (b(1).*x.^2 + b(2).*x + b(3)) ./ (x.^4 +b(4).*x.^3 + b(5).*x.^2 + b(6).*x + b(7));
B = fminsearch(@(b) norm(fcn(b,f) - Yr), [p,q]');
producing:
B =
302.955044139564
-2.17818972069488
1.94095942698736
-1348.05118232412
6.56586561370005
363190118.272236
4.27839390124021
9.28737158499735e+17
The ‘B’ vector corresponds to the ‘b’ coefficients in ‘fcn’.
(Note that this nonlinear approach does not require centreing and scaling.)
EDIT — (8 Feb 2021 at 15:24)
The plot is then:
figure
plot(f, Yr)
hold on
plot(f, fcn(B,f))
hold off
legend(["original", "fitted"])
.
avihai
avihai 2021 年 2 月 9 日
This is a great solution! I needed a way to fit to a custom function and having the ability to enter my own function is ideal.
Thank you!
Star Strider
Star Strider 2021 年 2 月 9 日
As always, my pleasure!
If you want confidence intervals on the parameters and other statistics on the fit itself, use the same objective function (‘fcn’ in my code) with fitnlm, and see NonLinearModel for more options.
avihai
avihai 2021 年 2 月 9 日
It is awsome!!!
Star Strider
Star Strider 2021 年 2 月 9 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFit Postprocessing についてさらに検索

製品

リリース

R2018a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by