MATLAB: Doing polyvar giving two conditions

conditionspolyvar

i have a matrix xx(100*1) of elements less than and greater than 70 and a y matrix.p1=polyfit(xx,y,1)when xx<70 and p2=polyfit(xx,y,1);when xx>70 . i need to polyvar(p1,xx)for xx<70 and polyvar(p2,xx)for xx>70 and get the two condition as a single matrix. i tried with if loop but getting answer only for the else condition.is it possible to get answer in both conditions?

Best Answer

See if this does what you want:
xx = linspace(1, 140); % Create Data

y = randi(99, 1, 100); % Create Data
p1 = polyfit(xx(xx<70), y(xx<70), 1);
p2 = polyfit(xx(xx>=70), y(xx>=70), 1);
y1 = polyval(p1,xx(xx<70));
y2 = polyval(p2,xx(xx>=70));
figure(1)
plot(xx(xx<70), y1, xx(xx<70),y(xx<70))
hold on
plot(xx(xx>=70), y2, xx(xx>=70),y(xx>=70))
hold off
grid
Related Question