cfit object doesn't work with 'plot' or 'differentiate'

조회 수: 10 (최근 30일)
Kai
Kai 2013년 6월 26일
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
Onno Broekmans
Onno Broekmans 2013년 7월 1일
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 :)
Kai
Kai 2013년 7월 1일
Right, that's what I will do. I was only a bit shocked not seeing it come out in the first place. The function I will use the fits in looks like:
F(t) = kE(t)+rE(t)'+uE(t)''+c
So, I see the 'expr' statement growing a bit longer than before...

댓글을 달려면 로그인하십시오.

채택된 답변

Onno Broekmans
Onno Broekmans 2013년 7월 1일
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
Onno Broekmans
Onno Broekmans 2013년 8월 1일
I forgot to post back, but TMW confirmed the issue (early July), and said they had their developers working on it.
Jon Cherrie
Jon Cherrie 2013년 8월 21일
We have published a patch for this issue. See the bug report 968079 for details.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2013년 6월 26일
편집: Andrei Bobrov 2013년 6월 26일
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])
  댓글 수: 1
Kai
Kai 2013년 6월 26일
Hi Andrei,
thanks for your answer. As I see it, you describe how to create a fit object and then use plot and differentiate. This is not really an answer to my question, because I already have the fit objects and know how to use the mentioned functions. The point is that directly after creation I could plot and differentiate them. But now, after a few days, I receive the errors shown above. Weird, I think. Or did I misunderstand your point?
Kai

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by