Curve Fitting LogLog Plot
41 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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])
0 comentarios
Respuestas (2)
Are Mjaavatten
el 16 de Jun. de 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 comentarios
Niklas Kurz
el 17 de Jun. de 2020
OMG such a Genius! I don't even catch a glimpse of how this code is working but many thanks!
Star Strider
el 16 de Jun. de 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 comentarios
José-Luis
el 29 de Ag. de 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
el 18 de Jun. de 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.
The logarithmic transform approach will not.
.
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!