How to fit curve to see the scaling

조회 수: 2 (최근 30일)
Auryn_
Auryn_ 2019년 1월 9일
댓글: A_V_G 2019년 1월 24일
Hi,
I would like to see the scaling (\alpha) of my curve f(x,y): x^\alpha
Thus, I need to fit my data to a curve but I cannot use the function polyfit as the value of \alpha could be any value between 0 and 2, and I receive the error message:
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Is there any direct method to do this in Matlab?
Thanks in advance!
  댓글 수: 4
Rik
Rik 2019년 1월 9일
What is the exact code that you are currently using? Because you should be fitting your x-y data to something like this
fitfun=@(x,alpha) x.^alpha;
The error doesn't look like you are doing a curve fit.
Auryn_
Auryn_ 2019년 1월 9일
I just did this:
p=polyfit(x,y,1.5);
f1 = polyval(p,x);
plot(x,f1,'r--')
which is obviously wrong.
How can I plot the result of the fitfun function you mentioned?

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2019년 1월 9일
That code doesn't work, because your data is not a polynomial. It doesn't make sense to fit to a polynomial if your data is not. The code below will generate some example data, perform the fit, and plot the result.
In this case you could also make an estimation of alpha by taking the x-base log of y. You'll probably have to do that in a loop.
%generate example data
x=linspace(0,20,30);
noise=(rand(size(x))-0.5)*5;
y=x.^1.7 + noise;
%define function
fitfun=@(alpha,x) x.^alpha;
%perform fit
fitobject = fit(x(:),y(:),fitfun,'StartPoint',1);
fitted_alpha=fitobject.alpha;
%plot in a clean figure
figure(1),clf(1)
plot(x,y,'*b',x,fitfun(fitted_alpha,x),'r')
  댓글 수: 6
Rik
Rik 2019년 1월 24일
The second output of the fit functions returns goodness-of-fit parameters, as you can read in the doc. I expect the sse is closest to what you mean.
A_V_G
A_V_G 2019년 1월 24일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by