Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
MATLAB Codes for Jacobi method
1. PRACTICAL
Name- Saloni Singhal
M.Sc. (Statistics) II-Sem.
Roll No: 2046398
Course- MATH-409 L
Numerical Analysis Lab
Submitted To: Dr. S.C. Pandey
2. OBJECTIVE
1. Create an M-file to implement Gauss Jacobi
method.
2. Write both the script file and function for the
program created.
3. Theory
Diagonal Dominance:
• Iterative Method can be applied when diagonal
dominance exist that is when absolute value of its
leading diagonal elements is greater than or equal to
the sum of absolute value of the remaining element.
• It is a method of solving a matrix equation that has no
zeros along its main diagonal. Each diagonal element is
solved for, and an approximate value plugged in. The
process is then iterated until it converges.
• Applied to non square matrix also as opposed to the direct
methods.
• The method is easily derived by examining each of
the Xn equations
4. Script File
% defines matrix A
A = [2 1 1; 3 5 2;2 1 4];
% defines vector b
b = [5;15;8]
% solves linear system (i.e. solves Ax=b
for x)
[m n]=size(A);
x=zeros(n,1);
if m~=n
fprintf("incorrect size")
exit
end
aug=[A b];
%diagonal dominance
for i=1:n-1
for j=i:n
[a b]=max(aug(j,1:n));
if b==i
temp=aug(i,:);
aug(i,:)=aug(j,:);
aug(j,:)=temp;
break
end
end
end
5. Script File Contd.
%check diag dominance
for i=1:n
[a b]=max(aug(i,1:n));
if a~=aug(i,i)
fprintf("divergent")
exit
end
end
%assign tolerance
error=1;
p=0;
while error>=0.01
xold=x;
p=p+1;
fprintf("x%d:",p);
xold
%approximations
for i=1:n
augx=[aug(i,1:i-1) aug(i,i+1:n)];
xx=[xold(1:i-1) xold(i+1:n)];
x(i)=(aug(i,n+1)-dot(augx,xx))/aug(i,i);
end
xold=abs(x-xold);
error=max(xold);
end
x
error
6. Function
File
function [x,convergence] = seidelA,b)
[m n]=size(A);
x=zeros(n,1);
if m~=n
fprintf("incorrect size")
exit
end
aug=[A b];
%converting into strictly or partially
diagonal dominant
for i=1:n-1
for j=i:n
[a b]=max(aug(j,1:n));
if b==i
temp=aug(i,:);
aug(i,:)=aug(j,:);
aug(j,:)=temp;
break
end
end
end
7. Program Contd.
%check diag dom
for i=1:n
[a b]=max(aug(i,1:n));
if a~=aug(i,i)
fprintf("divergent")
exit
end
end
%setting tolerance limit for terminating iterations
error=1;
p=0;
while error>=0.01
xold=x;
p=p+1;
fprintf("x%d:",p);
xold
%approximations
for i=1:n
augx=[aug(i,1:i-1) aug(i,i+1:n)];
%xold takes initial values calculated in successive
iterations
xx=[xold(1:i-1) xold(i+1:n)];
x(i)=(aug(i,n+1)-dot(augx,xx))/aug(i,i);
end
xold=abs(x-xold);
error=max(xold);
end
x
error
end
9. Caveats
• Iterative Method can only be used to
give solutions when the system of
linear equation is convergent or
diagonally dominant.
• The Jacobi iterative method works
fine with well-conditioned linear
systems. If the linear system is ill-
conditioned, it is most probably that
the Jacobi method will fail to
converge.