# Plot polyfit R-squared

조회 수: 30 (최근 30일)
gorilla3 2019년 8월 23일
댓글: dpb 2019년 8월 23일
Hi guys,
could you please help me figure out why the polyfit works in one figure but not in the other? I don'T understand why it's not plotting the full line in the second figure.
Thank you!
coeffs = polyfit(d_rel_mM, dil_rel, 1);
fittedX = linspace(min(d_rel_mM), max(d_rel_mM), 200);
fittedY = polyval(coeffs, fittedX);
coeffs_a = polyfit(d_rel_mM, time_a, 1);
fittedXA = linspace(min(time_a), max(time_a), 200);
fittedYA = polyval(coeffs_a, fittedXA);
figure(1)
plot(d_rel_mM,dil_rel,'x','Markersize',15, 'Linewidth',6)
xlabel('Imaging depth [\mum]', 'Fontname','LM Roman 10','Fontsize', 40)
ylabel('Peak dilation [%]','Fontname','LM Roman 10','Fontsize', 40)
set(gca,'Fontname','LM Roman 10','FontSize',20,'box','off')
axis tight
hold on;
plot(fittedX, fittedY, 'k-', 'LineWidth', 2);
%%
figure(3) %% INCOMPLETE LINE, SEE ATTACHED
plot(d_rel_mM,time_a,'x','Markersize',15, 'Linewidth',6)
xlabel('Imaging depth [\mum]', 'Fontname','LM Roman 10','Fontsize', 40)
ylabel('Time constant 1/\alpha [s]','Fontname','LM Roman 10','Fontsize', 40)
set(gca,'Fontname','LM Roman 10','FontSize',20,'box','off')
axis tight
hold on;
plot(fittedXA, fittedYA, 'k-', 'LineWidth', 3);

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

### 채택된 답변

dpb 2019년 8월 23일
Because
>> fittedXA(1),fittedXA(end)
ans =
10
ans =
20
>>
and that's what you plotted as the abscissa for some reason whereas the data are plotted versus ImagingDepth which has range of
>> min(d_rel_mM),max(d_rel_mM)
ans =
0
ans =
345.4000
>>
You've mixed up x and y for the prediction line evaluation...use
coeffs_a = polyfit(d_rel_mM, time_a, 1);
fittedXA = linspace(min(d_rel_mM), max(d_rel_mM), 200);
fittedYA = polyval(coeffs_a, fittedXA);
and all will be well...
##### 댓글 수: 2없음 표시없음 숨기기
gorilla3 2019년 8월 23일
Ahhh wonderful! Thank you :)
dpb 2019년 8월 23일
Of course, you DO realize that since is a first order fit, you only need two points to draw the line, right? :)

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

### 카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by