Question

I can successfully display data points on my GUI (built with GUIDE). This is how I do it:

t = t_rec(:);
d_PCr = d_PCr_rec(:);
plot(handles.axes_tau, t, d_PCr, 'wo', 'MarkerFaceColor', [0.5 0.5 0.5]);

But I'd like to overlay a fit on these data points. Here's how I fit the data:

ok_ = isfinite(t) & isfinite(d_PCr);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs', ...
        'Ignoring NaNs and Infs in data' );
end

st_ = [1.1 1.1 0.0945 ];
ft_ = fittype('a+b*[1-exp(-(x-300)/t1)]',...
    'dependent',{'y'},'independent',{'x'},...
    'coefficients',{'a', 'b', 't1'});

cf_ = fit(t(ok_), d_PCr(ok_), ft_, 'Startpoint', st_);

What syntax should I sue to plot this fit on top of my data points?

My axes_tau is already on add.

Was it helpful?

Solution

Assuming you have a 2D fit, it is returned as a cfit object. cfit can be directly evaluated by the feval function. Let's say you want to plot a smoother fit than the original data. You can do something as follows to evaluate the returned fit:

tf_ = [t(1):((t(end)-t(1))/1000):t(end)]; % 1000 will make it smooth and dense. Pick a number that suits you.
yf_ = feval(cf_, tf_); % Evaluate the fit
plot(handles.axes_tau, tf_, yf_, 'k-');

Since the fit object is just a set of coefficients that describes the analytical function you are fitting, you can sample it as coarsely or as finely as you would like. Pick a set of sampling points that give you a smooth looking curve on the screen. In this example, I just made 1001 samples from t(0) to t(end).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top