cfit object doesn't work with 'plot' or 'differentiate'
19 views (last 30 days)
Show older comments
Hello,
I stored a cfit obj to hard disc. It looks like:
General model:
ans(xd) = off+amp./(1+exp(-a.*(xd-b)))
Coefficients (with 95% confidence bounds):
off = -4.047 (-4.054, -4.041)
amp = 8.179 (8.169, 8.189)
a = 4.053 (4.034, 4.073)
b = 2.271 (2.27, 2.273)
In the process of creating the obj I was able to plot it and to calculate the derivatives.
Now I want to come back to these data and tried:
>>plot(cfObj)
but receive:
Error using cfit/plot (line 111)
Error evaluating CFIT function:
Error while trying to evaluate CFIT model: obj:
Error while trying to evaluate FITTYPE function obj:
Argument must contain a string or function_handle.
For:
>>differntiate(cfObj, x)
I receive:
Error using cfit/feval (line 31)
Error while trying to evaluate CFIT model: fitobj:
Error while trying to evaluate FITTYPE function obj:
Argument must contain a string or function_handle.
Error in cfit/differentiate (line 33)
f1 = feval(fitobj,x+ms);
Can anyone please help out with that? Sure I can recreate the fit from the coefficients, but I think it is not meant to be like that.
Thanks,
Kai
2 Comments
Onno Broekmans
on 1 Jul 2013
Your fit results are definitely not useless. You can use coeffvalues(cfObj) to get the coefficient values from the fit manually, and then plot them using:
fitCoeffs = num2cell(coeffvalues(cfObj));
fitFun = @(off,amp,a,b,xd) off+amp./(1+exp(-a.*(xd-b)));
plot(x, feval(fitFun, fitCoeffs{:}, x, '-r');
(Warning: untested code, so there might be minor syntax errors in there. I'm assuming your x data is in the variable 'x').
Note that this is what plot(cfObj,x) does internally, so the results are exactly the same :)
Accepted Answer
Onno Broekmans
on 1 Jul 2013
I can confirm this behavior (see code to reproduce at the bottom). Unfortunately, it also affects parallel processing: if you try to perform a large number of fits in parallel, using a 'parfor' loop, the resulting cfit objects are "broken" in the way described by Kai.
Unless I've missed something, I'd consider this a bug. I've filed a Service Request (ID: 1-NE8ATH) with TMW. I'll get back to this thread when I hear back.
As a workaround, you could try manually evaluting the fit's anonymous function, and using the coeffvalues() function to retrieve the coefficients from the cfit object (that does work fine).
- Onno
Code to reproduce the issue:
%%Generate some random data, and plot it
data_x = 1:50;
data_y = (2.* data_x) + randn(size(data_x)) + 5;
figure;
plot(data_x, data_y, '.b');
title('Original data');
%%Fit the data using a simple model
model = @(A, B, x) A.*x + B;
ftype = fittype(model);
fopt = fitoptions(ftype);
fopt.StartPoint = [1 1];
f = fit(data_x(:), data_y(:), ftype, fopt);
%%Plot the fit
figure;
hold on;
plot(data_x, data_y, '.b');
plot(f, '-r');
hold off;
title('Fitted data');
%%Store cfit object on disk, and then retrieve it again
save('fit.mat', 'f');
clear('f');
load('fit.mat');
figure;
hold on;
plot(data_x, data_y, '.b');
plot(f, '-r'); % <-- crash here:
% Error using cfit/plot (line 113)
% Error evaluating CFIT function:
% Error while trying to evaluate CFIT model: obj:
% Error while trying to evaluate FITTYPE function obj:
% Argument must contain a string or function_handle.
hold off;
title('Fitted data (fit loaded from disk)');
3 Comments
Onno Broekmans
on 1 Aug 2013
I forgot to post back, but TMW confirmed the issue (early July), and said they had their developers working on it.
Jon Cherrie
on 21 Aug 2013
More Answers (1)
Andrei Bobrov
on 26 Jun 2013
Edited: Andrei Bobrov
on 26 Jun 2013
x = (0:.01:4)'; %
y = -4 + 8./(1+exp(4.*(x - 2))) + .5*randn(numel(x),1); % your data
ftfun = fit(x,y,fittype(@(a,b,c,d,x)a + b./(1+exp(c.*(x - d)))),...
'Startpoint',[1 1 1 1])
dft = differentiate(ftfun,x);
plot(x,[y,ftfun(x),dft])
See Also
Categories
Find more on Fit Postprocessing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!