curve fitting a power function

408 ビュー (過去 30 日間)
Brandon
Brandon 2013 年 3 月 3 日
コメント済み: Image Analyst 2024 年 2 月 1 日
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 日
編集済み: 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.
Venkat Krisshna
Venkat Krisshna 2021 年 4 月 23 日
....what?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by