function Control1_3(model)

% Exercise 3 of control 1, 30/10/2015
% Find and plot two random-like successions.
% Written as a function to include all models of control 1.
% Input:
%   model : ('A'|'B'|'C'|'D')
% Sample usage:
%   Control1_3('B')
% J.M.Soler, Oct.2015

% Set data
if model=='A'
    n = 15;
    m = 31;
    a = [7,9];
    b = [2,5];
elseif model=='B'
    n = 18;
    m = 31;
    a = [10,13];
    b = [9,7];
elseif model=='C'
    n = 23;
    m = 31;
    a = [2,16];
    b = [19,21];
elseif model=='D'
    n = 30;
    m = 31;
    a = [21,19];
    b = [23,23];
else
    error('Wrong model')
end
kmin = 950;
kmax = 1000;

% Find successions
for k = 3:kmax
    a(k) = mod(a(k-1)*a(k-2),n);
    if mod(b(k-1),2)==1
        b(k) = mod(b(k-1)*b(k-2),m);       % b(k-1) odd
    else
        b(k) = mod((b(k-1)-1)*b(k-2),m);   % b(k-1) even
    end
end

% Plot successions
figure(11)
k = 1:kmax;
plot(k,b)
xlabel('k')
ylabel('b_k')

figure(12)
k = kmin:kmax;
plot( k,a(k), k,b(k) )
legend('a_k','b_k')
xlabel('k')
ylabel('succession')
