[Math] Use Matlab to determine the point of intersection of lines

MATLAB

I want to determine the point of intersection of two lines using Matlab. I have the two lines in the form of

y = tan(theta)*x + c.

I'm seeking an algorithm that does this and I already know how linear equations are solved manually to determine the point of intersection. I would like to know how its done using Matlab.
And also, what must I do if I have to determine the point of intersection for more than two lines using Matlab?

Thanks

Best Answer

You want to solve the system of equations \begin{align} \tan(\theta_1) x - y &= -c_1 ,\\ \tan(\theta_2) x - y &= -c_2 \\ \end{align} for $x$ and $y$. This system can be written in matrix notation as \begin{equation} \underbrace{ \begin{bmatrix} \tan(\theta_1) & -1 \\ \tan(\theta_2) & -1 \end{bmatrix} }_{A} \begin{bmatrix} x \\ y \end{bmatrix} = \underbrace{\begin{bmatrix} -c_1 \\ -c_2 \end{bmatrix}}_{b}. \end{equation} In Matlab, this linear system of equations can be solved with the command

z = A\b;

If you have more than two lines, and you know they all intersect in a single point $(x,y)$, then we will have more equations and $A$ will have more rows (one row for each line). We can find a least squares solution to \begin{equation} A \begin{bmatrix} x \\ y \end{bmatrix} = b, \end{equation} again using the Matlab command z = A\b;

Related Question