clear
metode=3;
n=10;
h=1/n;
k=1/(4*n^2);

%matrisen
a=-4*eye(n-1)+diag(ones(n-2,1),1)+diag(ones(n-2,1),-1);
A=a;
for i = 1:n-2
    A=blkdiag(A,a);
end
A=A+diag(ones((n-1)^2-(n-1),1),n-1)+diag(ones((n-1)^2-(n-1),1),-(n-1));

%randkrav
f=zeros((n-1)^2,1);
f(1:n-1)=sin(pi*(.1:.1:.9));

%litt deklarering av variable
v=zeros((n-1)^2,1);
u=zeros(n+1,n+1);
u(1:n+1,1)=sin(pi*(0:.1:1));

if metode==1
    %eksplisitt skjema
    for i = 1:50
        v=(eye((n-1)^2)+k/h^2*A)*v+k/h^2*f;
        u(2:n,2:n)=reshape(v,n-1,n-1);
        [x,y]=meshgrid(0:.1:1,0:.1:1);
        surf(x,y,u)
        pause(.1)
    end
end

if metode==2
    %implisitt skjema
    for i = 1:50
        v=(eye((n-1)^2)-k/h^2*A)\(v+k/h^2*f);
        u(2:n,2:n)=reshape(v,n-1,n-1);
        [x,y]=meshgrid(0:.1:1,0:.1:1);
        surf(x,y,u)
        pause(.1)
    end
end

if metode==3
    %crank-nicolson
    for i = 1:50
        v=(eye((n-1)^2)-k/(2*h^2)*A)\((eye((n-1)^2)+k/(2*h^2)*A)*v+k/(2*h^2)*f)+k/(2*h^2)*f;
        u(2:n,2:n)=reshape(v,n-1,n-1);
        [x,y]=meshgrid(0:.1:1,0:.1:1);
        surf(x,y,u)
        pause(.1)
    end
end
