# Struggling to add the polynomial line

조회 수: 2 (최근 30일)
emily bristow 2020년 12월 18일
댓글: emily bristow 2020년 12월 19일
I've managed to use ployfit(X,Y,1) to get the two readings but I'm unsure on how to add the line and caption to the actual plot.
Any help?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
David Goodmanson 2020년 12월 18일
편집: David Goodmanson 2020년 12월 18일
Hi emily,
I take it you want to add the fitting straight line to the plot. The output of polyfit is a two-component vector P. Then for an array variable x (which could be the same as X) the fitting line is just y = polyval(P,x). Then you can do, for example
plot(X,Y,x,y)

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

### 채택된 답변

KSSV 2020년 12월 18일
n = 10 ;
x = 1:n ;
y = rand(size(x)) ;
p = polyfit(x,y,1) ;
yi = polyval(p,x) ;
figure
hold on
plot(x,y,'*r')
plot(x,yi,'b')
legend('Original Points','Fitted Line')
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
David Goodmanson 2020년 12월 18일
Hi emily,
in case you don't want the equation in the title, take a look at the 'text' command.
Also, I have to disagree with KSSV on one detail. I think
plot(x,y,'*r',x,yi,'b')
is a better way to do things, partly because you don't have to use 'hold on'. And if you use 'hold on' and don't use 'hold off' after the plot commands, it can lead to problems later.
emily bristow 2020년 12월 19일
Thankyou!!!

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

### 카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by