Solving for Two Equations and Two Unknowns
Intersection of two Linear Equations
We have two line:
Where do these lines intersect? Visually, given some values for : 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));
title({'Intersection of 2 lines'...
,['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:
- for example: , has two unknowns.
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:
- , note the use of bold letter to represent a vector of unknowns, we could have called small x and y, and .
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:
yIntersection = solution(1,1)
xIntersection = solution(2,1)
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