%% Synaptic conductance model to simulate an EPSP
% Taken from Gabbiani & Cox,  Mathematics for Neuroscientists
% 
% Materials for Neuroscience (course a6m33nin) at dept. of Cybernetics, 
% Faculty of Electrical Engineering, CTU Prague, 2012
 
 
% initialization
g_syn=[]; I_syn=[]; v_m=[]; t=[];

%% Setting some constants and initial values
 c_m=1; g_L=1; tau_syn=1; E_syn=10; delta_t=0.01;  
 g_syn(1)=0; I_syn(1)=0; v_m(1)=0; t(1)=0; 
 

%% Numerical integration using Euler scheme
 for i=2:1000/delta_t
   t(i)=t(i-1)+delta_t;
   if abs(t(i)-1)<0.001; g_syn(i-1)=1; end  %opening of ion channel after neurotransimetter binding at t=1ms
    g_syn(i)= g_syn(i-1) - delta_t/tau_syn * g_syn(i-1);
    I_syn(i)= g_syn(i) * (v_m(i-1)-E_syn);
    v_m(i) = v_m(i-1) - delta_t/c_m *g_L* v_m(i-1) ...
               - delta_t/c_m * I_syn(i);
 end

%% Plotting results
 figure; hold on; 
 plot(t,v_m); 
 plot(t,g_syn*5,'r--'); 
 plot(t,I_syn/5,'k:');
 
 xlabel('t(ms)')
 ylabel('scaled quantities [-]')
 title('Model of post-synaptic potential (EPSP)')
 legend('V_m','g_{syn}','I_{syn}')
