function Control1_2(model)
% Solution to problem 2 of control 1
% J.M.Soler, Oct.2018

% Set parameters
if model=='A'
    s = 0.25;    % hit probability
    c = 0.2;     % cost per trial (euro)
    g = 0.7;     % gain per hit (euro)
elseif model=='B'
    s = 0.2;
    c = 0.15;
    g = 0.65;
elseif model=='C'
    s = 0.1;
    c = 0.25;
    g = 0.75;
elseif model=='D'
    s = 0.125;
    c = 0.15;
    g = 0.65;
else
    error('Control1_2 ERROR: bad model')
end  
tmax = 20;       % max number of trials
bmin = 5;        % minimum desired benefit (euros)

% Find hit probabilities and benefits
for t = 1:tmax
    for a = 0:tmax
        [P(t,a+1),B(t,a+1)] = F_Probabilidad_Beneficio(t,a,s,c,g);
    end
end

% Find averages
bavg = sum(P.*B,2);        % average benefit (sum over lines)
bmin = bmin-10*eps;        % 10*eps tolerance for numerical errors
p5 = sum(P.*(B>=bmin),2);  % prob. that benefit>=bmin

% Plot results
t = 1:tmax;
figure(1)
plot(t,bavg,'^--')
xlabel('Number of trials')
ylabel('Mean benefit')
figure(2)
plot(t,p5,'s-')
xlabel('Number of trials')
ylabel('Prob(benefit > 5 euro)')
