Curve Fitting LogLog Plot
43 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Are Mjaavatten
il 16 Giu 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 Commenti
Niklas Kurz
il 17 Giu 2020
OMG such a Genius! I don't even catch a glimpse of how this code is working but many thanks!
Star Strider
il 16 Giu 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 Commenti
José-Luis
il 29 Ago 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
il 18 Giu 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.
.
Vedere anche
Categorie
Scopri di più su Get Started with Curve Fitting Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!