MATLAB: Finding the equation of a line passing 2 points

equationlinearlinear equationpoints

Hello, I have two points (x1,y1) and (x2,y2). Now I want to find the linear equation of a line passing through these 2 points. The equation must be like f(x)=a*x+b. Is there any function in matlab that accepts coordinates of two points an gives the related linear equation back? If not, I know that a=(y2-y1)/(x2-x1) but what is the short and easy way to find 'b'? Thanks in advance!

Best Answer

Try polyfit:
coefficients = polyfit([x1, x2], [y1, y2], 1);
a = coefficients (1);
b = coefficients (2);
Related Question