# curve fitting a power function

409 views (last 30 days)

Show older comments

Write a user-defined function that fits data points to a power function of the form y=b*m.^x . Name the function [b,m] = powerfit(x,y), where the input arguments x and y are vectors with the coordinates of the data points, and the output arguments b and m are the constants of the fitted exponential equation. Use powerfit to fit the data below. Make a plot that shows the data points and function x 0.5 2.4 3.2 4.9 6.5 7.8 y 0.8 9.3 37.9 68.2 155 198

First thing I did was open up an mfile and create a script

function [b,m] = powerfit(x,y)

However here is where I get confused I know the outputs much match so the function must be of the form [b,m]=function

But as of now i'm not sure what that function is, so I moved on to the portion I know how to solve.

I then proceeded to fit the polynomial via the poly fit command.

p=polyfit(log(x),log(y),1)

p =

1.4934 1.8864

Here is where I need to use the powerfit function in order to find the coefficients b and m so I can plot the graph. what I would do after achieving b and m would be to solve for the equation using polyval, and plotting the graph.

I appreciate the help

Brandon

##### 6 Comments

Walter Roberson
on 14 Feb 2020

When you have a power model, you can either fit in the original space or in log space.

If you fit in log space, then the problem gets reduced to a linear fit. Linear fits can be done with fairly simple code, with most of the effort being a mean() .

If your values follow the true power model well, then fitting in log space is often a good enough approximation.

If you values are more scattered and you are just trying to find the "least bad" power model of them, then fitting in log space might not give you an accurate enough answer.

At the moment I do not know the non-iterative way to find the coefficients when you need to fit in the original space. There might be one that I do not happen to know. For iterative routines, often the easiest thing to do is fit in log space to get approximate starting parameters and fine tune from there.

### Answers (4)

ChristianW
on 4 Mar 2013

As I said in the comment, I think f(x)=b*x^m is your power function.

x = [0.5 2.4 3.2 4.9 6.5 7.8]';

y = [0.8 9.3 37.9 68.2 155 198]';

plot(x,y,'+r'), hold on

p = polyfit(log(x),log(y),1);

m = p(1);

b = exp(p(2));

ezplot(@(x) b*x.^m,[x(1) x(end)])

Or with curve fitting toolbox:

f = fit(x,y,'b*x^m'); plot(f,'k')

##### 5 Comments

Image Analyst
on 8 May 2021

Image Analyst
on 3 Mar 2013

You don't pass log(x) into the equation.

log(y) = log(b*m.^x) = log(m)*x + log(b)

So you do

coeffs = polyfit(x, log(y), 1);

coeffs (1) is equal to log(m), and coeffs (2) is equal to log(b), from which you can get m and b.

##### 8 Comments

Image Analyst
on 1 Feb 2024

Mona Mahboob Kanafi
on 13 Aug 2013

Edited: Mona Mahboob Kanafi
on 13 Aug 2013

I have the same problem. This method of converting to logarithmic scale and then use polyfit to fit a linear curve to data gives different result with when you fit a power law to the original data. You can test this, with MATLAB cftool. In the first case, it minimized errors which are differences in y; while for the latter case, errors show differences in log(y).

Now, anyone knows how to apply a power law fit[y=a*x^b] to data programatically and not with cftool.

f = fit(x,y,'b*x^m'); plot(f,'k')

This one is so simple and does not give an exact and acceptable result; In cftool it optimizes the starting point and I don't know how to optimize this.

##### 1 Comment

Jan Pospisil
on 20 Aug 2013

hitesh Mehta
on 17 Feb 2018

Edited: Image Analyst
on 17 Feb 2018

Respected Researcher,

My function is

y=a*x^b+c

Can anyone help me that how I can prove mathematically that this one in terms of exponential form?

y=d-g(exp(px.q)-1)-r

d,g,p,q, and r are constants.

##### 2 Comments

Image Analyst
on 17 Feb 2018

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!