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

91 vues (au cours des 30 derniers jours)
Alexander Grimm
Alexander Grimm le 28 Fév 2020
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 commentaire
dpb
dpb le 28 Fév 2020
"Generate a matrix wiht all datapoints..."
data=[2250 34.4599; 3500 18.3993; 6000 8.3306];

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 28 Fév 2020
Modifié(e) : dpb le 28 Fév 2020
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 commentaire
Alexander Grimm
Alexander Grimm le 2 Mar 2020
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by