function [A2,A1,B1] = expme(v,d,h)
%EXPME	Used by LINSIM to calculate matrix exponentials.
%       This function calculates matrix exponentials using eigenvalue
%	decomposition for the special case when the A matrix is
%	singular.  It is not used when A has repeated eigenvalues.

%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	Andrew Grace 11-12-90.
[n,dum] = size(v);
invv = inv(v);
d  = diag(d).';
de = exp(d*h);
A2 = real(v .* de(ones(n,1),:) *invv).';

[ind]  = find((abs(d)<1e-8));
on = ones(length(ind),1);
d(ind) = on;

d2 = (de - ones(1,n))./d;
d2(ind) = h*on;
A1 = real(v .*  d2(ones(n,1),:) * invv).';

d(ind) = on;
d3 = (d2 - h*ones(1,n))./d;
d3(ind) = h*h*0.5*on;
B1 = real(v .*  d3(ones(n,1),:) * invv).';
