curve fitting a power function
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
ChristianW
on 3 Mar 2013
Is your power function correct? It's not f(x) = b*x^m ?
shree patnaik
on 28 Dec 2017
yes, It is a power fit only. when the variable like 'x' or 'y' goes on the power, it will convert into an exponential fit.
Image Analyst
on 28 Dec 2017
Shree, you're answering a four year old question that Christian did not even ask you. If you have a similar question with some particular model in mind, then please post that as a new discussion thread.
Yukta Patil
on 14 Feb 2020
How to do this in C program?
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.
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
Ahmed Abdalazeez
on 11 Oct 2019
how to find a goodness of the fitting curve
Walter Roberson
on 12 Oct 2019
If you use the Curve Fitting Toolbox, then you can return goodness of fit information in the second output,
[f, gof_info] = fit(x,y,'b*x^m');
vedavathi
on 8 May 2021
how we can get equations
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.
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
Brandon
on 3 Mar 2013
Image Analyst
on 3 Mar 2013
Edited: Image Analyst
on 31 Jan 2024
Yes. I'm pretty sure. I proved it with the math. Can your book do that? I doubt it. Look at my solution and you'll see that the regression curve pretty much goes through your data points as expected. Show me some math where you prove that you need to take the log of x, because I'm just not seeing it. Did you know that log(m^x) = x * log(m)? It does NOT equal log(x) * log(m) - if your book says that it's wrong. See Rule #4 here.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
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, 'bs', 'LineWidth', 4);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% log(y) = log(b*m.^x) = log(b) + log(m.^x) =
% log(y) = log(b) + x * log(m).
% So finally
% log(y) = log(m) * x + log(b)
% Renaming
% y1 = a1 * x + a2
% where y1 = log(y), a1 = log(m), and a2 = log(b)
% So you do
coeffs = polyfit(x, log(y), 1);
% Get the estimate for log(y)
fittedX = linspace(x(1), x(end), 100);
fittedLogY = polyval(coeffs, fittedX);
% Now get y from fittedLogY.
fittedY = exp(fittedLogY)
hold on;
plot(fittedX, fittedY, 'r-', 'LineWidth', 4);
title('Power Law Fit', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Now get b and m
b = exp(coeffs(2))
m = exp(coeffs(1))
% Get y fitted another way
fittedY2 = b * m .^ fittedX
% Note: fittedY2 will be exactly the same as fittedY
plot(fittedX, fittedY2, 'kd', 'MarkerSize', 13);

Walter Roberson
on 12 May 2016
Abhinaysh Jin comments
U are wrong with the question it is b*x^m not b*m^x
Walter Roberson
on 12 May 2016
Abhinaysh Jin: b*m^x is what the person who asked the question used.
Benjamin Lambert
on 31 Jan 2024
Yeah this is very wrong. Wasted some time staring at this
Image Analyst
on 31 Jan 2024
@Benjamin Lambert not sure what/who you're saying is wrong - me or Abhinaysh Jin.
Abhinaysh Jin said I was wrong but @Walter Roberson confirmed me by saying that the original poster really did say he wanted b*(m^x), not b*x^m like Abhinaysh Jin said. Hopefully you didn't waste time staring at my code because it's correct -- at least it uses the formula the original poster said to use (I assume he didn't make a mistake because I can't go by anything else).
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
Mona Mahboob Kanafi
on 13 Aug 2013
Edited: Mona Mahboob Kanafi
on 13 Aug 2013
0 votes
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
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.
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
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?
Categories
Find more on Get Started with Curve Fitting Toolbox 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!