Matlab Class 09
Matlab Class 09
Class 09
Class 07
1. Direct Method
2. Indirect Methods
[ ][ ] [ ] [ ][ ] [ ]
1 2 3 𝑥 9 1 2 3 𝑥 9
2 −1 1 𝑦 = 8 0 −5 5 𝑦 = − 10
3 0 −1 𝑧 3 0 0 −4 𝑧 − 12
Elementary row operations
• Three elementary row operations can be performed on a set
of linear equations without changing the solution
1. Interchanging rows
2. Scaling a row by a constant
3. Replacing a row with a linear combination of the original row
and a multiple of any other row
Gaussian Elimination Method:
You are running a concession stand at a basketball game.
Ex: You are selling hot dogs, chips, and sodas. Each hot dog
costs $1.50, chip costs $1, and soda costs $0.50. At the
end of the night, you made a total of $98.5. You sold a
total of 107 hot dogs, chips, and sodas combined. The
number of sodas sold is twice that of hot dogs. You must
report the number of each sold. Solve this problem using
•Gaussian elimination.
number of hot dogs sold: x
• number of chips sold: y
• number of sodas sold: z
x + y + z = 107
1.5x + y + 0.5z = 98.5
z = 2x -> 2x – z = 0
Matrix form: Ax = b
Ex: Make a function that solves a linear system with three
linear equations using Gaussian Elimination. The
function’s head should be: Example run for the following equations:
• x + y + z = 107
• 1.5x + y + 0.5z = 98.5
[ |]
𝑎 11 𝑎 12 𝑎 13 𝑏1 >> A = [1 1 1; 1.5 1 0.5; 2 0 -1];
>> B = [107; 98.5; 0];
𝑎 21 𝑎 22 𝑎 23 𝑏2 >> myGE3(A,B)
𝑎 31 𝑎 32 𝑎 33 𝑏3 ans =
17
Recommended
56
steps:
34
function x = myGE3(A,b)
AG = [A b];
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) * AG(1,:);
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) * AG(1,:);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) * AG(2,:);
RE = AG;
[ |] [ | ]
1 2 3 9 𝑹 𝟐=𝑹𝟐 −𝟐 𝑹𝟏 1 2 3 9
2 −1 1 8 0 −5 − 5 −10
3 0 −1 3 3 0 −1 3
[ | ] [ | ]
𝑹 = 𝑹 − 𝑹𝟐 1
𝑹 𝟑=𝑹𝟑 −𝟑 𝑹𝟏1 2 3 9 𝟑 𝟑
𝟓 2 3 9
0 −5 −5 − 10 0 −5 − 5 − 10
0 −6 − 10 −24 0 0 − 4 − 12
Row-Echelon form obtained
by Gaussian elimination
Gauss-Jordan elimination example
[ | ] [
𝑹 𝟐=− 𝑹 𝟐 /𝟓 1
| ]
1 2 3 9 2 3 9
0 −5 − 5 − 10 0 1 1 2
0 0 − 4 − 12 0 0 − 4 − 12
[ |] 𝑹 𝟐=𝑹𝟐 − 𝑹 𝟑 1
[ | ]
𝑹 𝟑=− 𝑹 𝟑 /𝟒 1 2 3 9 2 3 9
0 1 1 2 0 1 0 −1
0 0 1 3 0 0 1 3
𝑹 𝟏=𝑹𝟏 −𝟐 𝑹𝟐 1
[ | ] 𝑹 𝟏=𝑹𝟏 −𝟑 𝑹𝟑1
[ | ]
0 3 11 0 0 2
0 1 0 −1 0 1 0 −1
0 0 1 3 0 0 1 3
x + y + z = 20
x = y + 6 x-y = 6
x = 1.5(y + z) x – 1.5y – 1.5 = 0
Matrix form: Ax = b
Gauss-Jordan elimination example
Linear system Augmented form
[ ][ ] [ ] [ |] [
𝑹 𝟐=𝑹𝟐 − 𝑹 𝟏 1
| ]
1 1 1 20 1 1 20
1 1 1 𝑥 20
1 −1 0 6 0 −2 −1 − 14
1 −1 0 𝑦 = 6
1 −1 . 5 −1 .5 0 1 −1 .5 − 1. 5 0
1 −1 . 5 − 1 .5 𝑧 0
[ | ] [
20 𝑹 𝟑=𝑹𝟑 −𝟏 . 𝟐𝟓 𝑹𝟏1
| ]
1 1 20
𝑹 𝟑= 𝑹𝟑 − 𝑹 𝟏1 1 1
0 −2 −1 −14
0 −2 − 1 −14
0 −2 . 5 − 2 .5 −20 0 0 −1 . 25 − 2. 5
Row-Echelon form obtained by Gaussian elimination
[ | ] [ |]
𝑹𝟑
[ | ]
𝑹 𝟑 =− 1 1 1 20 𝑹 = 𝑹 + 𝑹1
𝟐 𝟐
1 1 20 𝑹 𝟐=𝑹𝟐 /−𝟐 1 1 1 20
𝟏 .𝟐𝟓 0 −2 − 1 − 14 0𝟑 −2 0 − 12 0 1 0 6
0 0 1 2 0 0 1 2 0 0 1 2
𝑹 𝟏=𝑹𝟏 − 𝑹 𝟑1
[ | ] 𝑹 𝟏=𝑹𝟏 − 𝑹 𝟐1
[ | ]
1 0 18 0 0 12 𝑥=12
0 1 0 6 0 1 0 6 𝑦=6
0 0 1 2 0 0 1 2 𝑧 =2
Make a function that solves a linear system with three
linear equations using Gauss Jordan method. The
function’s head should be:
function
𝑎 11 𝑎 12 𝑎x13=
𝑏1myGJ3(A, b) function x = myGJ3(A,b)
[ 𝑎 21
𝑎 31
𝑎 22
𝑎 32 |]
𝑎 23 𝑏2
𝑎 33 𝑏3
AG = [A b];
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) *
AG(1,:);
Recommended steps:
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) *
AG(1,:);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) *
AG(2,:);
RE = AG;
x(1,1) = RRE(1,4);
x(2,1) = RRE(2,4);
x(3,1) = RRE(3,4);
end
For 3 Linear Equations For N Linear Equations
function x = myGJ3(A,b) function x = myGJ(A,b)
AG = [A b]; [row, col] = size(A); % Assume A is a square matrix
AG(2,:) = AG(2,:) - AG(2,1)/AG(1,1) * AG = [A b];
AG(1,:); for r = 2:row
AG(3,:) = AG(3,:) - AG(3,1)/AG(1,1) * for c = 1:r-1
AG(1,:); S = AG(r, c) / AG(c, c) * AG(c, :);
AG(3,:) = AG(3,:) - AG(3,2)/AG(2,2) * AG(r, :) = AG(r, :) - S;
AG(2,:); end
RE = AG; end
RE = AG;
RE(1,:) = RE(1,:) / RE(1,1);
RE(2,:) = RE(2,:) / RE(2,2); for r = 1:row
RE(3,:) = RE(3,:) / RE(3,3); RE(r,:) = RE(r,:) / RE(r,r);
end
RE(2,:) = RE(2,:) - RE(3,:) * RE(2,3);
RE(1,:) = RE(1,:) - RE(3,:) * RE(1,3); for r = row-1:-1:1
RE(1,:) = RE(1,:) - RE(2,:) * RE(1,2); S = 0;
for c = col:-1:r+1
RRE = RE; S = S + RE(c, :) * RE(r, c);
end
x(1,1) = RRE(1,4); RE(r, :) = RE(r, :) - S;
x(2,1) = RRE(2,4); end
x(3,1) = RRE(3,4);
end RRE = RE;
for r = 1:row
x(r,1) = RRE(r,4);
end
end
Ex: Make a function that returns the inverse matrix of a 3x3
matrix using the Gauss Jordan method. The function’s
head should be:
RRE = RE;
• The “x” values are the unknowns, and “a” and “b” represent constants.
A-1Ax = A-1b
Ix = A-1b
x = A-1b
(Ex) I
Solving matrices: Ax = b
Ax = b A-1Ax = A-1b
Ix = A-1b
x = A-1b
Jane is using an ATD phone plan, which costs $20 per
Ex: month, with calls costing an additional 50 cents per
minute. Another company, Spring, charges $40 per
month, but calls cost only 10 cents per minute. If Jane
uses more than a certain time of calls per month, it will be
beneficial to switch to Spring. What is that amount of
•time?
Assuming Jane uses her phone about “t” min per month.
• Assuming “c” = total costs
ATD: c = 20 + 0.5t
Two linear equations
Spring: c = 40 + 0.1t = A linear system with two linear equations
Matrix form: Ax = b
Ex:
You are running a concession stand at a basketball game.
You are selling hot dogs and sodas. Each hot dog costs
$1.50 and each soda costs $0.50. At the end of the night,
you made a total of $78.50. You sold a total of 87 hot dogs
and sodas combined. You must report the number of hot
dogs sold and the number of sodas sold. How many hot
dogs were sold and how many sodas were sold?
• # of hot dogs sold = x
• # of sodas sold = y
[ ][ ] [ ]
1 2 3 𝑥 0𝑦 + 3 𝑧 =1 𝑥 +2 ∗ ( − 2 ) +3 ∗3=10 → 𝒙=𝟖
1 𝑥1+2
0 1 5 +5 𝑧 =1 𝑦 +5 ∗ 3=13 → 𝒚 =− 𝟐
𝑦 =1 𝑦13
0 0 4 𝑧 412 𝑧 =12 → 𝒛 =𝟑
[ ][ ] [ ]
1 0 0 𝑥 1𝑥 1= 1 → 𝒙 =𝟏
2 1 0 2 𝑦 =2 ∗1+1 𝑦 =2 → 𝒚 =𝟎
2 𝑥+1
𝑦 =
1 2 1 3 𝑦 + 1 𝑧 =1 ∗1+ 2∗ 0+1 𝑧 =3 → 𝒛 =𝟐
𝑧 1 𝑥 +2
• Gauss Jacobi Method:
clear all
clc function x = Jacobi1(A, b, Eps)
A = [27 6 -1;6 15 2;1 1 54]; A = [27 6 -1;6 15 2;1 1 54];
b = [85;72;110]; b = [85;72;110];
maxerr = 1e-5; Eps = 1e-5;
x = zeros(1,size(A,1)); n = length(b) ;
n = size(A,1); x0 = zeros(3, 1) ;
err = Inf; x = ones(size(x0)) ;
itr = 0; while norm(x-x0, inf) >= Eps
while all(err>maxerr) x0 = x ;
xold = x; for i = 1 : n
for i = 1:n x(i) = b(i) ;
sum = 0; for j = 1 : n
for j = 1:n if j ~= i
if j~=i x(i) = x(i) - A(i, j)*x0(j) ;
sum = sum+A(i,j)*xold(j); end
end end
end x(i) = x(i) / A(i, i) ;
x(i)=(1/A(i,i))*(b(i)-sum); end
end end
itr = itr + 1;
y(itr,:) = x;
err = abs(xold-x);
end
disp(x)
• Gauss-Seidel Method:
A = [10 3 1;3 10 2;1 2 10];
A = [10 3 1;3 10 2;1 2 10]; b = [19;29;35];
B = [19;29;35]; x=[0 0 0]'
P = [0;0;0]; n=size(x,1);
n = 10; normVal=Inf;
e = 0.0001; tol=1e-5; itr=0;
N = length(B); while normVal>tol
X = zeros(N,1); x_old=x;
Y = zeros(N,1);%for stopping criteria for i=1:n
for j = 1:n %Number of iteration sigma=0;
for i = 1:N for j=1:i-1
X(i) = (B(i)/A(i,i))-(A(i,[1:i- sigma=sigma+A(i,j)*x(j);
1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i); end %summation of previous
P(i) = X(i); iteration
end for j=i+1:n
fprintf('Iteration no %d\n',j)
X sigma=sigma+A(i,j)*x_old(j);
if abs(Y-X)<e end %summation of current iteration
break x(i)=(1/A(i,i))*(b(i)-sigma);
end end%solution of vector x of current
Y = X; iteration
end itr=itr+1;
normVal=norm(x_old-x);
end
disp(x)
• Successive Over Relaxation Method:
clear all
clc if e < 1
A = [2 -1 0;-1 2 -1;0 -1 2]; while sum(abs(err)<=maxerr)~=size(A,1)
b = [7; 1; 1]; xnew = H*x+C;
x = [0;0;0]; err = xnew-x;
maxiter = 50; x = xnew;
err = 1e-5; iter = iter + 1;
maxerr = 0.04; y(iter,:) = x;
w = 0.8; if iter==maxiter
D = diag(diag(A)); break
L = tril(A)-D; end
U = triu(A)-D; end
H = inv(D+w*L)*((1-w)*D-w*U); else
C = w*inv(D+w*L)*b; disp('Not Converged');
e = max(eig(H)); end
iter = 1;
y(1,:) = x;