% Nernst
% with numberMolculesIn & movement based on charge Gradient
% Determine the grid size and generate the diffusion.
  N = input('Enter Time:');
  nRedMolecules=4;  %Number of Red Particles
  permRed=.1;  %permeability of red ions
  nBlueMolecules=4;
  permBlue=.5; %
  nGreenMolecules=4;
  permGreen=0.1; %permeability of Chloride ions
  membrane=0; %Membrane at x=0
  posRed='In';
  [a b]=DiffusionMemPos(N,membrane,permRed,posRed);
  xRed=[a];
  xRedLength=length(xRed);
  yRed=[b];
  redIn=3;
  for red=2:nRedMolecules
      if red>redIn-0.5;
          posRed='Out';
      end
      [c d] = DiffusionMemPos(N,membrane,permRed,posRed);
      xRed=[xRed;c];
      yRed=[yRed;d];
  end
  [positiveInR positiveOutR] =numberMoleculesIn(N,membrane,xRed,permRed,nRedMolecules);
  posBlue='In';
  [e f]=DiffusionMemPos(N,membrane,permBlue,posBlue); %Initial array for blue particles
  xBlue=[e]; %Initial X matrix for Blue Particles
  xBlueLength=length(xBlue);
  yBlue=[f]; %Initial y Martix for Blue particles
  blueIn=1;
  for blue=2:nBlueMolecules
      if blue>blueIn-0.5
          posBlue='Out';
      end
      [g h] = DiffusionMemPos(N,membrane,permBlue,posBlue);
      xBlue=[xBlue;g]; %Concatinate each x row
      yBlue=[yBlue;h]; %Concatinate each y row
  end
  [positiveInB positiveOutB] =numberMoleculesIn(N,membrane,xBlue,permBlue,nBlueMolecules);
  positiveIn=positiveInR + positiveInB;
  positiveOut=positiveOutR + positiveOutB;
  posGreen='In';
  [i j]=DiffusionMemPos(N,membrane,permGreen,posGreen); %Initial array for green particles
  xGreen=[i]; %Initial X matrix for Green Particles
  xGreenLength=length(xGreen);
  yGreen=[j]; %Initial y Martix for Green particles
  greenIn=1;
  for green=2:nGreenMolecules
      if green>greenIn-0.5
          posGreen='Out';
      end
      [k l] = DiffusionMemPos(N,membrane,permBlue,posGreen);
      xGreen=[xGreen;k]; %Concatinate each x row
      yGreen=[yGreen;l]; %Concatinate each y row
  end
  [negativeIn negativeOut] =numberMoleculesIn(N,membrane,xGreen,permGreen,nGreenMolecules);
  %Change Based On Charge Gradient
  [xRed chargeInR chargeOutR]=moveParticlesCharge(N,membrane,xRed,permRed,nRedMolecules,positiveIn, positiveOut, negativeIn, negativeOut);
  positiveIn=positiveIn + chargeInR;
  positiveInR=positiveInR + chargeInR;
  positiveOut=positiveOut + chargeOutR;
  positiveOutR=positiveOutR + chargeOutR;
  [xBlue chargeInB chargeOutB]=moveParticlesCharge(N,membrane,xBlue,permBlue,nBlueMolecules,positiveIn, positiveOut, negativeIn, negativeOut);
  positiveIn=positiveIn + chargeInB;
  positiveInB=positiveInB + chargeInB;
  positiveOut=positiveOut + chargeOutB;
  positiveOutB=positiveOutB + chargeOutB;
  [xGreen chargeInG chargeOutG]=moveParticlesCharge(N,membrane,xGreen,permGreen,nGreenMolecules,positiveIn, positiveOut, negativeIn, negativeOut);
  negativeIn=negativeIn + chargeInG;
  negativeOut=negativeOut + chargeOutG;
  % Create the figure window
  close all
  figure
  set(gcf,'position',[150 50 600 600])
  hold on
 
  
% boundary
M = 20;

 
  
% Set the axes
  axis([-20 20 -20 20])
  axis equal square manual
% Label in and out
  yText=M-1;
  xTextMin=-M+.5;
  xTextMax=M-7.5;
  text(xTextMin,yText,'Inside Cell')
  text(xTextMax,yText,'Outside Cell')
% Set membrane
  xMem=membrane;
  yMin=-20;
  yMax=20;
  blueConOut=0; %Conc of blue ions outside the cell
% Animates the diffusion.
  for col=2:xRedLength %By column
      plot([xMem xMem],[yMin yMax],'-c')
      for row=1:nRedMolecules %By Row
          plot(xRed(row,col-1),yRed(row,col-1),'.w','Markersize',20)
          plot(xRed(row,col),yRed(row,col),'.r','Markersize',20)
          plot(xBlue(row,col-1),yBlue(row,col-1),'.w','Markersize',20)
          plot(xBlue(row,col),yBlue(row,col),'.b','Markersize',20)
          plot(xGreen(row,col-1),yGreen(row,col-1),'.w','Markersize',20)
          plot(xGreen(row,col),yGreen(row,col),'.g','Markersize',20)
      end
      pause(.1)
  end
  
