Solving for Two Equations and Two Unknowns

back to Fan's Intro Math for Econ, Matlab Examples, or MEconTools Repositories
See also: System of Linear Equations
See also: Solving for Two Equations and Two Unknowns
See also: System of Linear Equations, Row Echelon Form

Intersection of two Linear Equations

We have two line:
Where do these lines intersect? Visually, given some values for :
% Symbol
syms x
% Parameters
a = 1.1;
b = 2;
c = 2;
d = -1;
% Define Equations
y1 = a + b*x
y1 = 
y2 = c + d*x
y2 = 
% Solve for analytical solutions using symbolic toolbox
solve_analytical_x = double(solve(y1 - y2 == 0));
solve_analytical_y = double(subs(y1, solve_analytical_x));
% Plot Figure
figure();
hold;
Current plot held
fplot(y1)
fplot(y2)
% Labeling
ylabel('y')
xlabel('x')
grid on;
title({'Intersection of 2 lines'...
,['a=' num2str(a)...
',b=' num2str(b)...
',c=' num2str(c)...
',d=' num2str(d)]...
,['x intersect=',num2str(solve_analytical_x)]...
,['y intersect=',num2str(solve_analytical_y)]});

Linear Equation in Matrix Form

Sometimes we can write down our problem as a set of linear equations. A linear equation is an equation where the unknown variables are multiplied by a set of known constants and then added up to a known constant:
Using matrix algebra, we can express the above equation in matrix form:

Two Linear Equation in Matrix Form

We have two equations above, we can write both of them using the matrix form, given:
We can re-write these as:
We can define these following matrixes to simplify notations:
And the linear system of equations is:

Linsolve: Matlab Matrix Solution for 2 Equations and Two Unknowns

Once you have transformed a system of equations, you can use matlab's linsolve function to solve for the unknowns. As long as the two lines are not parallel to each other, you will be able to find solutions:
W = [1, -b;1, -d]
W = 2×2
1 -2 1 1
v = [a; c]
v = 2×1
1.1000 2.0000
solution = linsolve(W,v)
solution = 2×1
1.7000 0.3000
yIntersection = solution(1,1)
yIntersection = 1.7000
xIntersection = solution(2,1)
xIntersection = 0.3000
The solution here should match the number in title of the graph plotted earlier.
When you do not have matlab, you can solve for the optimal choices using a combination of elementary row operations.
Note: If we used elementary row operations, and arrived at the reduced row echelon form, the analytical solution would be (and this is what linsolve is doing):
% Analytical Results using elementary row operations
yIntersectionEro = a + b*(c-a)/(b-d)
yIntersectionEro = 1.7000
xIntersectionEro = (c-a)/(b-d)
xIntersectionEro = 0.3000