\setupcolors[state=start]
\input verb-matlab
\starttext
\startMATLAB
n = 150e5 ;
p = 0.9 ;

d = 300 ;
r = 100 ;

m_total = n:(n+40) ;

warning off;

%%. Refundable Tickets

cost = zeros(length(m_total),1) ;
for m = m_total 
    for k = 0:n
       cost(m-n+1) = cost(m-n+1) + k*d*nchoosek(m,k)*p^k*(1-p)^(m - k) ;
    end;
    for k = (n+1):m
        cost(m-n+1) = cost(m-n+1) + (n*d - (k-n)*r)*nchoosek(m,k)*p^k*(1-p)^(m-k) ;
    end;
end;
[value,count] = max(cost) ;
disp(sprintf('The maximum occurs at %d',count+n-1)) ;
figure(1);
plot(m_total,cost) ;

%%. Nonrefundable Case

cost = zeros(length(m_total),1) ;
for m = m_total 
    for k = 0:n
       cost(m-n+1) = cost(m-n+1) + k*d*nchoosek(m,k)*p^k*(1-p)^(m - k) ;
    end;
    for k = (n+1):m
        cost(m-n+1) = cost(m-n+1) + (m*d - (k-n)*(d+r))*nchoosek(m,k)*p^k*(1-p)^(m-k) ;
    end;
end;
[value,count] = max(cost) ;
disp(sprintf('The maximum occurs at %d',count+n-1)) ;
figure(2);
plot(m_total,cost) ;
\stopMATLAB
\stoptext

