% Illustrating Jacobi's Method % Based on code from Kyle Mandli clear all; close all; % Settings TOLERANCE = 10^(-7); max_iterations = 1000; % Initial guess x(1)=1; y(1)=2; z(1)=2; for i=1:max_iterations-1 % Implement iteration scheme x(i+1) = (7 + y(i) - z(i))/4; y(i+1) = (21 + 4*x(i) + z(i))/8; z(i+1) = (15 + 2 * x(i) - y(i)) / 5; % Check to see if we have converged yet. Here, norm checks for % Euclidean distance between solution on subsequent iterations -- type % help norm! if ( norm([x(i+1) y(i+1) z(i+1)]-[x(i) y(i) z(i)],2) < TOLERANCE ) break; end end figure % Plot convergence set(gca,'FontSize',16) plot(x,'o-') hold on plot(y,'ro-') plot(z,'ko-') legend('x','y','z') title('Convergence of Jacobi Method') xlabel('Iteration'); ylabel('Value of (x y z)');