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

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

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.
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"]);
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!
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!!!
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2021년 2월 6일

댓글:

2021년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by