function Control1_1(version)
% Solution to problem 1 of control 1, Computacion I, 2017/11/03
% J.M.Soler, Oct.2017

% Set parameters
if version=='A'
  alpha = 0.578;
  theta = pi/6;
elseif version=='B'
  alpha = 0.578;
  theta = pi/8;
elseif version=='C'
  alpha = 0.594;
  theta = pi/11;
elseif version=='D'
  alpha = 0.676;
  theta = -2*pi/5;
end

% Set matrix A and vectors s and V1
nv = 1000;
s = zeros(4,1);
V = zeros(4,nv);
for n=1:4
  for m = 1:4
    A(n,m) = alpha*(-1)^(n+m)*cos(m*(n*theta-pi/2));
  end
  s(n) = (-1)^n*sqrt(alpha*n);
  V(n,1) = alpha/(n+2)*cos(theta^n+pi/2);
end

% Find matrix V
for iv = 2:nv
  V(:,iv) = A*V(:,iv-1)+s;
end

% Plot V2 vs V1 and V4 vs V3
figure(1)
plot(V(1,:),V(2,:),'r.',V(3,2:2:nv),V(4,2:2:nv),'g.')
legend('v2 vs v1','v4 vs v3 even')
xlabel('v1,v3')
ylabel('v2,v4')
axis equal
grid on

% Find how many times |v1|>|v4|
times_v1_gt_v4 = sum(abs(V(1,:))>abs(V(4,:)))

end
