The slope of a line perpendicular to another has a slope that is -1 over the slope of the other line. so
coefficients = polyfit(xKnown, yKnown, 1);
slope = coefficients(1);
Now get the min and max x where you want to draw the line.
[x1p, index1] = min(xKnown);
[x2p, index2] = max(xKnown);
y1p = yKnown(index1);
y2p = yKnown(index2);
perpSlope = -1 / slope;
y1 = perpSlope * (x1p - xKnown(index1)) + y1p
y2 = perpSlope * (x2p - xKnown(index1)) + y1p
line([x1p, x2p], [y1, y2], 'Color', 'r')
It's untested code, so no guarantees.
Best Answer