% Illustrating Gauss-Seidel 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+1) + z(i))/8; z(i+1) = (15 + 2 * x(i+1) - y(i+1)) / 5; % Check to see if we have converged, look up 'help norm' to find out % what it does. if ( norm([x(i+1) y(i+1) z(i+1)]-[x(i) y(i) z(i)],2) < TOLERANCE ) break; end end % Plot convergence figure set(gca,'FontSize',16) plot(x,'o-') hold on plot(y,'ro-') plot(z,'ko-') legend('x','y','z') axis([1 i 0 4.1]) title('Convergence of GS Method') xlabel('Iteration'); ylabel('Value of (x y z)');