echo off;
%----------------------------------------------------------------------
% PHYSL 317 / BIOPH 317 / NEURO 317 / BIOENG 376
% Methods in Computational Neurobiology
% Fall 97
%
% hw1p2.m - Homework #1, Problem #2
%
% RC circuit simulation
%
% Simulate step responses of a family of RC circuits (different values of R).
%
% M. Nelson  Sept 1993, 1997
%----------------------------------------------------------------------


%%%%%%%%%%%%%%%%%%%
% Initialization
%%%%%%%%%%%%%%%%%%%

clear;						% clear MATLAB variables
clf;						% clear MATLAB graphcis

C = 1;						   % capacitance
RVAL = [ 2;       5;       10;      20;      50;]; % resistance values
PSYM = ['- '  ;  '--'  ;  '-.'  ;  ': '  ;  '- ']; % plot symbols


I_STEP = 1.0;					% current injection magnitude
I_ON = 10;					% injection start time
I_OFF = 60;					% injection stop time

TMAX = 100;					% simulation stop time
DT = 1;						% integration time step
t = 0:DT:TMAX;					% time vector

%%%%%%%%%%%%%%%%%%%%%%%%
% Main simulation loop
%%%%%%%%%%%%%%%%%%%%%%%%

for r = 1:length(RVAL)			% loop over different R values

  v(1) = 0;
  R = RVAL(r);

  for i=2:length(t)				% INTEGRATION LOOP
    I = I_STEP*(t(i)>I_ON & t(i)<=I_OFF);	% calculate injection current
    vdot = (-v(i-1)/R + I)/C;			% calculate dv/dt
    v(i) = v(i-1) + vdot*DT;			% Euler integration
  end						% END INTEGRATION LOOP


  plot(t,v,PSYM(r,:));				% plot and label it
  hold on					% hold plot for overlay

  xpos = 0.5*TMAX;			% label it at the halfway point
  ypos = v(fix(length(v)/2))-1;
  label_string = ['R=' num2str(RVAL(r))];   
  text(xpos, ypos, label_string);   

end					% end loop over R values

title ('HW 1.2: RC-Circuit Step Response')	% add graph title
xlabel('time');					% x-axis label
ylabel('voltage');				% y-axis label
