function out = ex7ode(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.4*sin(200*pi*t);
  f23 = beta*(exp((u(2) - u(3))/Uf) - 1);
  f = zeros(5,1);
  f(1) = - (Ue - u(1))/R0;
  f(2) = - (Ub/R - u(2)*2/R - 0.01*f23);
  f(3) = - (f23 - u(3)/R);
  f(4) = - ((Ub - u(4))/R - 0.99*f23);
  f(5) = - (- u(5)/R);
  out = f;
else
  switch(flag)           
  case 'mass'
    c = 1e-6 * [1:3];
    M = sparse(5,5);
    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);
    out = M;
 otherwise
    error(['Unknown flag ''' flag '''.']);
  end
end     