Sie sind auf Seite 1von 6

PENYELESAIAN MENGGUNAKAN MATLAB

Penyelesaian dengan Jacobi :


Soal;
X 1 X 22 X 3=5
2 X 12 X 2 +3 X 3=1
3 X 12 X 2 +7 X 3 =20
function iterasi_jacobi(A, b, N)
%Jacobi(A, b, N) solve iteratively a system of linear equations
whereby
%A is the coefficient matrix, and b is the right-hand side column
vector.
%N is the maximum number of iterations.
%The method implemented is the Jacobi iterative.
%The starting vector is the null vector, but can be adjusted to
one's needs.
%The iterative form is based on the Jacobi transition/iteration
matrix
%Tj = inv(D)*(L+U) and the constant vector cj = inv(D)*b.
%The output is the solution vector x.
%This file follows the algorithmic guidelines given in the book
%Numerical Analysis, 7th Ed, by Burden & Faires
%Author: Alain G. Kapitho
%Date : Dec. 2005
%Rev. : Aug. 2007
n = size(A,1);
%splitting matrix A into the three matrices L, U and D
D = diag(diag(A));
L = tril(-A,-1);
U = triu(-A,1);
%transition matrix and constant vector used for iterations
Tj = inv(D)*(L+U);
cj = inv(D)*b;
tol = 15e-05;
k = 1;

x = zeros(n,1);

%starting vector

while k <= N
x(:,k+1) = Tj*x(:,k) + cj;
if norm(x(:,k+1)-x(:,k)) < tol
disp('The procedure was successful')
disp('Condition ||x^(k+1) - x^(k)|| < tol was met after k
iterations')
disp(k); disp('x = ');disp(x(:,k+1));
break
end
k = k+1;
end
if norm(x(:,k+1)- x(:,k)) > tol || k > N
disp('Maximum number of iterations reached without satisfying
condition:')
disp('||x^(k+1) - x^(k)|| < tol'); disp(tol);
disp('Please, examine the sequence of iterates')
disp('In case you observe convergence, then increase the
maximum number of iterations')
disp('In case of divergence, the matrix may not be diagonally
dominant')
disp(x');
end
(disave m-file dengan nama iterasi_jacobi(A, b, N))
Sumber:
http://m2matlabdb.ma.tum.de/download.jsp?MC_ID=3&MP_ID=407
Keterangan:
function jacobi(A, b, N)
a. A adalah matriks koefisien
b. b adalah matriks vector sebelah kanan tanda =
c. N adalah jumlah iterasi
d. Batas error yang digunakan ,tol=0,00015
Lalu jalankan program :
Pertama kali menginput matriks koefisien (A):
>> A=[1 -1 -2 ; 2 -2 1 ; 3 -2 7];
>> A
A=

1
2
3

-1
-2
-2

-2
1
7

Kemudian menginput matriks koefisien (b):


>> b=[5;1;20];
>> b
b=
5
1
20
Kemudian menginput nilai iterasi maksimal (N):
>> N=150
>> N
N=
150
Memanggil fungsi eksternal untuk menghitung iterasi Jacobi dengan inputan
seperti diatas dan menghasilkan data sebagai berikut sebagai berikut:
>> iterasi_jacobi(A, b, N)
The procedure was successful
Condition ||x^(k+1) - x^(k)|| < tol was met after k iterations
80
x=
29.8000
28.4000
-1.8000
Dan dapat disimpulan bahwa itrasi Jacobi , menghasilkan X1=29.8000 , X2= 28.4000,
X3=-1.8000 dengan iterasi ke -80

Dengan menggunakan Gauss Seidel :


function gauss_seidel_nizam(A, b, N)

%Gauss_seidel(A, b, N) solve iteratively a system of linear


equations
%whereby A is the coefficient matrix, and b is the right-hand
side column vector.
%N is the maximum number of iterations.
%The method implemented is the Gauss-Seidel iterative.
%The starting vector is the null vector, but can be adjusted to
one's needs.
%The iterative form is based on the Gauss-Seidel
transition/iteration matrix
%Tg = inv(D-L)*U and the constant vector cg = inv(D-L)*b.
%The output is the solution vector x.
%This file follows the algorithmic guidelines given in the book
%Numerical Analysis, 7th Ed, by Burden & Faires
%Author: Alain G. Kapitho
%Date : Dec. 2005
%Rev. : Aug. 2007
n = size(A,1);
%splitting matrix A into the three matrices L, U and D
D = diag(diag(A));
L = tril(-A,-1);
U = triu(-A,1);
%transition matrix and constant vector used for iterations
Tg = inv(D-L)*U;
cg = inv(D-L)*b;
tol = 2e-06;
k = 1;
x = zeros(n,1);

%starting vector

while k <= N
x(:,k+1) = Tg*x(:,k) + cg;
if norm(x(:,k+1)-x(:,k)) < tol
disp('The procedure was successful')
disp('Condition ||x^(k+1) - x^(k)|| < tol was met after k
iterations')
disp(k); disp('x = ');disp(x(:,k+1));
break
end
k = k+1;
end

if norm(x(:,k+1)- x(:,k)) > tol || k > N


disp('Maximum number of iterations reached without satisfying
condition:')
disp('||x^(k+1) - x^(k)|| < tol'); disp(tol);
disp('Please, examine the sequence of iterates')
disp('In case you observe convergence, then increase the
maximum number of iterations')
disp('In case of divergence, the matrix may not be diagonally
dominant')
disp(x');
end
(save m-file dengan nama gauss_seidel_Fandy)
Sumber:
http://m2matlabdb.ma.tum.de/download.jsp?MC_ID=3&MP_ID=406
A adalah matriks koefisien
b adalah matriks vector sebelah kanan tanda =
N adalah jumlah iterasi
Batas error yang digunakan ,tol=0,000002
Eksekusi program:
Pertama kali menginput matriks koefisien (A):
>> A=[1 -1 -2 ; 2 -2 1 ; 3 -2 7];
>> A
A=
1 -1 -2
2 -2 1
3 -2 7
Kemudian menginput matriks koefisien (b):
>> b=[5;1;20];
>> b
b=
5
1
20
Kemudian menginput nilai iterasi maksimal (N):
>> N=150;
>> N
N=

150
Terakhir memanggil fungsi eksternal untuk menghitung iterasi Gauss Seidel yang
telah disimpan sehingga dihasilkan sebagai berikut:
>> gauss_seidel_nizam(A, b, N)
The procedure was successful
Condition ||x^(k+1) - x^(k)|| < tol was met after k iterations
24
x=
29.8000
28.4000
-1.8000
Dari hasil diatas bahwa hasil iterasi adalah
. Dan hasil di dapatkan pada iterasi ke 24

X 1=29.8000 ; X 2=28.4000 ; X 3=1.8000

Das könnte Ihnen auch gefallen