How to use fittype and fit to get a logarithmic fit to some data

조회 수: 105 (최근 30일)
Mark
Mark 2012년 9월 7일
Hi, I am trying to get a logarithmic fit to some data I have. To do this, I am currently attempting to use the 'fittype' and 'fit' functions to get the fit, but I keep getting errors. Any help or suggestions would be helpful, including if you suggest using a different method to get the fit. My code and error messages are listed below:
data = fopen('ABC_Domain.txt');
time = fopen('Time_File.txt');
y = fscanf(data, '%f');
x = fscanf(time, '%f');
myfittype = fittype('a+b*log(x)');
[a,b] = fit(x,y, 'logfit')
fclose(data);
I get errors:
??? Error using ==> fittype.fittype>iCreateFromLibrary at 385 Library function mytype not found.
Error in ==> fittype.fittype>fittype.fittype at 311 obj = iCreateFromLibrary( obj, varargin{:} );
Error in ==> fit at 152 model = fittype( fittypeobj, 'numindep', size( xdatain, 2 ) );
Error in ==> ABC_Model_Extension at 12 [a,b] = fit(x,y, 'mytype');
Again, any help would be much appreciated.

답변 (2개)

Kevin Claytor
Kevin Claytor 2012년 9월 7일
You may need to supply additional parameters to the fyttype object, otherwise it doesn't know what variables it can vary, and which is the independent variable. Also in your code above, you don't actually use the fit you just created. Here's an example;
x = linspace(1,100)
y = 5 + 7*log(x);
myfit = fittype('a + b*log(x)',...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a','b'});
fit(x',y',myfit)

Tom Lane
Tom Lane 2012년 9월 9일
If you want to fit y as a linear function of log(x), you can just apply linear methods. For example, here's how to use backslash:
>> x = rand(20,1);
>> y = 2 - log(x);
>> [ones(size(x)),log(x)]\y
ans =
2.0000
-1.0000
You could also use the fit command with a poly1 fit by supplying log(x) as the predictor.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by