Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Algorithm to find approx solution of unknown variables in a system of
- % linear equations using Jacobi's method
- A = zeros(3, 3);
- C = zeros(3, 1);
- X = zeros(3, 1);
- X1 = 0; X2 = 0; X3 = 0; FLAG = 0;
- fprintf('Enter the coefficients of x1 one by one,\nthen the coefficients of x2, and so on:\n');
- for i = 1:9
- A(i) = input('Enter the element: ');
- end
- fprintf('\nEnter the vector of constants:\n');
- for i = 1:3
- C(i) = input('Enter the element: ');
- end
- ITR = input('Enter the number of iterations');
- TOL = input('Enter the stopping tolerance');
- for i = 1:ITR
- X(1) = (1 / A(1,1)) * (C(1) - A(1,2) * X2 - A(1,3) * X3);
- X(2) = (1 / A(2,2)) * (C(2) - A(2,1) * X1 - A(2,3) * X3);
- X(3) = (1 / A(3,3)) * (C(3) - A(3,1) * X1 - A(3,2) * X2);
- if (abs(X(1) - X1) < TOL) && (abs(X(2) - X2) < TOL) && (abs(X(3) - X3) < TOL)
- disp(round(X(1:3), 3))
- disp(i)
- FLAG = 1;
- break
- end
- X1 = X(1); X2 = X(2); X3 = X(3);
- end
- if FLAG == 0
- disp(round(X(1:3), 3))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement