% Evaluating the graph in problem 1
% A. Sun  4/2/05

clear;
clf;

N=30; % iterations
Y=.9;
X(1)=.9;

% no damping
A(1)=1-X(1);
for i = 2:N, 
    X(i)=1-A(i-1);
    %A(i)=Y*X(i)/(Y*X(i)+(1-Y)*(1-X(i)));
    one=Y*X(i);
    zero=(1-Y)*(1-X(i));
    A(i)=one/(one+zero);
end

plot(X,'k:')
title('Graph with Cycles');
xlabel('iterations');
ylabel('Px(1)');
axis([0,N,0,1]);
hold on;

% with damping (average with last)
B(1)=1-X(1);
for i = 2:N, 
    X(i)=(1-B(i-1));
    one=Y*((X(i)+X(i-1))/2);
    zero=(1-Y)*(1-(X(i)+X(i-1))/2);
    B(i)=one/(one+zero);
end

s3=['red = averaged with prev value = ' num2str(X(N))]
plot(X,'r')

% with damping (average with half last)
B(1)=1-X(1);
B(1)=1-X(1);
for i = 2:N, 
    X(i)=(1-B(i-1));
    one=Y*((X(i)+0.5*X(i-1))/2);
    zero=(1-Y)*(1-(X(i)+0.5*X(i-1))/2);
    B(i)=one/(one+zero);
end

s4=['green = averaged with half prev value = ' num2str(X(N))]
plot(X,'g')

% with minimal damping 
B(1)=1-X(1);
for i = 2:N, 
    X(i)=(1-B(i-1));
    one=Y*((X(i)+0.001*X(i-1))/2);
    zero=(1-Y)*(1-(X(i)+0.001*X(i-1))/2);
    B(i)=one/(one+zero);
end


s5=['blue = minimally damped =' num2str(X(N))]
plot(X,'b')

t_x=0.6*N;
t_y=.9

s1=['starting x value= ' num2str(X(1))];

text(t_x,t_y,s1);
text(t_x,t_y-0.05,'black = undamped');
text(t_x,t_y-.1,s3);
text(t_x,t_y-.15,s4);
text(t_x,t_y-.2,s5);


hold off;