function [TD,TO,CH,t,x,CS,ND]=demand(TD,TO,CH,CS,ND,t,x,h,p,l,L,s) ND=ND+1; %increment the number demanded by 1 CH=CH+x*h*(TD-t); %increment holding cost since last event t=TD; %update clock time if (x==0) CS=CS+p; %if no stock, add penalty p for unsatisfied demand elseif (x==s) %if stock=s, place order TO=t+L; % schedule time of arrival of order TO else end TD=TD+exprnd(1/l,1,1); %schedule next demand x=max(0,x-1); %reduce stock by 1 if positive