function [ a, b, c] = hh( t, x, flag, I)
% ML: dynamics of Morris-Lecar model
% August 1999 by K. Doya
% modified from XPP tutorial

global	C gca gk gl eca ek el
global	v1 v2 v3 v4 phi

if isempty( flag)	% No flag: return xdot
	% state variables
	v=x(1); w=x(2);
	% input current
	if size(I,2) == 2	% (t,I) table
		It = interp1(I(:,1),I(:,2),t);
	else			% constant
		It = I(1);
	end
	% activation curves and rate constants
	minf = 0.5*(1+tanh((v-v1)/v2));
	winf = 0.5*(1+tanh((v-v3)/v4));
	lamw = phi*cosh((v-v3)/(2*v4));
	% xdot
	a = [ (It - gca*minf*(v-eca) - gk*w*(v-ek) - gl*(v-el))/C;
		lamw*(winf-w)];
	return;
end

switch( flag)
case 'init'			% Initialization
	% membrane capacitance (uF/cm^2)
	C=20;
	% maximum conductances (uS/cm^2)
	gca = 4;		% calcium
	gk = 8;			% potassium
	gl = 2;			% leak
	% reversal potentials (mV)
	eca = 120;		% calcium
	ek = -84;		% potassium
	el = -60;		% leak
	% parameters of activation curves and rate constants
	v1 = -1.2; v2 = 18;		% calcium
	% v3 = 12; v4 = 17; phi = 0.06666667;	% potassim, type I dynamics
	v3 = 2; v4 = 30; phi = 0.04;	% potassim, type II dynamics
	% return default values
	a = [0 500];	% default tspan
	b = [ -60.899; 0.014873];	% default initial v, w
	c = [];			% default option made by odeset()
case 'name'			% Variable names
	a = char( 'v', 'w');
otherwise
	error( [ ' invalid option: ', flag]);
end

% end of ml.m
