Power function fitting graph: problem with tiledlayout
이전 댓글 표시
Hello,
I have used the curve fitting tool to fit 3 different sets of data. So I proceeded to make a graph for each of the three sets of data as follows:
C1=[5e-3 2e-4 5e-5 1.25e-5]';
C2=[5e-3 2e-4 5e-5 1.25e-5 3e-6]';
l3070=[1703.125 37875 68893.75 69006.25]';
l7030=[22915.625 49525 42953.125 101265.625 156909.375]';
l8515=[26353.125 36665.625 79937.5 94287.5 89846.875]';
%%
[fit7030,data7030] = fit(C2,l7030,"power2");
figure(2)=plot(fit7030,'k-');
set(figure(2),'lineWidth',3);
hold on
plot(C2,l7030,'r*',MarkerSize=10);
legend('Location', 'northeast','FontSize',16,'NumColumns',1);
legend("Experimental Data","Power Fitting");
ylim([0 160000]);
xlim([0 0.005]);
%export as 600 dpi
filename = 'cf7030.png';
resolution = 600;
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 10 8]);
print(gcf, filename, '-dpng', ['-r', num2str(resolution)]);
hold off
%%
[fit8515,data8515]=fit(C2,l8515,"power2");
figure(3)=plot(fit8515,'k-');
set(figure(3),'lineWidth',3)
hold on
plot(C2,l8515,'r*',MarkerSize=10);
legend('Location', 'northeast','FontSize',16,'NumColumns',1);
legend("Experimental Data","Power Fitting");
ylim([0 160000]);
xlim([0 0.005]);
%export as 600 dpi
filename = 'cf8515.png';
resolution = 600;
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 10 8]);
print(gcf, filename, '-dpng', ['-r', num2str(resolution)]);
hold off
%%
[fit3070,data3070]=fit(C1,l3070,"power2");
figure(4)=plot(fit3070,'k-');
set(figure(4),'lineWidth',3);
hold on
plot(C1,l3070,'r*',MarkerSize=10);
legend('Location', 'northeast','FontSize',16,'NumColumns',1);
legend("Experimental Data","Power Fitting");
ax = gca;
ax.FontSize = 20;
ylim([0 160000]);
xlim([0 0.005]);
%export as 600 dpi
filename = 'cf3070.png';
resolution = 600;
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 10 8]);
print(gcf, filename, '-dpng', ['-r', num2str(resolution)]);
hold off
As you can see, I get a fitted curve that approximates good enough for my needs. It's clear that I get an error for each of the graph, saying that non positive values are treated as NaNs. Although, I cannot seem to find any non positive values in my input data.
The problem arises when I try to make a tiled layout, and display these three graphs in a row. I have tried this:
x=10;
C1=[5e-3 2e-4 5e-5 1.25e-5]';
C2=[5e-3 2e-4 5e-5 1.25e-5 3e-6]';
l3070=[1703.125 37875 68893.75 69006.25]';
l7030=[22915.625 49525 42953.125 101265.625 156909.375]';
l8515=[26353.125 36665.625 79937.5 94287.5 89846.875]';
%%
t=tiledlayout(1,3);
[fit7030,data7030] = fit(C2,l7030,"power2");
nexttile
figure(2)=plot(fit7030,'k-');
set(figure(2),'lineWidth',3);
hold on
plot(C2,l7030,'r*',MarkerSize=10);
legend('Location', 'northeast','FontSize',x-2,'NumColumns',1);
legend("Experimental Data","Line Fitting");
ax = gca;
ax.FontSize = x-2;
ylim([0 160000]);
xlim([0 0.005]);
%%
[fit8515,data8515]=fit(C2,l8515,"power2");
nexttile
figure(3)=plot(fit8515,'k-');
set(figure(3),'lineWidth',3)
hold on
plot(C2,l8515,'r*',MarkerSize=10);
legend('Location', 'northeast','FontSize',x-2,'NumColumns',1);
legend("Experimental Data","Line Fitting");
ax = gca;
ax.FontSize = x-2;
ylim([0 160000]);
xlim([0 0.005]);
hold off
%%
[fit3070,data3070]=fit(C1,l3070,"power2");
nexttile
figure(4)=plot(fit3070,'k-');
set(figure(4),'lineWidth',3);
hold on
plot(C1,l3070,'r*',MarkerSize=10);
legend('Location', 'northeast','FontSize',x-2,'NumColumns',1);
legend("Experimental Data","Line Fitting");
ax = gca;
ax.FontSize = x-2;
ylim([0 160000]);
xlim([0 0.005]);
hold off
%export as 600 dpi
filename = 'tiled.png';
resolution = 600;
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 14 8]);
print(gcf, filename, '-dpng', ['-r', num2str(resolution)]);
As you can see, leaving the huge discrepancies in the legend size (my bad), the real problem is that the curves when using tiledlayout are cut between 0 and 1 on the x-axis. Any idea why this is happening?
Thank you in advance.
Stefano
채택된 답변
추가 답변 (1개)
Emma Farnan
2024년 4월 4일
When you plot a curve fit, it generates a line with 1001 x-points. When you let it autogenerate these x values in the tiledlayout, it chose to make those points from 0 to 1 to match the default axes generated by nexttile. That meant the first point > 0 was 0.001.
You should be able to get around this by making your own linspace of points and evaluating the curve at those points.
C2=[5e-3 2e-4 5e-5 1.25e-5 3e-6]';
l7030=[22915.625 49525 42953.125 101265.625 156909.375]';
[fit7030,data7030] = fit(C2,l7030,"power2");
xfit = linspace(0,0.005,2001); % Generate the x-points to evaluate the fit at
xfit = xfit(2:end); % Cut off the 0 to avoid the warning about power functions and negative values
yfit = fit7030(xfit); % Get the corresponding y values.
% Repeat for other fits
% Use this as the curve fit plot in your tiled layout plots
tiledlayout(1,3);
nexttile;
plot(xfit,yfit,'k-',C2,l7030,'r*',MarkerSize=10)
% Adjust axis as needed and then repeat for other tiles
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






