%Oppgave 1 + oppgave 2a

%Kode for aa plotte a_k (eller b_k hvis man endrer linje 12, se kommentar):
clear all
set(0,'DefaultAxesFontSize',18);
N=100;
p=[0.45,0.49,0.50,0.51,0.55];
q=1-p;
A=zeros(N-1,length(p));
for i=1:length(p)
    C=sparse(1:(N-1),1:(N-1),ones(1,(N-1)),N-1,N-1);%matrise m -1 paa diagonalen, 0 ellers
    L=sparse(2:(N-1),1:(N-2),-q(i)*ones(1,N-2),N-1,N-1);%matrise m q(i) paa subdiagonalen, 0 ellers
    U=sparse(1:(N-2),2:(N-1),-p(i)*ones(1,N-2),N-1,N-1);%matrise m p(i) paa superdiagonalen, 0 ellers
    D=L+U+C;%matrise med -q(i) paa subdiagonalen, 1 paa diagonalen, -p(i) paa superdiagonalen og 0 ellers
    d=zeros(N-1,1);%vektor med bare 0-er
    d(1)=q(i);%foerste element i d blir q(i), naar man skal regne ut b_k-ene setter man istedet d(N-1)=p(i)
    a=D\d;%inverterer D og multipliserer med d
    A(1:(N-1),i)=a;
end
plot([1:N-1],A);%plotter
xlabel('Initial capital');
ylabel('a_k');

%Skriver ut a_50 (til oppgave 2a):
A(50,1:length(p))
%ans =

%    1.0000    0.8808    0.5000    0.1192    0.0000

%Kode for aa plotte tau_k
clear all
set(0,'DefaultAxesFontSize',18);
N=100;
p=[0.45,0.49,0.50,0.51,0.55];
q=1-p;
T=zeros(N-1,length(p));
for i=1:length(p)
    C=sparse(1:(N-1),1:(N-1),ones(1,(N-1)),N-1,N-1);%matrise m 1 paa diagonalen, 0 ellers
    L=sparse(2:(N-1),1:(N-2),-q(i)*ones(1,N-2),N-1,N-1);%matrise m -q(i) paa subdiagonalen, 0 ellers
    U=sparse(1:(N-2),2:(N-1),-p(i)*ones(1,N-2),N-1,N-1);%matrise m -p(i) paa superdiagonalen, 0 ellers
    D=L+U+C;%matrise med -q(i) paa subdiagonalen, 1 paa diagonalen, -p(i) paa superdiagonalen og 0 ellers
    D=L+U+C;
    d=ones(N-1,1);%vektor med bare 1-ere
    t=D\d;%inverterer D og multipliserer med d
    T(1:(N-1),i)=t;
end
plot([1:N-1],T);
xlabel('Initial capital');
ylabel('tau_k');

%Skriver ut tau_50 (til oppgave 2a):
T(50,1:length(p))
%ans =

% 500    1904    2500    1904    500
