How to combine 2 Fits done with cftool
11 次查看(过去 30 天)
显示 更早的评论
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.
3 个评论
dpb
2020-6-25
Basically, just string the two together -- you'll have to create a second set of graphics handle variables for the two to keep from overwriting the previous for h and the legend strings merged to a single call at the end.
Also call hold on to add the second to the first axis and remove the duplicated label call...
采纳的回答
dpb
2020-6-25
编辑:dpb
2020-6-25
%% 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
3 个评论
dpb
2020-6-25
Different size won't matter...all I see is missed adding the '1' to end of X|YData in first call to fit()...
h(1) = plot( fitresult1, xData1, yData1,'x');
should fix that.
You'll just have to step through and debug...all it's doing is calling fit twice with the two data sets after the preparatory work is done.
The other way to approach it is to take one of the original copies, turn it from a script into a function using generic data arguments and then call that function with the two data sets in turn. Then you could rearrange to return the fit results and plot them together instead of asking the fitting routine to also do the plotting...or not, your choice.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!