How to create a fit like 1/(x^n)

18 ビュー (過去 30 日間)
Alexander Grimm
Alexander Grimm 2020 年 2 月 28 日
コメント済み: Alexander Grimm 2020 年 3 月 2 日
Hello,
I am trying to fit a curve to three data points. The data points are measurements from a field weakening characteristic of an electric mashine. The characteristic should be direct proportional to 1/(x^n)
datapoint1=[2250 34.4599];
datapoint2=[3500 18.3993];
datapoint3=[6000 8.3306];
% Generate a matrix wiht all datapoints
data=[datapoint1; datapoint2; datapoint3];
xdata=data(:,1);
ydata=data(:,2);
%create the new fittype (n=1)
ft=fittype({'1/(x)'})
% fit the three datapoints with the given fittype
fitobject=fit(data(:,1),data(:,2),ft);
%
% Plot the results
plot(fitobject,data(:,1),data(:,2),'o')
Result for n=1
Result for n =1.4
So my question is: Is it possible to create a fittype like 1/(x^n) and let matlab search for the best solution for n?
I tried
fo = fitoptions('Method','NonlinearLeastSquares', 'Normalize', 'on', 'Robust', 'LAR','StartPoint',[1]);
ft = fittype('C0*x^n','problem','n','options',fo);
from a former question, but I dont know how to handle the problem parameter. Thank you very much for any suggestions :-)
  1 件のコメント
dpb
dpb 2020 年 2 月 28 日
"Generate a matrix wiht all datapoints..."
data=[2250 34.4599; 3500 18.3993; 6000 8.3306];

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

採用された回答

dpb
dpb 2020 年 2 月 28 日
編集済み: dpb 2020 年 2 月 28 日
Of course, the above can be linearized by log transform...
b=polyfit(log10(x),log10(y),1);
>> b(1), 10^b(2)
ans =
-1.4484
ans =
2.4810e+06
>>
b(1)=-b(1); b(2)=10^b(2); % for initial start point in nonlinear model
fo=fitoptions('Method','NonlinearLeastSquares','StartPoint',flip(b));
ft=fittype(@(a,n,x) a./x.^n,'options',fo);
f1=fit(x,y,ft);
>> f1
f1 =
General model:
f1(x) = a./x.^n
Coefficients (with 95% confidence bounds):
a = 2.317e+06 (-1.344e+06, 5.978e+06)
n = 1.44 (1.239, 1.641)
>>
ADDENDUM:
I forgot there is a power model built in...simplest is to use it:
>> fit(x,y,'power1')
ans =
General model Power1:
ans(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 2.24e+06 (-1.159e+06, 5.639e+06)
b = -1.436 (-1.629, -1.243)
>>
Returns slightly different coefficients as it fits x instead of 1/x; I didn't try to compare the two.
You can, of course, pass 1/x if is really important to do it that way...
>> fit(1./x,y,'power1')
ans =
General model Power1:
ans(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 2.24e+06 (-1.159e+06, 5.639e+06)
b = 1.436 (1.243, 1.629)
>>
but it returns same coefficients either way using the internal power model formulation.
  1 件のコメント
Alexander Grimm
Alexander Grimm 2020 年 3 月 2 日
Hello dpd,
thank you very much for your reply. The power model is exactly what i was looking for. I will keep in mind how you generated the starting points for your first suggestion. Maybe i need this for other applications in the future.
The result is amazing :-)
Thank you again and I wish you a nice day!

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

その他の回答 (0 件)

カテゴリ

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