MATLAB: How to extend a best fit line

best fit linelength

Hi, I divided the datapoints of a fault scarp profile into three datasets for the footwall, scarp slope and hanging wall and plotted the respective best fit lines. What is the easiest way to just extend the length of the best fit line for the scarp so that it crosses the best fit lines of the footwall and the hanging wall?

Best Answer

P1 = rand(2,1) ;
P2 = rand(2,1) ;
% line(P1,P2)
plot([P1(1) P2(1)],[P1(2) P2(2)],'b','linewidth',5)
%%Extend line
m = (P2(2)-P1(2))/(P2(1)-P1(1)) ; % slope of line
N = 100 ;
x = linspace(-2,2,N) ; % specify your x limits
y = P1(2)+m*(x-P1(1)) ; % y = y1+m*(x-x1) ;
hold on
plot(x,y,'.r')