Plotting two data sets using the Curve Fitting Tool

Hi,
I was wondering is it possible two fit two different data sets in the same plot? The data sets are scatter plots and I have fitted a 4th polynomial through both sets seen attached. I would like to combine the two plots into one. Is there anyway to do so? I tried using the hold function but it did not work.

댓글 수: 2

dpb
dpb 2020년 8월 22일
편집: dpb 2020년 8월 22일
No can do. It lets you compare fits of different types for a given data set to help in model selection for the one set, but doesn't have the facility to do multiple datasets at the same time.
Just use the command line tools...if you wanted, you could save the desired fits from the tool (remember to save with names so can recognize which is which) and then just evaluate the fits for the final plot.
I'd look carefully at the statistics for the coefficients, though -- a 4th order to those data would make me suspicious are overfitting.
Alright, thank you for the recommemdation. I've plotted the data using another code, however I'm having trouble fitting a line through the points. Is there anyway to include the polynomial fit for the two sets of data?
healthydata = scatter(PIVlabLocation1.VarName38,PIVlabLocation1.VarName39);
hold on
unhealthydata = scatter(G0801location1.AVERAGE,G0801location1.VarName21);
hold off
xlabel ('Y*')
ylabel ('U*')
title('Comparison of healthy and unhealthy data')
legend({'healthydata','unhealthydata'},'Location','northeast')

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

 채택된 답변

dpb
dpb 2020년 8월 22일
편집: dpb 2020년 8월 24일
Well, the old-fashioned way if all you wanted was the coefficients would be to use polyfit and polyval ...but since you have the TB, why not just use fit and get the statistics and builtin behavior automagically?
fithealthy4=fit(PIVlabLocation1.VarName38,PIVlabLocation1.VarName39,'poly4');
fitunhealthy4=fit(G0801location1.AVERAGE,G0801location1.VarName21,'poly4');
hLH=plot(fithealthy4PIVlabLocation1.VarName38,PIVlabLocation1.VarName39);
hold on
hLUH=plot(G0801location1.AVERAGE,G0801location1.VarName21);
set(hLH,{'color'},{'b'}) % healthy blue
set(hLUH,{'color'},{'r'}) % unhealthy red
vals=[{'o';'o'} get([hLH(1) hLUH(1)],{'color'})]; % marker circle, same color as line face color
set([hLH(1) hlUH(1)],{'Marker','MarkerFaceColor'},vals)
xlabel('Y*')
ylabel('U*')
title('Comparison of healthy and unhealthy data')
hLg=legend([hLH(2),hLUH(2)],'Healthy','Unhealthy','Location','northeast');

댓글 수: 8

I tried using your code, however, the data that I want to plot has NaNs (considered to be an outlier) which has resulted in an error. Is there anyway to plot the polyline while excluding the NaNs/outliers? Also I would rather have the scatter plot displayed with the line fit through it rather than just a single polyline.
badhealthy = isnan(PIVlabLocation1.VarName38(:)) | isnan(PIVlabLocation1.VarName39(:));
fithealthy4 = fit(PIVlabLocation1.VarName38(~badhealthy),PIVlabLocation1.VarName39(~badhealthy),'poly4');
Likewise for the unhealthy.
Thank you for your help, I've applied the following code, however is there anyway to display the points as a scatter plot with the polyline going through the points? The plot using the current code is displayed in the first image and I would like to display it like in the second screenshot.
dpb
dpb 2020년 8월 24일
편집: dpb 2020년 8월 24일
The plot routine for a fit object will automagically put both the line and the data points on the plot -- I don't have your data but I wrote the above code using the census data in the MATLAB distribution. The exact code I used is
load census % leaves two variables, cdate, pop (population) in workspace
pop2=sqrt(pop); % just to make up a second variable for illustration
censusfit2=fit(cdate,pop,'poly2'); % fit 2nd order polynomial to pop
popfit2=fit(cdate,pop2,'poly2'); % ditto for the pop2 variable
figure
hL1=plot(censusfit2,cdate,pop); % plot the first data, fit, save line handles
hold on % add to same plot
hL2=plot(popfit2,cdate,pop2); % ditto second fit
set(hL1,{'color'},{'b'}), set(hL2,{'color'},{'r'}) % make health blue, unhealthy red
vals=[{'o';'o'} get([hL1(1) hL2(1)],{'color'})]; % circle marker, color of fit
set([hL1(1); hL2(1)],{'marker','markerfacecolor'},vals) % set the two scatter plots to match
hLg=legend([hL1(2),hL2(2)],'Healthy','Unhealthy','location','best'); % add legend
This results in following figure--
with both the data points and the fitted line. That's the default behavior of the plot routine for a fit object.
As for the NaN in the data; Walter showed one way, a slightly different shown below-
pop(randperm(numel(pop),5))=nan; % introduce some NaN elements in pop vector
isOK=isfinite(pop); % those aren't NaN are finite
popOK=pop(isOK);cdateOK=cdate(isOK); % save the good 'uns...
>> censusfit2=fit(cdate,pop,'poly2'); % prove really are bad apples in the barrel
Error using fit>iFit (line 232)
X, Y and WEIGHTS cannot have NaN values.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
>>
censusfit2=fit(cdateOK,popOK,'poly2'); % only the good die^h^h^hfit young...
figure
hL1OK=plot(censusfit2,cdate,pop); % plot, make same fixups but just do one line...
set(hL1OK,{'color'},{'b'})
set(hL1OK(1),{'marker','markerfacecolor'},{'o','b'})
This results in
which shows the gaps for missing data.
NB: the plot function accepted the original cdate, pop vectors. plot ignores NaN silently but the fit was evaluated over the 1000 or so points used internally to draw a smooth curve for the line so they're not used for it. You'd get the identical plot if passed the two "OK" arrays.
So, would have to see exactly what you did, but unless Mathworks has changed the behavior from that in R2019b, you definitely should see both the fitted line and the scatter plot in the original as shown.
I've applied your code and it works perfectly now, thank you again for your help!
There was still a hL1, hL2 in the first that missed when renamed on the fly to hLH, hLUH in the first answer...hadn't noticed until now.
Sorry to bother again, but is there anyway to extend the polyline beyond the data points? I've attached the current figure as well as how I want the graph to look.
dpb
dpb 2020년 8월 25일
편집: dpb 2020년 8월 25일
Sure -- two ways to approach; both entail adding points to be evaluated over the extended range you want the plot drawn over.
  1. The fit object plot() routine:
newX=[newpoints1 X newpoints2];
hL1=plot(fitObject,newX,[nan(size(newpoints1)) data nan(size(newpoints1))]);
where newpoints1/2 are the locations outside the current range of independent variable you want to add...the X and the observed data vectors have to be same size here so must augment the data vector with NaN to match. As we've noted before, the NaN won't show on the plot.
2. The "traditional" plot() function:
hold on % altho should be already, to remind just in case
newY1=fitObject(newpoints1); % evaluate the fitted curve over
newY2=fitObject(newpoints2]; % the two new ranges wanted to add
hL3=plot(newpoints1,newY1); % add the two new ranges to the existing graph
hL4=plot(newpoints2,newY2); % first point in ranges should be existing line ends
Set linestyle, etc., as desired to match.
What isn't at all clear to me is how you get the second plot -- the range for the first covers the desired x-axis values, but the data points are scaled differently in the second and don't cover that range. Are those completely independent fits of a scaled variable in the second vis a vis the first, I guess?
Also, the first makes sense that the wider bell shape has lower peak and higher tails whereas the second shows the wider (blue) having both wider tails and being (slightly) taller at the peak. That wouldn't seem to normalize correctly to conserve area???
But, I have no idea what any of this is/means, but just looks strange to this dumb ol' nuclear engineer is all :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

태그

질문:

2020년 8월 22일

편집:

dpb
2020년 8월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by