function dummy
%############################################
%Set the precision to be high. You may need to do this a 
%few times in the code, e.g. within functions...
format long
%Now we write (just for example) a function that calculates the 
%dot product of two vectors. This is not used in your code, but 
%should give you an idea for writing a function that calculates
%the _euclidean_ distance between 2 vectors
function result=dot(v,w)
  format long
  result = 0
  for i=1:length(v)
    result=result+v(i)*w(i)
  end
end
function result=dist(p,q)
    format long
    result=abs(p(1)-q(1))
end
%The above is a dummy "distance" function. YOU NEED TO 
%WRITE YOUR OWN ONE.
%############################################################
%Now lets do a Jacobi iteration on a simple 2x2 matrix. I'm 
%assuming here you will have written a function dist() for the 
%distance between 2 vectors
A = [2 -1; -1 2];
%This defines the 2x2 matrix
D=diag(diag(A))
%###############################################################
%diag(A) gives a column vector of the diagonal elements of A, if 
%A is a matrix. For a vector v, diag(v) gives a diagonal matrix 
%whose diagonal elements are the elements of v
%################################################################
R=A-D
%calculate the remainder matrix R
b=[2;2]
Dinv=diag(1./diag(A))
%this gives the inverse of matrix D. The operator "./" takes the 
%elementwise inverse of each diagonal element.
P=Dinv*b
Q=-Dinv*R
%These are just convenience operations to simplify the formula
%Dinv*b-Dinv*R*x - this becomes now P+Qx
xo=[0;0]
%our starting point is the vector (guess) [0;0]. xo stands for
%"old x"
xn=P+Q*xo
%calculate "new x"
%#############################################################
while (dist(xo,xn)>0.1)
%in this example we calculate iterations until the error is less than 0.1 
  xo=xn
  xn=P+Q*xn
%we update "x old" and "x new" to their new values
end
end
