curve fitting a power function

538 次查看(过去 30 天)
Brandon
Brandon 2013-3-3
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 个评论
Image Analyst
Image Analyst 2020-2-14
Try asking in a forum where C programmers hang out.
Walter Roberson
Walter Roberson 2020-2-14
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.

请先登录,再进行评论。

回答(4 个)

ChristianW
ChristianW 2013-3-4
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 个评论
vedavathi
vedavathi 2021-5-8
how we can get r square and y equations or values
Image Analyst
Image Analyst 2021-5-8
@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
Image Analyst 2013-3-3
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 个评论
Benjamin Lambert
Benjamin Lambert 2024-1-31
y=b*(m^x) is not a power function if domain is x
Image Analyst
Image Analyst 2024-2-1
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
编辑:Mona Mahboob Kanafi 2013-8-13
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 个评论
Jan Pospisil
Jan Pospisil 2013-8-20
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
hitesh Mehta 2018-2-17
编辑:Image Analyst 2018-2-17
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 个评论
Image Analyst
Image Analyst 2018-2-17
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by