Jacobi method in MATLAB
47 views (last 30 days)
Show older comments
I have to write two separate codes for the Jacobi method and Gauss-Seidel
The question exactly is: "Write a computer program to perform jacobi iteration for the system of equations given. Use x1=x2=x3=0 as the starting solution. The program should prompt the user to input the convergence criteria value, number of equations and the max number of iterations allowed and should output the solution along with the number of iterations it took for the solution to convergence to the user specified value."
Jacobi Method:
%*************************Eric Douglas*****************************%
%***********************August 30th, 2013**************************%
%This code is used to compute the Jacobi Method of a certain matrix.%
%Input:
% C_n = convergence criteria value
% N = number of equations in the matrix
% Imax = the maximum number of iterations
%Output:
% S = the solution( M x 1 matrix ; jacobi approximation)
% j = the number of iterations it took to
% converge to the user inputed value
% R = Residual Values
%establishes the variables needed
%B is an M x 1 matrix
%A is an M x M matrix
%P is the initial M x 1 matrix
%Z = remembering matrix
K = 0;%Initialize K to be 0
Z = K;%receives value from the previous matrix iterations
%Ask the user for each input statement required
Imax = input('What do you want the maximum iteration to be? ');
N = input('How many equations do you want? ');
C_n = input('What value do you want to converge to? ');
sprintf('A is a %d x %d matrix',N,N);
L2 = 100;%Initializes L2 norm to be something larger than C_n
%Assigns the values inputed by the user into the matrices
for x=1:1:N
for y=1:1:N
strA = ['What do you desire your numbers in the matrix to be? ' num2str(x) 'Row: ' num2str(y) 'Column: '];
A = input(strA);
end
end
for l=1:1:N
strB = ('What do you desire the Solution matrix to be? ');
B = input(strB);
end
S = P;%Solution starts at the initial value given
P = zeros(N,1);%Initializes the first guess at N x 1 matrix of zeros
M = length(B);%tells the program the amount of iterations to perform
while j<Imax && L2>C_n
for j=1:1:Imax,%loops until Imax is reached
for i=1:1:M,%loops until M; M is the value it takes to get there
S(M,1)=(B(i,1)-A(i,[1:j-1,j+1:M])*P([1:j-1,j+1:M],1))/A(i,i);
Pn(M,1) = S(i,1);
end
P = Pn;%Assigns new Initialization value to compute the next it
R = A*S-B;%Assigns Ax - B residuals
%computes the Convergence criteria
for k=1:1:M
L2 = R(k)^2;
end
%computes the Convergence criteria
for i=1:1:M
L2= L2 + (R(i)^2);
end
%Loops to check L2 is still greater than the user inputed value
if(L2<C_n)
if(j<=Imax)
break
end
S = S^(j+1);
end
end
break
end
j
S
This is what I have so far with the Jacobi method. The problem that I need to fix has to deal with me printing out the correct number of iterations to get to the convergence number if that number is before the maximum iteration inputed by the user. Also, when I run through the code it seems to just do the first iteration and prints out that number.
If you could give me some help/give some hints that would be awesome.
0 Comments
Answers (3)
Walter Roberson
on 1 Sep 2013
"break" as the last statement of a "for" or "while" loop is often a mistake. Not always, but often.
0 Comments
zeryihun
on 21 Dec 2022
4x1+3x2- 3x4 = 6
2x1+ 2x2–x3+ 2x4=5
3x1+x2-4x3+x4= 7
-x1-2x2+ 3x3- 4x4= -9
0 Comments
Saud Saeed
on 1 Jun 2023
1. Jacobi’s Iteration method find the solution of the given set simultaneous linear equations: -
20x +y - 2z = 17 3x + 20y - z = -18 2x -3y + 20z = 25
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!