function out = ex8ode(t,u,flag)

% Copyright 1999, The MathWorks, Inc.

if nargin < 3 | isempty(flag)
  Ub = 6;
  R0 = 1000;
  R = 9000;
  alpha = 0.99;
  beta = 1e-6;
  Uf = 0.026;

  Ue = 0.1*sin(200*pi*t);
  f23 = beta*(exp((u(2) - u(3))/Uf) - 1);
  f56 = beta*(exp((u(5) - u(6))/Uf) - 1);
  f = zeros(8,1);
  f(1) = - ((Ue - u(1))/R0);
  f(2) = - ((Ub - u(2))/R - u(2)/R + (alpha - 1)*f23);
  f(3) = - (f23 - u(3)/R);
  f(4) = - ((Ub - u(4))/R - alpha*f23);
  f(5) = - ((Ub - u(5))/R - u(5)/R + (alpha - 1)*f56);
  f(6) = - (f56 - u(6)/R);
  f(7) = - ((Ub - u(7))/R - alpha*f56);
  f(8) = - (- u(8)/R);
  out = f;
else
  switch(flag)           
  case 'mass'
    c = 1e-6 * [1:5];
    M = sparse(8,8);
    M(1,1) = -c(1);
    M(1,2) =  c(1);
    M(2,1) =  c(1);
    M(2,2) = -c(1);
    M(3,3) = -c(2);
    M(4,4) = -c(3);
    M(4,5) =  c(3);
    M(5,4) =  c(3);
    M(5,5) = -c(3);
    M(6,6) = -c(4);
    M(7,7) = -c(5);
    M(7,8) =  c(5);
    M(8,7) =  c(5);
    M(8,8) = -c(5);
    out = M;
 otherwise
    error(['Unknown flag ''' flag '''.']);
  end
end     