I have this code to linear fit data
x = 1:10;
y1 = [1 5 7 8 9 15 16 12 18 20];
P = polyfit(x,y1,1);
slope = P(1)
intercept = P(2)
yfit = P(1)*x+P(2);
And the figure I got from code is attached here
The intercept from figure should be 2.2. But I get the value from code is 0.5.
I didn't understand why the values are different? The code seems correct to me. Any suggestion from anyone?