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 comentario

James Tursa
James Tursa el 8 de Feb. de 2021
Please post code as normal text highlighted by the CODE button. We can't copy & run images.

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 6 de Feb. de 2021

0 votos

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 comentarios

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"]);
Star Strider
Star Strider el 8 de Feb. de 2021
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 el 8 de Feb. de 2021
Thanks.
Is there a way to convert the coeffs easily?
I get:
Star Strider
Star Strider el 8 de Feb. de 2021
Editada: Star Strider el 8 de Feb. de 2021
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 el 9 de Feb. de 2021
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 el 9 de Feb. de 2021
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 el 9 de Feb. de 2021
It is awsome!!!
Star Strider
Star Strider el 9 de Feb. de 2021
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 6 de Feb. de 2021

Comentada:

el 9 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by