% Supplementary Matlab files for the paper:
%
% " Deterministic Modelling and Stochastic Simulation of Pathways using Matlab"
% by M.Ullah, H.Schmidt, K-H.Cho and O.Wolkenhauer
%
% This script file simulates the calcium example. 
%
% We would appreciate a citation if these files are used by others.


rand('state',sum(100*clock))
[c0,c1,v1,v2,v3,k3,d1,d2,d3,d5,a2,tmax,IP3,nT,dt] = ...
    deal(2,.185,6,.11,.9,.1,.13,1.049,.94,.082,.2,500,.3,20,.01);
k3sq = k3^2; 
c1p = c1+1; 
minf3 = (IP3/(IP3 + d1))^3/nT;
ahdt = a2*d2*(IP3 + d1)/(IP3 + d3)*dt; 
a2dt = a2*dt;
t = (0:dt:tmax).'; 
nmax = numel(t)-1; 
zerovec = zeros(nmax,1);
isopen = unidrnd(2,nT,3) - 1; 
ca = [.1; zerovec];
hopen = [numel(nonzeros(all(isopen,2)))/nT; zerovec];
for i = 1:nmax
    ninf3 = (ca(i)/(ca(i)+d5))^3;
    J1 = (v1*minf3*ninf3*hopen(i)+v2)*(c0-c1p*ca(i));
    J2 = v3*ca(i)^2/(k3sq + ca(i)^2);
    ca(i+1) = ca(i) + (J1-J2)*dt;
    bhdt = a2dt*ca(i);
    y = rand(nT,3);
    id = (isopen & y<bhdt) | (~isopen & y<ahdt);
    isopen(id) = ~isopen(id);
    hopen(i+1) = numel(nonzeros(all(isopen,2)));
end

fig = figure('PaperPosition',[2 2 8 6.3],'PaperType','A4');

ax1 = axes('FontName','cmr','FontSize',8,'Position',[0.16 0.1765 0.78 0.34],'Parent',fig);
stairs(ax1,t,hopen/nT);
xlim(ax1,[300 500]);
xlabel(ax1,'time  (sec)');
ylabel(ax1,'{\ith}-open fraction');

ax2 = axes('FontName','cmr','FontSize',8,'Position',[0.16 0.5734 0.78 0.34],'Parent',fig);
stairs(ax2,t,ca);
set(ax2,'Xlim',[300 500],'XTickLabel',{})
ylabel(ax2,'{[Ca^2+] (\mu M)}');

% print  -depsc 'figures\calcium.eps'