View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Power Law, Exponential and Logarithmic Fit

4.7 | 14 ratings Rate this file 119 Downloads (last 30 days) File Size: 20.9 KB File ID: #29545 Version: 1.5
image thumbnail

Power Law, Exponential and Logarithmic Fit



26 Nov 2010 (Updated )

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

| Watch this File

File Information

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

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;

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
29 Nov 2016 JJ_unr

JJ_unr (view profile)

29 Sep 2016 jeetesh jain

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

Comment only
04 Aug 2016 Yetti

Yetti (view profile)

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

05 Oct 2015 Ashley Skeete

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

25 Aug 2015 Nathaniel Howard

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.

21 Apr 2015 Jørgen Grythe

Perfect, exactly what I needed!

08 Feb 2015 Alberto Rodríguez Fernández

Thank you for your function!!

13 Nov 2013 Budi

Budi (view profile)

very usefull,,

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


23 Sep 2013 Saskia

Saskia (view profile)

11 Jul 2013 Franziska

Thanks a lot! Very helpful

23 May 2013 Shea

Shea (view profile)

Just what the Dr. ordered. Thank You.

11 Feb 2013 a

a (view profile)


30 Jan 2012 Shatrughan

Saviour !!!!!!!!!!

06 Jan 2012 Matlab2010


I use:

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


30 Jan 2011 Manas Hardas

Extremely cool!

09 Aug 2013 1.2

Updated to include Mean Squared Error

17 Dec 2013 1.3

fixed 'skipbegin' feature functionality

09 Jan 2014 1.4

Updated to use R2 as 'best fit' criterion rather than MSE

22 Aug 2014 1.5

Added new color option which lets you set the 'color' of both lines and markers with one parameter. Added robustness to NaN values.

Contact us