Splitting task 2 matlab function into smaller functions

This commit is contained in:
root 2021-11-06 04:01:04 +01:00
parent 159ae4e1e9
commit adf1f954e3
38 changed files with 129 additions and 293 deletions

View File

@ -0,0 +1,11 @@
function [Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector)
for i3 = Columns : -1 : 1
E = 0;
for iter = i3+1 : Columns
E = E + Matrix(i3,iter) * x(iter,1);
end % end for
x(i3, 1) = (Vector(i3,1) - E) / Matrix(i3,i3);
end % end for
end % end function

View File

@ -0,0 +1,5 @@
function checkIfMatrixIsSquareMatrix(Matrix)
[Rows,Columns] = size(Matrix);
if Rows ~= Columns
error ('Matrix is not square matrix!');
end % end if

View File

@ -1,71 +0,0 @@
% ------- Solving a system of n linear equations Ax = b. ------- %
% -------------------- The Indicated Method -------------------- %
function x = Indicated_Method(A, b)
[M,N] = size(A);
if M ~= N
error ('A is not square matrix!');
end
for j = 1 : N - 1
%--- Find the greatest value within column ---%
m = max(A(j:N,j));
%--- Find row whitin greatest value occure ---%
for k = j : N
if A(k,j) == m
%--- SwapRow in matrix A ---%
tempRow = A(j , :);
A(j , :) = A(k, :);
A(k, :) = tempRow;
%--- SwapValue in vector b ---%
tempVal = b(j);
b(j) = b(k);
b(k) = tempVal;
break;
end
end
for i = j + 1 : N
l = A(i,j) / A(j,j);
b(i,1) = b(i,1) - l * b(j, 1);
for t = 1 : N
A(i,t) = A(i,t) - l * A(j, t);
end
end
end
x = zeros(N,1);
% -------------------- The back-substitution phase -------------------- %
for k = N : -1 : 1
E = 0;
for iter = k+1 : N
E = E + A(k,iter) * x(iter,1);
end
x(k, 1) = (b(k,1) - E) / A(k,k);
end
euclideanNormOfR = norm(r);
r = A*x - b;
euclideanNormOfR = norm(r);
new_euclideanNormOfR = euclideanNormOfR;
while new_euclideanNormOfR <= euclideanNormOfR
euclideanNormOfR = new_euclideanNormOfR;
r = A*x - b;
x = x - r;
new_euclideanNormOfR = norm(r);
end

View File

@ -1,71 +0,0 @@
% ------- Solving a system of n linear equations Ax = b. ------- %
% -------------------- The Indicated Method -------------------- %
function x = debilMode(A, b)
[M,N] = size(A);
if M ~= N
error ('A is not square matrix!');
end % end if
for j = 1 : N - 1
%--- Find the greatest value within column ---%
m = max(A(j:N,j));
%--- Find row whitin greatest value occure ---%
for k = j : N
if A(k,j) == m
%--- SwapRow in matrix A ---%
tempRow = A(j , :);
A(j , :) = A(k, :);
A(k, :) = tempRow;
%--- SwapValue in vector b ---%
tempVal = b(j);
b(j) = b(k);
b(k) = tempVal;
break;
end % end if
end % end for
for i = j + 1 : N
l = A(i,j) / A(j,j);
b(i,1) = b(i,1) - l * b(j, 1);
for t = 1 : N
A(i,t) = A(i,t) - l * A(j, t);
end % end for
end % end for
end
x = zeros(N,1);
% -------------------- The back-substitution phase -------------------- %
for k = N : -1 : 1
E = 0;
for iter = k+1 : N
E = E + A(k,iter) * x(iter,1);
end
x(k, 1) = (b(k,1) - E) / A(k,k);
end
r = A*x - b;
euclideanNormOfR = norm(r);
new_euclideanNormOfR = euclideanNormOfR;
while new_euclideanNormOfR <= euclideanNormOfR
euclideanNormOfR = new_euclideanNormOfR;
r = A*x - b;
x = x - r;
new_euclideanNormOfR = norm(r);
end
end % end function

View File

@ -1,67 +0,0 @@
% ------- Solving a system of n linear equations Ax = b. ------- %
% -------------------- The Indicated Method -------------------- %
function x = debilMode(A, b)
[M,N] = size(A);
if M ~= N
error ('A is not square matrix!');
end % end if
for j = 1 : N - 1
%--- Find the greatest value within column ---%
m = max(A(j:N,j));
%--- Find row whitin greatest value occure ---%
for k = j : N
if A(k,j) == m
%--- SwapRow in matrix A ---%
tempRow = A(j , :);
A(j , :) = A(k, :);
A(k, :) = tempRow;
%--- SwapValue in vector b ---%
tempVal = b(j);
b(j) = b(k);
b(k) = tempVal;
break;
end % end if
end % end for
for i = j + 1 : N
l = A(i,j) / A(j,j);
b(i,1) = b(i,1) - l * b(j, 1);
for t = 1 : N
A(i,t) = A(i,t) - l * A(j, t);
end % end for
end % end for
end % end for
x = zeros(N,1);
% -------------------- The back-substitution phase -------------------- %
for k = N : -1 : 1
E = 0;
for iter = k+1 : N
E = E + A(k,iter) * x(iter,1);
end % end for
x(k, 1) = (b(k,1) - E) / A(k,k);
end % end for
r = A*x - b;
euclideanNormOfR = norm(r);
new_euclideanNormOfR = euclideanNormOfR;
while new_euclideanNormOfR <= euclideanNormOfR
euclideanNormOfR = new_euclideanNormOfR;
r = A*x - b;
x = x - r;
new_euclideanNormOfR = norm(r);
end % end while
end % end function

View File

@ -0,0 +1 @@
fileattrib debilMode.m

0
ENUME/projectA/gaussianElimination.asv Normal file → Executable file
View File

67
ENUME/projectA/gaussianElimination.m Normal file → Executable file
View File

@ -1,61 +1,8 @@
function x = gaussianElimination(A, b) % A - matrix, b - vector
[~, Height] = size(A);
% Find column with greatest value element
for i = 1 : Height - 1
m = max(A(i:Height, i));
% Find row with greatest value element
for k = i : Height
if A(k, i) == m % If the element has the biggest value we now
% have the value of two rows that we want to swap
% Swap row in matrix A %
swapRow(A, i, k);
swapValue(b, i, k);
break;
end % end if
end % end for
% Swap value in vector B %
function [Matrix, Vector] = gaussianElimination(i1, Columns, Matrix, Vector)
for i2 = i1 + 1 : Columns
l = Matrix(i2,i1) / Matrix(i1,i1);
[Matrix, Vector] = substractRows(Matrix, Vector, i2, l, i1, Columns);
end % end for
for j = i + 1 : Height
l = A(j, i) / A(i, i);
b(j, 1) = b(j, 1) - l * b(i, 1);
for t = 1 : Height
A(j, t) = A(j, t) - l * A(i, t);
end % end for
end % end for
x = zeros(Height, 1);
for k = Height: -1:1
E = 0;
for iter = k + 1 : Height
E = E + A(k, iter) * x(iter, 1);
end
x(k, 1) = (b(k, 1) - E) / A(k, k);
end
end % end function
function swapRow(A, j, k)
temp = A(j, :);
A(j, :) = A(k, :);
A(k, :) = temp;
end % end function
function swapValue(b, j, k)
temp = b(j);
b(j) = b(k);
b(k) = temp;
end % end function
end % end function

View File

@ -0,0 +1,7 @@
function [Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector)
for i1 = 1 : Columns - 1
m = max(Matrix(i1:Columns,i1));
partialPivoting(Matrix, Vector, i1, m);
[Matrix, Vector] = gaussianElimination(i1, Columns, Matrix, Vector);
end % end for
end % end function

View File

@ -0,0 +1,8 @@
function x = improveSolution(x, new_euclideanColumnsormOfR, euclideanColumnsormOfR, Matrix, Vector)
while new_euclideanColumnsormOfR <= euclideanColumnsormOfR
euclideanColumnsormOfR = new_euclideanColumnsormOfR;
r = Matrix*x - Vector;
x = x - r;
new_euclideanColumnsormOfR = norm(r);
end % end while
end % end function

View File

@ -0,0 +1,7 @@
function x = indicatedMethod(Matrix, Vector)
[~,Columns] = size(Matrix);
checkIfMatrixIsSquareMatrix(Matrix);
[Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector);
[Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector);
x = iterativeResidualCorrection(Matrix, x, Vector);
end % end function

View File

@ -0,0 +1,7 @@
function x = indicatedMethod(Matrix, Vector)
[~,Columns] = size(Matrix);
checkIfMatrixIsSquareMatrix(Matrix);
[Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector);
[Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector);
x = iterativeResidualCorrection(Matrix, x, Vector);
end % end function

View File

@ -0,0 +1,6 @@
function x = iterativeResidualCorrection(Matrix, x, Vector)
r = Matrix*x - Vector;
euclideanColumnsormOfR = norm(r);
new_euclideanColumnsormOfR = euclideanColumnsormOfR;
x = improveSolution(x, new_euclideanColumnsormOfR, euclideanColumnsormOfR, Matrix, Vector);
end % end function

0
ENUME/projectA/matrixA.m Normal file → Executable file
View File

0
ENUME/projectA/matrixB.m Normal file → Executable file
View File

View File

@ -0,0 +1,6 @@
function [Matrix, Vector] = partialPivoting(Matrix, Vector, i1, m)
[~,Columns] = size(Matrix);
for i3 = i1 : Columns
partialPivotingSwapOneRow(Matrix, Vector, i1, i3, m);
end % end for
end % end function

View File

@ -0,0 +1,6 @@
function [Matrix, Vector] = partialPivotingSwapOneRow(Matrix, Vector, i1, i3, m)
if Matrix(i3,i1) == m
swapRowMatrix(Matrix, i1, i3);
swapValueVector(Vector, i1, i3);
end % end if
end % end function

View File

@ -0,0 +1,6 @@
function [Matrix, Vector] = partialPivotingSwapOneRow(Matrix, Vector, i1, i3, m)
if Matrix(i3,i1) == m
swapRowMatrix(Matrix, i1, i3);
swapValueVector(Vector, i1, i3);
end % end if
end % end function

0
ENUME/projectA/pro1.txt Normal file → Executable file
View File

0
ENUME/projectA/projectA.aux Normal file → Executable file
View File

36
ENUME/projectA/projectA.fdb_latexmk Normal file → Executable file
View File

@ -1,6 +1,6 @@
# Fdb version 3
["pdflatex"] 1635438957 "projectA.tex" "projectA.pdf" "projectA" 1635449229
"/etc/texmf/web2c/texmf.cnf" 1624878795 475 c0e671620eb5563b2130f56340a5fde8 ""
["pdflatex"] 1636164631 "projectA.tex" "projectA.pdf" "projectA" 1636164632
"/etc/texmf/web2c/texmf.cnf" 1635008344 475 c0e671620eb5563b2130f56340a5fde8 ""
"/usr/share/texlive/texmf-dist/fonts/enc/dvips/base/8r.enc" 1165713224 4850 80dc9bab7f31fb78a000ccfed0e27cab ""
"/usr/share/texlive/texmf-dist/fonts/map/fontname/texfonts.map" 1577235249 3524 cb3e574dea2d1052e39280babc910dc8 ""
"/usr/share/texlive/texmf-dist/fonts/tfm/jknappen/ec/ecbx1000.tfm" 1136768653 3584 2d666ecf6d466d8b007246bc2f94d9da ""
@ -126,24 +126,24 @@
"/usr/share/texmf/fonts/enc/dvips/cm-super/cm-super-t1.enc" 1565080000 2971 def0b6c1f0b107b3b936def894055589 ""
"/usr/share/texmf/fonts/enc/dvips/cm-super/cm-super-ts1.enc" 1565080000 2900 1537cc8184ad1792082cd229ecc269f4 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx1000.pfb" 1565080000 145408 43d44302ca7d82d487f511f83e309505 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx1200.pfb" 1624878812 140176 d4962f948b4cc0adf4d3dde77a128c95 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx1440.pfb" 1624878812 135942 859a90cad7494a1e79c94baf546d7de5 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx2074.pfb" 1624878812 140194 627cc7f36c05b80e25d178974ccb3442 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx2488.pfb" 1624878812 135938 299ac3a69892db3b7674a8b2543b0a77 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx1200.pfb" 1635008356 140176 d4962f948b4cc0adf4d3dde77a128c95 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx1440.pfb" 1635008356 135942 859a90cad7494a1e79c94baf546d7de5 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx2074.pfb" 1635008356 140194 627cc7f36c05b80e25d178974ccb3442 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfbx2488.pfb" 1635008356 135938 299ac3a69892db3b7674a8b2543b0a77 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfrm1000.pfb" 1565080000 138258 6525c253f16cededa14c7fd0da7f67b2 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfrm1200.pfb" 1624878812 136101 f533469f523533d38317ab5729d00c8a ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfrm1728.pfb" 1624878812 131438 3aa300b3e40e5c8ba7b4e5c6cebc5dd6 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfrm1200.pfb" 1635008356 136101 f533469f523533d38317ab5729d00c8a ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfrm1728.pfb" 1635008356 131438 3aa300b3e40e5c8ba7b4e5c6cebc5dd6 ""
"/usr/share/texmf/fonts/type1/public/cm-super/sfti1000.pfb" 1565080000 186554 e8f0fa8ca05e038f257a06405232745f ""
"/usr/share/texmf/web2c/texmf.cnf" 1613593815 38841 799d1dd9682a55ce442e10c99777ecc1 ""
"/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map" 1624878842 5160710 ecf427ae8fa19139d8691f526e47bb9b ""
"/var/lib/texmf/web2c/pdftex/pdflatex.fmt" 1624878876 2570852 ef2a47e64eb64912b01ccaedc81044b8 ""
"projectA.aux" 1635438958 4771 f2c1e1e1b62e376e2ee3d41d6ebec6a2 "pdflatex"
"projectA.out" 1635438958 1864 f5c30b186e6644a7cbcb08ae397fee3f "pdflatex"
"projectA.tex" 1635449228 7751 93268bfae51173fe98c8d150f4220c3a ""
"projectA.toc" 1635438958 2529 683b89eb0e469a5b173eb23a111fbf71 "pdflatex"
"/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map" 1635008389 5160710 ecf427ae8fa19139d8691f526e47bb9b ""
"/var/lib/texmf/web2c/pdftex/pdflatex.fmt" 1635008460 2570450 6e12b1c097cbda0f70015645294afd24 ""
"projectA.aux" 1636164632 4771 f2c1e1e1b62e376e2ee3d41d6ebec6a2 "pdflatex"
"projectA.out" 1636164632 1864 f5c30b186e6644a7cbcb08ae397fee3f "pdflatex"
"projectA.tex" 1636164631 7712 950b41b6e8c3b69e4724dbf4abd7be49 ""
"projectA.toc" 1636164632 2529 683b89eb0e469a5b173eb23a111fbf71 "pdflatex"
(generated)
"projectA.aux"
"projectA.out"
"projectA.log"
"projectA.toc"
"projectA.pdf"
"projectA.log"
"projectA.out"
"projectA.aux"
"projectA.toc"

View File

@ -1,4 +1,4 @@
PWD /home/kuchy/Zlew/Studia/SEM_5/enume_done/matlabproject/projectA
PWD /home/kuchy/Zlew/Studia/SEM_5/enume_done/Project/matlabproject/ENUME_Project_31/projectA
INPUT /etc/texmf/web2c/texmf.cnf
INPUT /usr/share/texmf/web2c/texmf.cnf
INPUT /usr/share/texlive/texmf-dist/web2c/texmf.cnf

8
ENUME/projectA/projectA.log Normal file → Executable file
View File

@ -1,4 +1,4 @@
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/Debian) (preloaded format=pdflatex 2021.6.28) 28 OCT 2021 18:35
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/Debian) (preloaded format=pdflatex 2021.10.23) 6 NOV 2021 03:10
entering extended mode
restricted \write18 enabled.
file:line:error style messages enabled.
@ -541,15 +541,15 @@ Package rerunfilecheck Info: File `projectA.out' has not changed.
)
Here is how much of TeX's memory you used:
12275 strings out of 479304
214137 string characters out of 5869779
618030 words of memory out of 5000000
214137 string characters out of 5869778
618032 words of memory out of 5000000
29197 multiletter control sequences out of 15000+600000
420242 words of font info for 68 fonts, out of 8000000 for 9000
1141 hyphenation exceptions out of 8191
81i,7n,88p,292b,1137s stack positions out of 5000i,500n,10000p,200000b,80000s
{/usr/share/texmf/fonts/enc/dvips/cm-super/cm-super-t1.enc}{/usr/share/texmf/fonts/enc/dvips/cm-super/cm-super-ts1.enc}{/usr/share/texlive/texmf-dist/fonts/enc/dvips/base/8r.enc}</usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmex10.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmmi10.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmmi7.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr7.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy10.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy7.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/bera/fvmr8a.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfbx1000.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfbx1200.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfbx1440.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfbx2074.pfb></usr/share/
texmf/fonts/type1/public/cm-super/sfbx2488.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfrm1000.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfrm1200.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfrm1728.pfb></usr/share/texmf/fonts/type1/public/cm-super/sfti1000.pfb>
Output written on projectA.pdf (11 pages, 229855 bytes).
Output written on projectA.pdf (11 pages, 229976 bytes).
PDF statistics:
302 PDF objects out of 1000 (max. 8388607)
269 compressed objects within 3 object streams

0
ENUME/projectA/projectA.out Normal file → Executable file
View File

BIN
ENUME/projectA/projectA.pdf Normal file → Executable file

Binary file not shown.

Binary file not shown.

View File

@ -216,7 +216,7 @@ And so on, for $x_k$:
Gaussian elimination method has one flaw, where it can come into halt if:
\[ a_{kk}^{(k)} = 0 \]
To avoid it we use method of pivoting, in our case we will use partial pivoting method.
Before each Gaussian elimination step, we do it before each Gaussian elimination step since this will lead to smaller error.
We do it before each Gaussian elimination step since this will lead to smaller error.
\\We first find a row $i$ such that:
\[ |{a_{ik}^{k}}| = \underset{j}{max} \{ |{a_{kk}^{k}}|, |{a_{k+1, k}^{k}}|, \cdots, |{a_{nk}^{k}}|\} \]

0
ENUME/projectA/projectA.toc Normal file → Executable file
View File

View File

@ -0,0 +1,6 @@
function [Matrix, Vector] substractRows(Vector, i2, l, i1, t, Columns)
Vector(i2,1) = Vector(i2,1) - l * Vector(i1, 1);
for t = 1 : Columns
Matrix(i2,t) = Matrix(i2,t) - l * Matrix(i1, t);
end % end for
end % end function

View File

@ -0,0 +1,6 @@
function [Matrix, Vector] = substractRows(Matrix, Vector, i2, l, i1, Columns)
Vector(i2,1) = Vector(i2,1) - l * Vector(i1, 1);
for t = 1 : Columns
Matrix(i2,t) = Matrix(i2,t) - l * Matrix(i1, t);
end % end for
end % end function

View File

@ -0,0 +1,5 @@
function Matrix = swapRowMatrix(Matrix, i1, i3)
tempRow = Matrix(i1 , :);
Matrix(i1 , :) = Matrix(i3, :);
Matrix(i3, :) = tempRow;
end

View File

@ -0,0 +1,5 @@
function Vector = swapValueVector(Vector, i1, i3)
tempVal = Vector(i1);
Vector(i1) = Vector(i3);
Vector(i3) = tempVal;
end % end function

View File

@ -0,0 +1,5 @@
function Vector = swapValueVector(Vector, i1, i3)
tempVal = Vector(i1);
Vector(i1) = Vector(i3);
Vector(i3) = tempVal;
end % end function

0
ENUME/projectA/task-USOS_1153296.pdf Normal file → Executable file
View File

1
ENUME/projectA/untitled.m Executable file
View File

@ -0,0 +1 @@
jebaćdsia

0
ENUME/projectA/usefullinks.txt Normal file → Executable file
View File

0
ENUME/projectA/vectorA.m Normal file → Executable file
View File

0
ENUME/projectA/vectorB.m Normal file → Executable file
View File