% 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 % Convergent iteration scheme x(i+1) = -1*(15 - y(i) -5*z(i))/2; y(i+1) = -1*(-21 - 4*x(i) - z(i))/8; z(i+1) = 7 - 4*x(i) + y(i); % 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)'); axis([1 15 -10^6 10^6]) %set axis limits; use help to see more on this