How to create a fit like 1/(x^n)
조회 수: 29 (최근 30일)
이전 댓글 표시
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
2020년 2월 28일
"Generate a matrix wiht all datapoints..."
data=[2250 34.4599; 3500 18.3993; 6000 8.3306];
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!