function [x1,x2,x3,x4,x5,x6,x7,x8] = solve(f1,f2,f3,f4,f5,f6,f7,f8);
%SOLVE	Symbolic solution of algebraic equations.
%	One equation in one unknown:
%	SOLVE(S), where S is a symbolic equation, or a symbolic expression,
%	solves the given equation, or the equation S = 0, for its free
%	variable, as determined by SYMVAR.
%	SOLVE(S,'v') solves for the variable 'v'.
%
%	Several equations in several unknowns:
%	SOLVE(S1,S2,..,SN) solves N symbolic equations for the N unknowns
%	determined by SYMVAR.
%	SOLVE(S1,S2,..,SN,'v1,v2,..,vn') solves N symbolic equations for
%	the N unknowns specified by the last input argument.
%	[X1,X2,..,XN] = SOLVE(S1,S2,..,SN), and
%	[X1,X2,..,XN] = SOLVE(S1,S2,..,SN,'v1,v2,..,vn') return N symbolic
%	vectors containing expressions for the individual variables in
%	the solutions.
%
%	In all cases, numeric solutions are returned if symbolic solutions
%	can not be found.
%
%	Examples:
%	    solve('log(x) = x/pi')
%	    x = solve('a*x^2 + b*x + c')
%	    b = solve('a*x^2 + b*x + c', 'b')
%	    [x,y] = solve('x^2 + 2*x*y + y^2 = 4', 'x^3 + 4*y^3 = 1')
%	    [u,v] = solve('a*u^2 + v^2', 'u - v = 1', 'u,v')
%	    [a,u,v] = solve('a*u^2 + v^2', 'u - v = 1', 'a^2 - 5*a + 6')
%
%	See also DSOLVE, NUMERIC.

%	Copyright (c) 1993-95 by The MathWorks, Inc.
%	$Revision: 1.12 $  $Date: 1995/03/01 21:38:57 $

% Determine n = number of unknowns by examining last input argument.

n = nargin;
eval(['v = f' int2str(n) ';'])
w = length(v);
if v(1) == '[' & v(w) == ']'
   v = v(2:w-1);
end
if n > 1 & (any(v == ',') | all(isletter(v) | ('0' <= v & v <= '9')))
   n = n-1;
end

% Form a Maple set from the equations.

f = ['{' f1];
for k = 2:n
   eval(['f = [f '','' f' int2str(k) '];'])
end
f = [f '}'];

% Determine the free variables, if not specified. 

if nargin == n
   if n == 1
      v = symvar(f);
   else
      v = symvar(f,[n,n]);
      if ~findstr(v,f), v = []; end
   end
   if isempty(v) | isnan(v)
      error('Symbolic variable(s) not uniquely determined.')
   end
end
v = ['{' v '}'];

% Solve the equations.
r = maple('solve',f,v);

% Try numeric solution if no analytical solution found.
if isempty(r)
   disp(' ')
   disp('Warning: No analytic solution found.')
   [r,status] = maple('fsolve',f,v);
   if (status==2) & strcmp(r, ...
   'Error, (in fsolve) should use exactly all the indeterminates')
      r = '';
      return;
      disp(' ')
   else
      disp('         Returning numeric solution.')
   end
end

% Determine m = the number of solutions.

m = sum(r == '{');

% If right number of output args, return n vectors of length m.

if (nargout == n) | (n == 1 & nargout <= 1)
   e = find(r == '=');
   c = sort([findcomma(r) find(r == '}')]);
   v(1) = [];
   v(length(v)) = ',';
   for j = 1:n
      k = min(find(v==','));
      vj = v(1:k-1);
      v(1:k) = [];
      pj = findstr(vj,r);
      s = [];
      for k = 1:m
         p = min(e(e>pj(k)))+2;
         q = min(c(c>p))-1;
         s = str2mat(s,['[' r(p:q) ']']);
      end
      s = comstack(s(2:m+1,:));
      eval(['x' int2str(j) ' = s;'])
   end

% Otherwise, return m solution sets.

else
   p = findstr(r,'{')+1;
   q = findstr(r,'}')-1;
   x1 = [];
   for k = 1:m
      x1 = str2mat(x1,r(p(k):q(k)));
   end
   x1(1,:) = [];
end
