Curve Fitting LogLog Plot

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])

Answers (2)

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 Comments

OMG such a Genius! I don't even catch a glimpse of how this code is working but many thanks!

Sign in to comment.

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 Comments

I maintain that my code gives a better fit in a loglog plot. For comparison, run first my code, then the first part of Star Strider's code. Then run:
figure(2)
hold on
plot(x,exp(z),'r')
While Star Strider's version may give lower absolute errors for large values of x, the relative errors are significantly lower in my version.
In part 2 of StarStrider's code, the plots are misleading. You should replace LLfit by exp(LLfit) in the plot commands.
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.
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.
.

Sign in to comment.

Categories

Asked:

on 16 Jun 2017

Commented:

on 21 Apr 2022

Community Treasure Hunt

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

Start Hunting!