Curve Fitting LogLog Plot

43 vues (au cours des 30 derniers jours)
David Frew
David Frew le 16 Juin 2017
I have a data set that I have created a LogLog plot with and was wondering if there was a way to generate a linear and power trendline for the loglog plot. I have been able to use the curve fitting for the Rectangular scale but cant seem to figure it out for the loglog plot. Here is the data and the graph code for it as well.
x= [ 0.5000 1.0000 2.0000 5.0000 10.0000 20.0000 50.0000 100.0000] y= [ 0.8447 1.4494 3.5760 10.9288 23.1908 44.6963 114.9254 344.6238]
loglog(x,y,'bd') axis([0 100 0 350])

Réponses (2)

Are Mjaavatten
Are Mjaavatten le 16 Juin 2017
You should curve fit the logarithms:
x = [ 0.5000 1.0000 2.0000 5.0000 10.0000 20.0000 50.0000 100.0000];
y = [ 0.8447 1.4494 3.5760 10.9288 23.1908 44.6963 114.9254 344.6238];
loglog(x,y,'bd');
axis([0 100 0 350])
p = polyfit(log(x),log(y),1);
z = polyval(p,log(x));
hold on;loglog(x,exp(z))
  2 commentaires
Niklas Kurz
Niklas Kurz le 17 Juin 2020
OMG such a Genius! I don't even catch a glimpse of how this code is working but many thanks!
Hung-Sheng Huang
Hung-Sheng Huang le 21 Avr 2022
omg

Connectez-vous pour commenter.


Star Strider
Star Strider le 16 Juin 2017
The best approach is to use a power-function fit rather than a log-log fit.
The Code
fit_fcn = @(b,x) x.^b(1) .* exp(b(2)); % Objective Function
RNCF = @(b) norm(y - fit_fcn(b,x)); % Residual Norm Cost Function
B = fminsearch(RNCF, [1; 1]); % Estimate Parameters
xplot = linspace(min(x), max(x));
figure(1)
plot(x, y, 'pg') % Plot Data
hold on
plot(xplot, fit_fcn(B,xplot)) % Plot Fit
hold off
grid
figure(2)
loglog(x, y, 'pg') % Plot Data
hold on
plot(xplot, fit_fcn(B,xplot)) % Plot Fit
hold off
grid
When I tried it, the linear log-log fit using polyfit and polyval was not even an approximate fit. I include that code here:
Bp = polyfit(log(x), log(y), 1); % Linear ‘loglog’ Fit
LLfit = polyval(Bp, log(xplot));
figure(3)
plot(x, y, 'pg') % Plot Data
hold on
plot(xplot, LLfit) % Plot Fit
hold off
grid
figure(4)
loglog(x, y, 'pg') % Plot Data
hold on
plot(xplot, LLfit) % Plot Fit
hold off
grid
Your data are approximately linear, so doing a linear fit without any transformations would likely be appropriate.
  3 commentaires
José-Luis
José-Luis le 29 Août 2017
What " better " means is open to interpretation. Most (some?) fitting functions focus on minimizing the squared residuals.
Thus, if you transform your data beforehand you are not really fitting the same thing.
What's better would then depend of what the objective is.
Squared residuals place undue emphasis on high values. When the values encompass a large range, a log-transform should give a better overall fit.
Just my unsolicited two cents.
Star Strider
Star Strider le 18 Juin 2020
When the values encompass a large range, a log-transform should give a better overall fit.’
Not in this universe.
The log transformation transforms additive errors into mulitplicative errors, and the errors are no longer normally distributed, but lognormally distributed. Since the least squares approach requires that they be normally distributed (and assumes that they are), the ‘better fit’ is simply illusory. The parameters are grossly inaccurate unless the data are absolutely free of noise.
So my nonlinear parameter estimation approach (link) will produce the correct estimated parameters.
The logarithmic transform approach will not.
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by