%Oppgave 2b
clear all
p=[0.45,0.49,0.50,0.51,0.55];
q=1-p;
sim=1000;
N=100;
X0=50;
nulltell=zeros(1,length(p));
tau.emp=zeros(1,length(p));
for i=1:length(p)
    Xlength=zeros(1,sim);
    for j=1:sim
        test=1;
        X=X0;
        while test
            if rand<p(i)
                X=[X,X(length(X))+1];
            else
                X=[X,X(length(X))-1];
            end
	    if X(length(X))==0
                test=0;
                nulltell(i)=nulltell(i)+1;
            elseif X(length(X))==N
                test=0;
            end
        end
        %Skal plotte 5 av realisasjonene for p=0.45
        if p(i)==0.45 & j<=5
            plot([1:length(X)],X);%plotter
            hold on
            xlabel('Tid');
            ylabel('Tilstand');
        end
        Xlength(j)=length(X)-1;
    end
    hold off
    tau.emp(i)=mean(Xlength);
end
%Antall ganger simuleringene ender i 0 for hver p:
nulltell
%nulltell =
%        1000         882         466         112           0
%Gjennomsnittlig tid til absorpsjon i 0 eller N for hver p:
tau.emp
%ans =

%    495    1874    2560    1946    493
