-
Notifications
You must be signed in to change notification settings - Fork 20
/
matrix_linear_system_2.m
113 lines (110 loc) · 4.04 KB
/
matrix_linear_system_2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%% Solving for Two Equations and Two Unknowns
% *back to* <https://fanwangecon.github.io *Fan*>*'s* <https://fanwangecon.github.io/Math4Econ/
% *Intro Math for Econ*>*,* <https://fanwangecon.github.io/M4Econ/ *Matlab Examples*>*,
% or* <https://fanwangecon.github.io/MEconTools/ *MEconTools*> *Repositories*
%%
% _See also_: <https://fanwangecon.github.io/Math4Econ/matrix_system_of_equations/matrix_linear_equations.html
% System of Linear Equations>
%
% _See also_: <https://fanwangecon.github.io/Math4Econ/matrix_system_of_equations/matrix_linear_system_2.html
% Solving for Two Equations and Two Unknowns>
%
% _See also_: <https://fanwangecon.github.io/Math4Econ/matrix_system_of_equations/matrix_row_echelon_form.html
% System of Linear Equations, Row Echelon Form>
%% Intersection of two Linear Equations
% We have two line:
%
% $$\begin{cases} y = a + b \cdot x \\ y = c + d \cdot x \end{cases}$$
%
% Where do these lines intersect? Visually, given some values for $a,b,c,d$:
% Symbol
syms x
% Parameters
a = 1.1;
b = 2;
c = 2;
d = -1;
% Define Equations
y1 = a + b*x
y2 = c + d*x
% 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;
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:
%%
% * for example: $-2\cdot x + \cdot y = 1$, has two unknowns.
%%
% Using matrix algebra, we can express the above equation in matrix form:
%%
% * $\left[ {\begin{array}{cc}-2 & 1 \\\end{array} } \right] \cdot \left[ {\begin{array}{c}x
% \\ y \end{array} } \right] = -2\cdot x + 1 \cdot y = 1$
%% Two Linear Equation in Matrix Form
% We have two equations above, we can write both of them using the matrix form,
% given:
%%
% * $\begin{cases} y = a + b \cdot x \\ y = c + d \cdot x \end{cases}$
%%
% We can re-write these as:
%%
% * $\left[ {\begin{array}{cc} 1 & -b \\ 1 & -d \\ \end{array} } \right] \cdot
% \left[ {\begin{array}{c} y \\ x \end{array} } \right] = \left[ {\begin{array}{cc}
% 1\cdot y - b \cdot x \\ 1\cdot y - d \cdot x \\ \end{array} } \right] = \left[
% {\begin{array}{c} a \\ c \\ \end{array} } \right]$
%%
% We can define these following matrixes to simplify notations:
%%
% * $W = \left[ {\begin{array}{cc} 1 & -b \\ 1 & -d \\ \end{array} } \right]$
% * $\mathbf{X} = \left[ {\begin{array}{c}x \\ y \end{array} } \right]$, note
% the use of bold letter to represent a vector of unknowns, we could have called
% small $x$ and $y$, $x_1$ and $x_2$.
% * $v = \left[ {\begin{array}{c} a \\ c \\ \end{array} } \right]$
%%
% And the linear system of equations is:
%%
% * $W\cdot\mathbf{X} = v$
%% 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]
v = [a; c]
solution = linsolve(W,v)
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)
xIntersectionEro = (c-a)/(b-d)
%%
%
%
%
%
%