function Blatt6_UE3
clear; clc;

% a ... Koeffizienten des Polynoms 
% a(1) + a(2)*x + a(3)*x^2 + ... a(n)*x^(n-1)
% ---------------------------------------
%a = [1 2 3 4 5 6 7 8 9 0.2 0.7 1.001 0 0 -0.431 0.002];
a = rand(1, 101);
%x = 7.1657;
x = -0.83;
n = length(a);

F = 20;  % Ziffernformat
sdp    = num2str(sdp(a,x), F);
horner = num2str(horner(a,x), F);
sdp_single    = num2str(sdp_single(a,x), F);
horner_single = num2str(horner_single(a,x), F);


disp(['Grad des Polynoms: ', num2str(n-1)]);
disp(['x = ', num2str(x, F)]);
disp('---------------------------------');
disp(['DOUBLE: sdp    : ', sdp]);
disp(['DOUBLE: horner : ', horner]);
disp(['SINGLE: sdp    : ', sdp_single]);
disp(['SINGLE: horner : ', horner_single]);


return


% ---------------------------------------
% -------- HORNER -----------------------
% ---------------------------------------
function y = horner(koeff, x)
    n = length(koeff);
    y = koeff(n);
    for k = (n-1):-1:1
        y = x*y + koeff(k);
    end
return

% ---------------------------------------
% -------- HORNER - SINGLE --------------
% ---------------------------------------
function y = horner_single(koeff, x)
    koeff = cast(koeff, 'single');
    x     = cast(x, 'single');
    n = length(koeff);
    y = koeff(n);
    for k = (n-1):-1:1
        y = cast(cast(x*y, 'single') + koeff(k), 'single');
    end
return

% ---------------------------------------
% -------- SUMME DER POTENZEN -----------
% ---------------------------------------
function y = sdp(koeff, x)
    n = length(koeff);
    y = koeff(1);
    for k = 2:n
        y = y + koeff(k)*x^(k-1);
    end
return

% ---------------------------------------
% -------- SUMME DER POTENZEN - SINGLE --
% ---------------------------------------
function y = sdp_single(koeff, x)
    koeff = cast(koeff, 'single');
    n = length(koeff);
    y = koeff(1);
    for k = 2:n
        y = cast(y + cast(koeff(k)*pow(x, k-1), 'single'), 'single');
    end
return

% ---------------------------------------
% -------- POWER FUER SINGLE ------------
% ---------------------------------------
function p = pow(x, k)
    p = single(1);
    for i = 1:k
        p = single(x*p);
    end
return

