Professional Documents
Culture Documents
Jacobi
Jacobi
function Jacobi_Method()
%system of equations
%% -----INPUT-----
A = [ ];
b = [];
itr = 15;
%Unless otherwise specified, the intitial guess x_0 will be all zeros
x0 = zeros(1,length(A));
%% -----SOLUTION-----
n = length(A);
x = [x0;zeros(itr,n)];
%The iterative method below will run for the specified number of iteration
for k = 1:itr
for i = 1:n
%We define a variable "sigma" that will be used to sum the values
%that are known (not on the main diagonal of the matrix) for each
%equation)
sigma = 0;
for j = 1:n
if i~=j
end
end
%Lastly, the x_i is calculated from the equation and recorded under
x(k+1,i) = (b(i)-sigma)/A(i,i);
end
end
%% -----OUTPUT-----
%For output purposes, 'k' will be defined to show the iteration number
k = [0:itr]';
fprintf('\nSolution of the system is : \nx_1 =%9.5f \nx_2 =%9.5f \nx_3 =%9.5f \n', x(end,:))
table(k,x(:,1),x(:,2),x(:,3),...
'VariableNames',{'k','x_1','x_2','x_3'})
figure(1); hold on
plot(k,x(:,1),k,x(:,2),k,x(:,3))
legend('x_1','x_2','x_3')
xlabel('k')
ylabel('x')
hold of