Rozwiązywanie układów równań liniowych algebraicznych
a) c)
b) d)
Metoda Cramera
a)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[11
12
13
14];
A=[1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3];
W=det(A);
Wx=det([11 2 3 4; 12 3 4 1; 13 4 1 2; 14 1 2 3]);
Wy=det([1 11 3 4; 2 12 4 1; 3 13 1 2; 4 14 2 3]);
Wz=det([1 2 11 4; 2 3 12 1; 3 4 13 2; 4 1 14 3]);
Ww=det([1 2 3 11; 2 3 4 12; 3 4 1 13; 4 1 2 14]);
xA=[Wx/W
Wy/W
Wz/W
Ww/W];
xA
toc
wynik:
xA =
2.000000000000000e+00
1.000000000000000e+00
1.000000000000000e+00
9.999999999999998e-01
Elapsed time is 0.010930 seconds.
b)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[1
2
-3
-6];
A=[2 -1 1 -1; 2 -1 0 -3; 3 0 -1 1; 2 2 -2 5];
W=det(A);
Wx=det([1 -1 1 -1; 2 -1 0 -3; -3 0 -1 1; -6 2 -2 5]);
Wy=det([2 1 1 -1 ; 2 2 0 -3 ; 3 0 -3 1 ; 2 -6 -2 5]);
Wz=det([2 -1 1 -1; 2 -1 2 -3; 3 0 -3 1 ; 2 2 -6 5]);
Ww=det([2 -1 1 1 ; 2 -1 0 2 ; 3 0 -1 -3; 2 2 -2 6]);
xB=[Wx/W
Wy/W
Wz/W
Ww/W];
xB
toc
wynik:
xB =
-1.973729821555835e-16
-9.555555555555559e+00
1.666666666666667e+00
-5.333333333333334e+00
Elapsed time is 0.010858 seconds.
c)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[7
-2
10
0];
A=[2 2 -1 1; 1 -1 1 -1; 1 1 1 1; 4 3 -2 -1];
W=det(A);
Wx=det([7 2 -1 1; -2 -1 1 -1; 10 1 1 1; 0 3 -2 -1]);
Wy=det([2 7 -1 1; 1 -2 1 -1; 1 10 1 1; 4 0 -2 -1]);
Wz=det([2 2 7 1; 1 -1 -2 -1; 1 1 10 1; 4 3 0 -1]);
Ww=det([2 2 -1 7; 1 -1 1 -2; 1 1 1 10; 4 3 -2 0]);
xC=[Wx/W
Wy/W
Wz/W
Ww/W];
xC
toc
wynik:
xC =
1.000000000000001e+00
1.999999999999999e+00
3.000000000000000e+00
4.000000000000000e+00
Elapsed time is 0.010612 seconds.
d)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[3
0
-2
-2];
A=[3 1 1 2; 3 2 3 5; 6 5 1 5; 3 2 3 7];
W=det(A);
Wx=det([3 1 1 2; 0 2 3 5; -2 5 1 5; -2 2 3 7]);
Wy=det([3 3 1 2; 3 0 3 5; 6 -2 1 5; 3 -2 3 7]);
Wz=det([3 1 3 2; 3 2 0 5; 6 5 -2 5; 3 2 -2 7]);
Ww=det([3 1 1 3; 3 2 3 0; 6 5 1 -2; 3 2 3 -2]);
xD=[Wx/W
Wy/W
Wz/W
Ww/W];
xD
toc
wynik:
xD =
2.000000000000000e+00
-1.999999999999999e+00
9.999999999999997e-01
-9.999999999999997e-01
Elapsed time is 0.010689 seconds.
Metoda dzielenia lewostronnego A\b
a)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[11
12
13
14];
A=[1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3];
xA=A\b;
xA
toc
wynik:
xA =
2.000000000000000e+00
1.000000000000000e+00
1.000000000000000e+00
1.000000000000000e+00
Elapsed time is 0.009719 seconds.
b)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[1
2
-3
-6];
A=[2 -1 1 -1; 2 -1 0 -3; 3 0 -1 1; 2 2 -2 5];
xB=A\b;
xB
toc
wynik:
xB =
7.401486830834377e-17
2.000000000000000e+00
1.666666666666667e+00
-1.333333333333333e+00
Elapsed time is 0.085826 seconds.
c)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[7
-2
10
0];
A=[2 2 -1 1; 1 -1 1 -1; 1 1 1 1; 4 3 -2 -1];
xC=A\b;
xC
toc
wynik:
xC =
1
2
3
4
Elapsed time is 0.009957 seconds.
d)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[3
0
-2
-2];
A=[3 1 1 2; 3 2 3 5; 6 5 1 5; 3 2 3 7];
xD=A\b;
xD
toc
wynik:
xD =
2.000000000000000e+00
-2.000000000000000e+00
9.999999999999994e-01
-9.999999999999997e-01
Elapsed time is 0.009966 seconds.
Metoda inv(A)*b
a)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[11
12
13
14];
A=[1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3];
xA=inv(A)*b;
xA
toc
wynik:
xA =
1.999999999999999e+00
1.000000000000000e+00
9.999999999999999e-01
1.000000000000000e+00
Elapsed time is 0.009778 seconds.
b)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[1
2
-3
-6];
A=[2 -1 1 -1; 2 -1 0 -3; 3 0 -1 1; 2 2 -2 5];
xB=inv(A)*b;
xB
toc
wynik:
xB =
0
2.000000000000000e+00
1.666666666666667e+00
-1.333333333333333e+00
Elapsed time is 0.010263 seconds.
c)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[7
-2
10
0];
A=[2 2 -1 1; 1 -1 1 -1; 1 1 1 1; 4 3 -2 -1];
xC=inv(A)*b;
xC
toc
wynik:
xC =
1.000000000000000e+00
2.000000000000000e+00
3.000000000000001e+00
4.000000000000000e+00
Elapsed time is 0.009957 seconds.
d)
program:
%n - licznik wierszy
%m - licznik kolumn
tic
clear all;
clc;
format long e
b=[3
0
-2
-2];
A=[3 1 1 2; 3 2 3 5; 6 5 1 5; 3 2 3 7];
xD=inv(A)*b;
xD
toc
wynik:
xD =
2.000000000000000e+00
-2.000000000000000e+00
9.999999999999998e-01
-9.999999999999998e-01
Elapsed time is 0.010038 seconds.
Metoda eliminacji Gaussa
a)
program:
tic
clear all;
clc;
format long e
b=[ 11
12
13
14];
A=[1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3];
[nb mb]= size (b);
[n m]= size (A);
L= eye (n);
U= [A b];
for k = 1 :m -1
L(k+1:n,k)=U(k+1:n,k)/U(k,k);
U(k+1:n,:)=U(k+1:n,:)-L(k+1:n,k)*U(k,:);
end
L2=eye(n);
for k = m : -1 : 2
L2(1:k-1,k)=U(1:k-1,k)/U(k,k);
U(1:k-1,:)=U(1:k-1,:)-L2(1:k-1,k)*U(k,:);
end
U1 = U(:,1:m);
B=U(:,m+1:m+mb);
for k=1 : n
x(k,:)=B(k,:)/U1(k,k);
end
x
toc
wynik:
x =
2
1
1
1
Elapsed time is 0.014379 seconds.
b)
program:
tic
clear all;
clc;
format long e
b=[ 1
2
-3
-6];
A=[2 -1 1 -1; 2 -1 0 -3; 3 0 -1 1; 2 2 -2 5];
[nb mb]= size (b);
[n m]= size (A);
L= eye (n);
U= [A b];
for k = 1 :m -1
L(k+1:n,k)=U(k+1:n,k)/U(k,k);
U(k+1:n,:)=U(k+1:n,:)-L(k+1:n,k)*U(k,:);
end
L2=eye(n);
for k = m : -1 : 2
L2(1:k-1,k)=U(1:k-1,k)/U(k,k);
U(1:k-1,:)=U(1:k-1,:)-L2(1:k-1,k)*U(k,:);
end
U1 = U(:,1:m);
B=U(:,m+1:m+mb);
for k=1 : n
x(k,:)=B(k,:)/U1(k,k);
end
x
toc
wynik:
x =
NaN
NaN
NaN
NaN
Elapsed time is 0.014151 seconds.
c)
program:
tic
clear all
clc
format long e
b=[7
-2
10
0];
A=[2 2 -1 1;1 -1 1 -1;1 1 1 1;4 3 -2 -1];
[nb mb] = size(b);
[n m] = size(A);
L=eye(n);
U=[A b];
for k = 1 : m-1
L(k+1:n,k) = U(k+1:n,k)/U(k,k);
U(k+1:n,:) = U(k+1:n,:)-L(k+1:n,k)*U(k,:);
end
L2 = eye(n);
for k=m : -1 : 2
L2(1:k-1,k) = U(1:k-1,k)/U(k,k);
U(1:k-1,:) = U(1:k-1,:)-L2(1:k-1,k)*U(k,:);
end
U1 = U(:,1:m);
B = U(:,m+1:m+mb);
for k = 1 : n
x(k,:) = B(k,:)/U1(k,k);
end
x
toc
wynik:
x =
1
2
3
4
Elapsed time is 0.013919 seconds.
d)
program:
tic
clear all
clc
format long e
b=[3
0
-2
-2];
A=[3 1 1 2;3 2 3 5;6 5 1 5;3 2 3 7];
[nb mb] = size(b);
[n m] = size(A);
L=eye(n);
U=[A b];
for k = 1 : m-1
L(k+1:n,k) = U(k+1:n,k)/U(k,k);
U(k+1:n,:) = U(k+1:n,:)-L(k+1:n,k)*U(k,:);
end
L2 = eye(n);
for k=m : -1 : 2
L2(1:k-1,k) = U(1:k-1,k)/U(k,k);
U(1:k-1,:) = U(1:k-1,:)-L2(1:k-1,k)*U(k,:);
end
U1 = U(:,1:m);
B = U(:,m+1:m+mb);
for k = 1 : n
x(k,:) = B(k,:)/U1(k,k);
end
x
toc
wynik:
x =
2
-2
1
-1
Elapsed time is 0.015360 seconds.
Wnioski