8_Programming with MATLAB[1]
8_Programming with MATLAB[1]
8_Programming with MATLAB[1]
ANKARA UNIVERSITY
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
LINEAR ALGEBRAIC EQUATIONS
2
LINEAR ALGEBRAIC EQUATIONS
For example,
The equations
6x – 10y = 2
3x – 4y = 5
have graphs that intersect at the
solution: y = 4, x = 7.
3
LINEAR ALGEBRAIC EQUATIONS
Another Example:
3x - 4y = 5
6x - 8y = 3
It has no solution.
The graphs of these two equations are distinct
but parallel.
They do not intersect No solution exists.
4
LINEAR ALGEBRAIC EQUATIONS
◾ For 2 Equations and 2 Unknowns, the solution is the intersection of the two lines.
◾ For 3 Equations and 3 Unknowns, the solution is the intersection of three planes.
5
LINEAR ALGEBRAIC EQUATIONS
◾ Consider the following set of homogeneous equations (which means that their
right sides are all zero)
6x + ay = 0
2x + 4y = 0
where a is a parameter. Multiply the second equation by 3 and subtract the result
from the first equation to obtain
(a - 12)y = 0
◾ The solution is y = 0 only if a ≠ 12.
◾ If a = 12, there is an infinite number of solutions for x and y, where x = -2y. 6
LINEAR ALGEBRAIC EQUATIONS
◾ It is often convenient to represent a system of equations as a matrix equation.
◾ Matrix notation enables us to represent multiple equations as a single matrix equation.
◾ Consider the system of equations:
2x1 + 9x2 = 5
3x1 - 4x2 = 7
◾ This set can be expressed in vector-matrix form as
2 x1
5
9 x2 7
3 4
which can be represented in the following compact form
Ax = b 7
LINEAR ALGEBRAIC EQUATIONS
◾ For square systems of equations (i.e. the number of equations and the unknowns
are equal), the most powerful tool to determine the number of solutions the system
has is determinant:
◾ For the equation set Ax = b,
◾ if |A| = 0, then there is no unique solution.
◾ A n x n nonhomogeneous system of linear equations has a unique solution if and
only if its determinant is non-zero (det(A) command).
8
𝑎𝑥+𝑏𝑦
The simplest form of two equations:
𝑑𝑥+𝑒𝑦
=𝑐
=𝑓
Write in vector-matrix form:
9
MATLAB:
◾ The MATLAB command inv(A) computes the inverse of the matrix A. The
following MATLAB session solves the following equations using MATLAB.
2x + 9y = 5
3x - 4y = 7
>>A = [2,9;3,-4];b = [5;7]
>>x = inv(A)*b OR
x = >> x = A\b
x =
2.3714 2.3714
0.0286
0.0286
◾ If you attempt to solve for an A matrix whose determinant is zero, the inv command 10
in
EXISTENCE AND UNIQUENESS OF SOLUTIONS
◾ The set Ax = b with m equations and n unknowns has solutions if and only
if rank[A] = rank[A b]
◾ If rank[A] ≠ rank[A b], a Unique Solution Does Not Exist
◾ If rank[A] = rank[A b] = Number of Unknowns, a Unique Solution Exists
◾ If rank[A] = rank[A b] but rank[A] ≠ Number of an Infinite Number of Solutions
Unknowns, Exist
11
WHAT IS RANK?
Find the rank of the given
matrix.
R3 → R3 –
R2
12
Number of non-zero rows = 2
Hence, the rank of matrix A =
EXISTENCE AND UNIQUENESS OF SOLUTIONS
14
15
OVER-DETERMINED SYSTEMS
% Over-Determined System:
% 4x+3y = 7; x-2y = -1; 3x+5y = 8
A = [4 3; 1 -2; 3 5];
B = [7; -1; 8];
AB = [A B];
rank_A = rank(A)
rank_AB = rank(AB)
z= A\B
17
OVER-DETERMINED SYSTEMS
% Over-Determined System:
% 4x+3y = 6; x-2y = -1; 3x+5y = 8
A = [4 3; 1 -2; 3 5];
B = [6; -1; 8];
AB = [A B];
rank_A = rank(A)
rank_AB = rank(AB)
z= A\B
18
EXAMPLE #1
◾ Consider the following 2 x 2 system of equations:
𝑥1 + 2𝑥2 = 3
𝑥1 − 2𝑥2 = 3
Find the intersect point.
Let's write these equations in the form y = mx + b.
𝑦 = −0.5𝑥 + 1.5
𝑦 = 0.5𝑥 − 1.5
We can plot these straight-line equations in MATLAB:
x = -5:5;
y1 = -0.5*x + 1.5; The intersection of the lines is the point
(3,0) 19
y2 = 0.5*x - 1.5;
plot(x, y1, x, y2)
Alternative way to solve Example #1:
A = [1,2;1,-2];
b = [3;3];
x = inv(A)*b
x =
3
0
20
EXAMPLE #2
◾ Consider the following 2 x 2 system of equations:
y1 = (6 - 4x) / 5
y2 = (14 - 4x) / 5
Find x.
21
EXAMPLE #3
y1 = (-4x + 6)/5;
y2 = (3x -14)/2;
y3 = 7x - 25;
Find x.
22