TU Wien:Computernumerik VU (Schranz-Kirlinger)/Übungen SS16/Beispiel 31

Aus VoWi
Zur Navigation springen Zur Suche springen

Lösung[Bearbeiten | Quelltext bearbeiten]

b)[Bearbeiten | Quelltext bearbeiten]

Matlab Code

function Bsp1

%l = log(1 + x);

figure;
[ERROR, DEGREE] = approx(-1/2, 21);
semilogy(DEGREE, ERROR);
hold on
[ERROR, DEGREE] = approx(-1/4, 21);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(-1/8, 21);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(-1/12, 21);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(-1/100, 21);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(0.6, 21);
semilogy(DEGREE, ERROR);
legend('-1/2', '-1/4', '-1/8', '-1/12', '-1/100', '0.6');
hold off

figure;
[ERROR, DEGREE] = approx(-1/2, 50);
semilogy(DEGREE, ERROR);
hold on
[ERROR, DEGREE] = approx(-1/4, 50);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(-1/8, 50);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(-1/12, 50);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(-1/100, 50);
semilogy(DEGREE, ERROR);
[ERROR, DEGREE] = approx(1, 50);
semilogy(DEGREE, ERROR);
legend('-1/2', '-1/4', '-1/8', '-1/12', '-1/100', '1');
hold off

end

function [ERROR, DEGREE] = approx(x, n)

ERROR = [];
DEGREE = [];
sumV = 0;
for i = 1:n
    sng = (-1)^(i - 1);
    value = sng * ((1/i) * x^i);
    sumV = sumV + value;
    ERROR = cat(1, ERROR, abs(sumV - log(1 + x)));
    DEGREE = cat(1, DEGREE, i);
end

end