%========================================
% script recur (PROBLEM 1--PROJECT 1)
%==============================================
% Problem: compute the dominant and subdominant
% solutions of a linear, 2nd order recurrence
% with constant coefficients:
% Y(i+2) = a*Y(i+1) + b*Y(i)
% Solution: let x(i) = r^i then divide equation by r^i to get
% r^2 = a*r + b , a quadratic equation, with roots:
% rp = (a+sqrt(a^2+4*b))/2 ; rm = (a-sqrt(a^2+4*b))/2
% Since the recurrence is linear, any linear combination
% of the form:
% Y(n) = A*rp^n + B*rm^n
% satisfies it. So, for example, if we are given x(0), x(1),
% then find A, B by solving:
% Y(0) = A + B ; Y(1) = A*rp + B*rm
% Now, let's say that rp > rm (true if a,b>0, e.g.)
% then, for n large,:
% Y(n) = rp^n * (A + B * (rm/rp)^n) ---> A*rp^n
% since (rm/rp)<1 so that (rm/rp)^n ---> 0
% The two computations below demonstrate a subtler
% property of recurrences for the recurrence relation:
% Y(i+2) = Y(i+1) + Y(i)
% the first converges to rp, the second to rm. Why?
% What happens for the general case above? Answer
% by computations and explain by analysis of the problem.
% ( Hint: consider the ratios
% R(i) = Y(i+1)/Y(i) and r(i) = Y(i-1)/Y(i)
% What equation do they satisfy?)
clear
figure
m = 60
%ascending
x(1)=(1-sqrt(5))/2;
for i=2:m
x(i)=1+1/x(i-1);
end
for k = 1:4
subplot(2,2,k);
plot(1:(15*k),x(1:15*k)-x(1))
end
%descending
figure
x(1)=(1+sqrt(5))/2;
for i=2:m
x(i)=1/(x(i-1)-1);
end
for k = 1:4
subplot(2,2,k);
plot(1:(15*k),x(1:15*k)-x(1))
end