title: How to code a general Gauss-seidel method
body: <p>How can I program a Matlab code for Gauss-Seidel method that able the users to input any number of equations, n and any input for the matrix coefficient?
  I did this, but this is for a fix number of equations and known equation's coefficients.
  I need a code that where both number of equations, n and the value of coefficients (a11, a12, ..) are given by user.</p>
  
  <pre><code> clear;clc;format('long','g');
   i=1;
   x2(i)=32; x3(i)=41; x4(i)=12;
   error_x1(i)=100;
  
   while error_x1(i) &gt;= 0.01
   x1(i+1)=(21-x2(i)+42*x3(i)-9*x4(i))/-2;
   x2(i+1)=(-3-4*x1(i)-x3(i)+x4(i))/-12;
   x3(i+1)=(-24-2*x1(i)-6*x2(i)-18*x4(i))/-2;
   x4(i+1)=(41-21*x1(i)+2*x2(i)-x3(i))/-1;
  
   error_x1(i+1)=abs((x1(i+1)-x1(i))/x1(i+1))*100;
   error_x2(i+1)=abs((x2(i+1)-x2(i))/x2(i+1))*100;
   error_x3(i+1)=abs((x3(i+1)-x3(i))/x3(i+1))*100;
   error_x4(i+1)=abs((x4(i+1)-x4(i))/x4(i+1))*100;
  
   i=i+1;
   end
   disp('                  x1                    error(%)');
   disp([x1',error_x1'])
   disp('                  x2                    error(%)');
   disp([x2',error_x2'])
   disp('                  x3                    error(%)');
   disp([x3',error_x3'])
   disp('                  x4                    error(%)');
   disp([x4',error_x4'
  </code></pre>
  
  <p>please... help </p>
  
  <p>@DougLipinski, thanks for your help and suggestions. From the code above, it's been changed to the code below.
  Basically it's like this.</p>
  
  <pre><code>while (n&lt;maxit) &amp;&amp; (max(err)&gt;tol)
  n = n + 1;%no. of iteration
  for i = 1:n_eq
      x(i) = (b(i) - (A(i,:) * x)) / A(i,i) + x(i);%Gauss-Seidel method
  
      x_old = x;%roots
  end
      err = abs((x - x_old) ./ x);%checking for error
  end
  </code></pre>
  
  <p>to ask input from user, use the input command
  example:</p>
  
  <pre><code> A = input('Give matrix A');
  </code></pre>
  
  <p>and so on.
  Good luck.
  Credit: Matteo Carpentieri (Uni of Surrey)</p>