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;
Excellent! Thank you.
Sir I want to find the value of a and b so plz suggest the code for this curve
@ashley, the b is the slope output. exp(intercept) gives a.
Great stuff. Do you know of a way to input powerlaw data and to determine the b in y = a x^b ?
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.
Perfect, exactly what I needed!
Thank you for your function!!
by the way, how to make non linear curve fit using custom equation when plot on log scale?
Thanks a lot! Very helpful
Just what the Dr. ordered. Thank You.
% y = a x^b
function [a b] = my_powerLawFit(y, x)
Added new color option which lets you set the 'color' of both lines and markers with one parameter. Added robustness to NaN values.
Updated to use R2 as 'best fit' criterion rather than MSE
fixed 'skipbegin' feature functionality
Updated to include Mean Squared Error
View the winning live scripts from faculty and students who participated in the recent challenge.Learn more
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.