# curve fitting a power function

273 views (last 30 days)
Brandon on 3 Mar 2013
Commented: Image Analyst on 1 Feb 2024
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 CommentsShow 4 older commentsHide 4 older comments
Image Analyst on 14 Feb 2020
Try asking in a forum where C programmers hang out.
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.

Sign in to comment.

### 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 CommentsShow 3 older commentsHide 3 older comments
vedavathi on 8 May 2021
how we can get r square and y equations or values
Image Analyst on 8 May 2021
@vedavathi, you can do it the way I showed in my answer below. You can get the equation. To get R squared, compute the difference between fitted values and actual values as usual.

Sign in to comment.

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 CommentsShow 6 older commentsHide 6 older comments
Benjamin Lambert on 31 Jan 2024
y=b*(m^x) is not a power function if domain is x
Image Analyst on 1 Feb 2024
Well whatever you or he wants to call it, y=b*(m^x) is what he asked for. And later he says that he's supposed to get a "fitted exponential equation" (not a polynomial) meaning that the x is in the exponent like he showed. However he says to solve it by taking the log of x and y and using polyfit to fit a line. While you can do it that way, I don't know how accurate it would be compared to using a non-linear fit right on the original equation, like I do in my attached demos of fitnlm

Sign in to comment.

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 CommentShow -1 older commentsHide -1 older comments
Jan Pospisil on 20 Aug 2013
In cftool, you can choose file->generate code and the result shows that cftool actually uses fit(), in this particular example it uses fit(x,y,'power1'). You may set the starting point using fitoptions, but as far as I know, there is no optimization in choosing the proper starting point. If it is not specified, random starting point is chosen.

Sign in to comment.

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 CommentsShow NoneHide None
Image Analyst on 17 Feb 2018
We don't understand your question, or even its grammar. Are you trying to prove that y equals one (y=1)??? I have no idea what you want to prove. Read this link and try again to explain.
Venkat Krisshna on 23 Apr 2021
....what?

Sign in to comment.

### Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!