function dZM = neur2(t,ZMI)
%% funkcja do calkowania ukladu rownian
global I Transm;
C=0.001;
gL=5e-5;
EL=-78;
gT=0.00175;
ET=120.0;
gCaN=0.0003;
ECaN=-20;
gNa=0.10;
ENa=50;
gKCa=0.010;
EKCa=-95.0;
gK=0.010;
EK=-95.0;
gGABA=0.10;
EGABA=-80.0;

V=ZMI(1);
Ca=ZMI(2);
mT=ZMI(3);
hT=ZMI(4);
mKCa=ZMI(5);
mCaN=ZMI(6);
m=ZMI(7);
h=ZMI(8);
n=ZMI(9);
r=ZMI(10);

IL=gL*(V-EL);
IT=gT*mT*mT*hT*(V-ET);
IKCa=gKCa*mKCa*mKCa*(V-EKCa);
ICaN=gCaN*mCaN*mCaN*(V-ECaN);
INa = gNa*m*m*m*h*(V-ENa);
IK=gK*n*n*n*n*(V-EK);
IGABA=gGABA*r*(V-EK);

dV = (-IL-IT-IKCa-ICaN-INa-IK-IGABA+I)/C;
dCa=-10000*(5.2e-6)*IT-0.0001*Ca/(0.0001+Ca)+(2.4e-4-Ca)/1e10;
dmT = (mTinf(V)-mT)/taum(V);
dhT = (hTinf(V)-hT)/tauh(V);
dmKCa = (minfKCa(Ca)-mKCa)/tauKCa(Ca);
dmCaN=(minfCaN(Ca)-mCaN)/tauCaN(Ca);
dm=am(V)*(1-m)-bm(V)*m;
dh=ah(V)*(1-h)-bh(V)*h;
dn=an(V)*(1-n)-bn(V)*n;
dr=0.53*Transm*(1-r)-0.184*r;

dZM=[dV;dCa;dmT;dhT;dmKCa;dmCaN;dm;dh;dn;dr];

%% fumkcje pomocnicze
function y=an(V)
v=V+55;
y=0.032*vtrap(15-v,5);
%%***********************
function y=bn(V)
v=V+55;
y=0.5*exp((10-v)/40);
%%************************
function y=mTinf(V)
y=1./(1+exp(-(V+52)/7.4) );
%%************************
function y=hTinf(V)
y=1./(1+exp((V+80)/5.0) );
%%************************
function y=taum(V)
y=0.4349 + 0.145./( exp((V+27)/10.0) + exp(-(V+102)/15.0) );
%%*********************
function y=tauh(V) 
y=22.7 + 0.27./( exp((V+48)/4.0) + exp(-(V+407)/50.0) );
%%************************
function y=am(V)
v=V+55;
y=0.32*vtrap(13-v,4);
%%*************************
function y=bm(V)
v=V+55;
y=0.28*vtrap(v-40,5);
%%*************************
function y=ah(V)
v=V+55;
y=0.128*exp((17-v)/18);
%%*************************
function y=bh(V)
v=V+55;
y=4./(1+exp((40-v)/5));
%--------------------------------------------------%
function z=vtrap(x,y)
if abs(x./y) < 1e-6
z=y.*(1-x./y/2);
else
z=x./(exp(x./y)-1);
end
%%*************************
function y=minfKCa(Ca) 
cac=0.025;
car=(Ca/cac).^2;
y= car./(1+car);
%%*************************
function y=tauKCa(Ca) 
cac=0.025;
car=(Ca/cac).^2;
tadj=4.6555;
y=1/0.03/(1+car)/tadj;
%%*************************
function y= minfCaN(Ca)
y= 20*Ca*Ca./(20*Ca*Ca+0.002);
%%*************************
function y= tauCaN(Ca)
y= 1./(20*Ca*Ca+0.002)/4.6555;


