% ES2 tema esame 18 set 2014

A=[1/2  0  0  -1; 0 1  -1/3  0;  0  -1/4  1  0; -1  0  0 4/5];
b=A*ones(4,1);

B=eye(4)-A;
max(abs(eig(B)))

%ans= 1.3612

D=diag(diag(A));
E=-tril(A,-1);
F=-triu(A,1);
BJ=inv(D)*(E+F);
max(abs(eig(BJ)))

%ans = 1.5811

BGS=inv(D-E)*F;
max(abs(eig(BGS)))

%ans = 2.5000

P=eye(4);
P(4,:)=0; P(4,1)=1;
P(1,:)=0; P(1,4)=1;
Ap=P*A;
bp=P*b;

[xGS,iter]=gseidel(Ap,bp,zeros(4,1),300,1e-6);
norm(xGS-ones(4,1),2)

%ans = 9.6038e-007

[xJ,iter]=jacobi(Ap,bp,zeros(4,1),300,1e-6);
norm(xJ-ones(4,1),2)

%ans=  1.5185e-006