function u = formalseries(x, n, x0)
% Creates a formal power series.
%
% FORMALSERIES(x) returns the terms up to degree 6 of
% a formal power series in the variable x, centered
% at x = 0.
%
% FORMALSERIES(x, n) returns the terms up to degree n
% of the formal power series in x at 0.
%
% FORMALSERIES(x, n, x0) returns the terms up to degree
% n of the formal power series in x centered at x = x0.

if nargin < 3
  x0 = 0;
end
if nargin < 2
  n = 6;
end
x = sym(x);

% The next loop builds a formal polynomial of degree n
% in the variable x centered at the point x0.
u = sym(0);
for k = 0:n
  v = sym(['a' num2str(k)]);
  u = u + v*(x - x0)^k;
end
