MATLAB: Struggling to add the polynomial line

polyfitpolyfitline

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?

Best Answer

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')