MATLAB: I am planning to 3 sepearate fit line in a graph. But I encounter a problem. How to solve this

fit linegraph

clear all
close all
clc
hold on
temp=[30.83600 30.13100 29.33600 27.90400 27.07000 26.31200 25.04400 24.21000 23.38900];
point=[1 2 3 4 5 6 7 8 9];
plot(point,temp,'x');
p1 = polyfit(point(1:3),temp(1:3),1);
f1 = polyval(p1,temp(1:3));
p2 = polyfit(point(4:6),temp(4:6),1);
f2 = polyval(p2,temp(4:6));
p3 = polyfit(point(7:9),temp(7:9),1);
f3 = polyval(p2,temp(7:9));
plot(f1,temp(1:3))
plot(f2,temp(4:6))
plot(f3,temp(7:9))

Best Answer

temp=[30.83600 30.13100 29.33600 27.90400 27.07000 26.31200 25.04400 24.21000 23.38900];
point=[1 2 3 4 5 6 7 8 9];
plot(point,temp,'x');
hold on
xx=linspace(1,3,1000)
yy=interp1(point(1:3),temp(1:3),xx,'linear')
plot(xx,yy,'r')
xx=linspace(4,6,1000)
yy=interp1(point(4:6),temp(4:6),xx,'linear')
plot(xx,yy,'g')
xx=linspace(7,9,1000)
yy=interp1(point(7:9),temp(7:9),xx,'linear')
plot(xx,yy,'b')