File Exchange

Power Law, Exponential and Logarithmic Fit

version 1.5.0.0 (20.9 KB) by Jonathan C. Lansey

Jonathan C. Lansey (view profile)

Finds and plots the linear fit to some data points when plotted on a log scale.

Updated 22 Aug 2014

logfit(X,Y,graphType), where X is a vector and Y is a vector or a
matrix will plot the data with the axis scaling determined
by graphType as follows: graphType-> xscale, yscale
loglog-> log, log
logx -> log, linear
logy -> linear, log
linear -> linear, linear
A line is then fit to the scaled data in a least squares
sense.
See the 'notes' section below for help choosing a method.
logfit(X,Y), will search through all the possible axis scalings and
finish with the one that incurs the least error (with error
measured as least squares on the linear-linear data.)

Notes:
A power law relationship
[slope, intercept] = logfit(x,y,'loglog');
yApprox = (10^intercept)*x.^(slope);

An exponential relationship
[slope, intercept] = logfit(x,y,'logy');
yApprox = (10^intercept)*(10^slope).^x;

A logarithmic relationship
[slope, intercept] = logfit(x,y,'logx');
yApprox = (intercept)+(slope)*log10(x);

A linear relationship
[slope, intercept] = logfit(x,y,'linear');
yApprox = (intercept)+(slope)*x;

Pim Goedhart

Oriol Bertran

Ivan Rojkov

M. Tilmans

Evan Young

DuRK

DuRK (view profile)

Arislan Makhmudov

Athos Garcia

Zhida Li

Nicholas Herrick

Nicholas Herrick (view profile)

Excellent! Thank you.

Ruijia Wang

JJ_unr

jeetesh jain

jeetesh jain (view profile)

Sir I want to find the value of a and b so plz suggest the code for this curve

Yetti

Yetti (view profile)

@ashley, the b is the slope output. exp(intercept) gives a.

Ashley Skeete

Ashley Skeete (view profile)

Great stuff. Do you know of a way to input powerlaw data and to determine the b in y = a x^b ?

Nathaniel Howard

Nathaniel Howard (view profile)

Jonathan C. Lansey, you sir are a legend!
Many thanks for this function.
Would the be any way to extend the fitted line to meet x-axis at all?
Its not vital, merely for display purposes.
Many thanks anyway.
Nath

Jørgen Grythe

Jørgen Grythe (view profile)

Perfect, exactly what I needed!

Alberto Rodríguez Fernández

Alberto Rodríguez Fernández (view profile)

Great!
Thank you for your function!!

Budi

Budi (view profile)

very usefull,,

by the way, how to make non linear curve fit using custom equation when plot on log scale?

thx

Franziska

Franziska (view profile)

Thanks a lot! Very helpful

Shea

Shea (view profile)

Just what the Dr. ordered. Thank You.

a

Awesome!

Shatrughan

Shatrughan (view profile)

Saviour !!!!!!!!!!

Matlab2010

Matlab2010 (view profile)

Nice.

I use:

% y = a x^b
function [a b] = my_powerLawFit(y, x)

logx=log(x);
logy=log(y);
p=polyfit(logx,logy,1);
b=p(1);
loga=p(2);
a=exp(loga);
end

Manas Hardas

Extremely cool!