%% Series expansion: exp(x)
% HH 12.4.2018
% This code should work fine if x > 0. However, things go wrong for x < 0.

function y = seriesexp(x)
oldsum = 0;
newsum = 1;
term = 1;
n = 0;

while newsum ~= oldsum
    n = n + 1;
    term = term * x / n;
    oldsum = newsum;
    newsum = newsum + term;
end
y = newsum;
end