function ex02()
fprintf("x       N    err exp(x)   err exp(-x)   err 1/exp(x)\n");

x = 10;
N = 30;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);

N = 40;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);

N = 50;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);


x = 20;
N = 30;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);

N = 50;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);

N = 70;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);

N = 100;
[err1 err2 err3] = getexperrors(x, N);
fprintf("%3.1f   %3d   %2.4e   %2.4e    %2.4e\n", x, N, err1, err2, err3);

%-------------------------------------------------
function [err1 err2 err3] = getexperrors(x, N)
% berechnet die relativen Fehler für exp(x) und exp(-x),
% wenn die Taylorreihe bis N verwendet wird, 
% sowie von exp(-x) = 1/exp(x) mit dem Näherungswert für exp(x).
% Verwendet Matlabs exp-Funktion als Vergleichswert

k = 0:N;

% exp(x)
terms = (x.^k)./factorial(k);
expTaylor1 = sum(terms);
expExact1 = exp(x);
err1 = abs(expTaylor1 - expExact1)/expExact1;

% exp(-x)
terms = ((-x).^k)./factorial(k);
expTaylor2 = sum(terms);
expExact2 = exp(-x);
err2 = abs(expTaylor2 - expExact2)/expExact2;

% 1/exp(x)
expTaylor3 = 1/expTaylor1;
expExact3 = exp(-x);
err3 = abs(expTaylor3 - expExact3)/expExact3;
