
function D = diagonale(A)
    D=[]
    s=size(A)
    for i=1:s(1)
        D=[D,A(i,i)]
    end
endfunction

function M = EplusF(A)
    M=-A
    s=size(A)
    for i=1:s(1)
        M(i,i)=0
    end
endfunction

function s=normInf(x)
    s=0
    for i=1:length(x)
        if s < abs(x(i))
            s=abs(x(i))
        end 
    end
endfunction

function x = Jacobi(A,b,tol)
    x0=zeros(length(b),1)
    x1=x0
    while normInf(A*x0-b) > tol
        x1= (diagonale(A)').^(-1).*(b+EplusF(A)*x0)
        x0=x1
    end
    x=x0
endfunction

function x=GaussSeidel(A,b,tol)
    x0=zeros(length(b),1)
    x1=x0
    while normInf(A*x0-b) > tol
        for i=1:length(b)
            s=0
            for j=1:(i-1)
                s=s+A(i,j)*x1(j)
            end
            for j=(i+1):length(b)
                s=s+A(i,j)*x0(j)
            end
            x1(i)=(1/A(i,i))*(b(i)-s)
        end
        x0=x1
    end
    x=x0
endfunction

function A = tridiag(c1,c2,c3,n)
    A=zeros(n,n)
    A(1,1)=c1
    A(1,2)=c3
    A(1,n)=c2
    A(n,1)=c3
    A(n,n-1)=c2
    A(n,n)=c1
    for i=2:(n-1)
        A(i,i-1)=c2
        A(i,i)=c1
        A(i,i+1)=c3
    end
endfunction    
