I am trying to solve the system by Jacobi method.
\begin{align*} 5x-y+z&=10\\ 2x+8y-z&=11\\ -x+y+4z&=3 \end{align*}
So I wrote the following Matlab code.
Now when I run this, only one iterate is given. But I am expecting multiple iterates approaching exact solution.
But when I use "for" loop, it gives multiple iterates.

What am I doing wrong with "while" loop?
At the end of your while loop, you set
x0=xwhich means thatx-x0=0up to machine precision. This means thatnorm(x-x0)=norm(0)which will obviously satisfy the while condition and break from the loop. Your for loop does not have this problem, since you computexbefore setting it equal tox0with your if comparison. Basically, your while loop should work if you switch the order of your two statements.