%% Synaptic conductance model to simulate an EPSP
 clear; close all; hold on;

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

%% Numerical integration using Euler scheme
 for i=2:10/dt
  % record the time (in ms) in slot i of vector t 
   t(i)=t(i-1)+dt;
  % simulate the opening of the (transmitter-gated) ion chanels at t=1ms;  
   if abs(t(i)-1)<0.0001; 
       g_syn(i-1)=1
   end
  % calculate the currents at this time 
   I_L(i)= g_L * (v_m(i-1)-E_L); % leakage cannel 
   I_syn(i)= g_syn(i-1) * (v_m(i-1)-E_syn); % synaptic cannel 

  %update differential equations
   g_syn(i)= g_syn(i-1) - dt/tau_syn * g_syn(i-1);
   v_m(i) = v_m(i-1) - dt/c_m*(I_L(i)+I_syn(i));
 end

%% Plotting results
 plot(t,v_m,'k'); plot(t,g_syn*3,'r--'); 
 plot(t,I_syn/5,'b:');plot(t,I_L/2,'g:'); 