MATLAB: How to combine 2 Fits done with cftool

cftool

I know right there's kind of an article in matlab concering this topic, but I don't grasp it. So here again, I set up 2 fits and "generated code" so there are 2 .m files with the fit including. How do I combine them? I tried to create 2 fits in the toolbox and generated code afterwards, but it still plots 2 seperate plots. Would be great if you could help me out.

Best Answer

%% Fit: 'untitled fit 1'.
[xData1, tmp] = prepareCurveData( Fqr, Prem );
yData1 = tmp+pi/2;
% Set up fittype and options.
% Type is same for both so only need one object initially
ft = fittype( 'a*atan(2*b*t/3.83^2-t^2)+pi/2', 'independent', 't', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
% Will change the start point between two -- may or may not matter...
opts.StartPoint = [0.0496544303257421 0.902716109915281];
% Fit model to data.
% Make a first variable set unique
[fitresult1, gof1] = fit( xData, yData, ft, opts );
% Plot fit with data.
%figure( 'Name', 'untitled fit 1' ); % let default handle it...
h(1) = plot( fitresult1, xData1, yData1,'x');
% NOW DATA FOR SECOND
[xData2, tmp] = prepareCurveData( Fq2r, Prem2 );
yData2 = tmp+pi/2;
% and the other start points, fit...
opts.StartPoint = [0.425259320214135 0.312718886820616];
[fitresult2, gof2] = fit( xData2, yData2, ft, opts );
hold on % add to existing plot
h(2) = plot( fitresult2, xData2, yData2 );
% set both line handles in h array...
set(h,'LineWidth',2,'Markersize',7.5)
grid on
legend({'Phasenverschiebung','Fit: $(-1,02 \pm 0,07) \cdot \tan^{-1}(\frac{2\cdot (27,59 \pm 0,27)\cdot \omega}{3,83^2-\omega^2})$'}, ...
{'Phasenverschiebung bei $I = 0,25 A$','Fit: $(-1,26 \pm 0,20) \cdot \tan^{-1}(\frac{2\cdot (26,59 \pm 0,65)\cdot \omega}{3,83^2-\omega^2})$'}, ...
'Interpreter','latex','FontSize',14)
xlabel({'$\omega$ in rad'},'Interpreter','latex','FontSize',14)
ylabel({'Phasenverschiebung $\Delta \varphi$ in rad'},'Interpreter','latex','FontSize',14)
is a first cut