% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%=====================================
% find the intersection of two lines
% (a line is described as y=m*x+b)
%=====================================
clear all
close all
clc
format long

% parameter for line #1
m1 = 5;
b1 = -3;

% parameter for line #2
m2 = -0.3;
b2 = 7;

% translate the m's and b's into matrix formalism
a11 = -m1;
a12 = 1;
a21 = -m2;
a22 = 1;
A = [a11, a12; a21, a22];
b = [b1; b2];            % NOTE: b has to be a column vector!

% solve for intersection where the two lines cross
xcross_method1 = mldivide(A,b)
xcross_method2 = A\b
xcross_method3 = inv(A)*b

% visualise the result of each method by plotting a single point 
plot(xcross_method1(1),xcross_method1(2),'x'); hold on
plot(xcross_method2(1),xcross_method2(2),'o')
plot(xcross_method3(1),xcross_method3(2),'+')

% plot the two lines (use a reasonable range that includes the crossing point!)
x = linspace(xcross_method1(1)-2,xcross_method1(1)+2,100);

y = m1*x+b1;
plot(x,y,'b')

y = m2*x+b2;
plot(x,y,'g')

xlabel('x')
ylabel('y')







