Voraussetzungen

  • Programmier-Grundlagen

  • Matrizen-Grundlagen

Lerninhalte

  • Lösen der Nullstellen mithilfe der Eigenwerte der Begleitmatrix

  • Erstellen eigener Unit Tests

Nullstellen von Polynomen

Die sogenannte Begleitmatrix

\[\begin{split} \begin{bmatrix} -c_{1} & -c_{2} & \cdots & -c_{n-1} & -c_n \\ 1 & 0 & \cdots & & 0 \\ 0 & 1 & 0 & \cdots 0 \\ \vdots & & \ddots & & \vdots \\ 0 & \cdots & 0 & 1 & 0 \end{bmatrix} \end{split}\]

besitzt das charakteristische Polynom \(p(x)=x^n + c_{1}x^{n-1} + ... + c_{n-1}x + c_n\).

Sie wird häufig in der Steuerungstechnik genutzt, um die Eigenwerte und somit die dynamischen Eigenschaften des linearen Systems zu bestimmen.

Aufgabe 1 - Stimmt das?

Zeigen Sie für \(n=2\), dass das angegebene charakteristische Polynom zur Begleitmatrix passt.

Aufgabe 2 - Nullstellen berechnen

Entwerfen Sie eine Matlab Funktion, mit welcher Sie auf dieser Basis die Nullstellen eines beliebigen Polynoms unter Verwendung der Funktion eig berechnen können.

%%file my_roots.m
function z = my_roots(C)
% Find polynomial roots.
%
%    roots(C) computes the roots of the polynomial whose coefficients
%    are the elements of the vector C. If C has N+1 components,
%    the polynomial is C(1)*X^N + ... + C(N)*X + C(N+1).

% INSERT CODE HERE
  • Überprüfen Sie händisch anhand eines einfachen quadratischen Polynoms, ob Ihr Programm funktioniert, z.B. \(p(x)=3x^2-9x+6\).

  • Wie lauten die Nullstellen von \(p(x) = 2x^4 -20x^3 + 70x^2 -100x +48\)?

Aufgabe 3 - Automatisches Testen

Schreiben Sie nun Unit Tests für Ihre neue Funktion. Diese Unit Tests verwenden das Framework MOxUnit, das sowohl mit Matlab als auch Octave kompatibel ist. Wenn Sie die Tests auch lokal auf Ihrem Rechner ausführen wollen, benötigen Sie eine Installation von MOxUnit.

Erstellen Sie zunächst eine Datei mit folgendem Inhalt:

%%file test_my_roots.m
function test_suite=test_my_roots
% initialize unit tets
    try
        test_functions=localfunctions();
    catch
    end
    initTestSuite;

function test_quadratic_polynomial
    % tests, wether my_roots correctly determines the 
    % roots of a quadratic polynomial

    % INSERT CODE HERE. Use assertElementsAlmostEqual(a, b, tol)
    % to test, wether two values are equal up to a tolerance tol.
    % Never compare flaoting point numbers with ==
    % Remove the next line to get started
    assertEqual(1,0)
    
function test_compare_to_roots
    % tests, wether my_roots yields the same results as roots

    % INSERT CODE HERE. Use assertElementsAlmostEqual(a, b, tol)
    % to test, wether two values are equal up to a tolerance tol.
    % Never compare flaoting point numbers with ==
    % Remove the next line to get started
    assertEqual(1,0)

In dieser Datei sind die zwei Unit Tests test_quadratic_polynomial und test_compare_to_roots vorbereitet, die beide wegen der Zeile assertEqual(1,0) garantiert fehlschlagen werden. Ändern Sie die Unit Tests wie folgt ab:

test_quadratic_polynomial

Erstellen Sie ein zufälliges quadratisches Polynom, berechnen Sie die Nullstellen mit der abc- oder der pq-Formel und überprüfen Sie mit assertElementsAlmostEqual(a, b, tol), ob Ihre Funktion my_roots das selbe Ergebnis liefert.

test_compare_to_roots

Erstellen Sie eine zufällige natürliche Zahl \(n\) zwischen 1 und 10 und anschließend ein zufälliges Polynom \(n\)-ten Grades und überprüfen Sie mit assertElementsAlmostEqual(a, b, tol), ob Ihre Funktion my_roots das selbe Ergebnis liefert wie die eingebaute Funktion roots.

Besteht Ihre Funktion die Unit Tests?

moxunit_runtests test_my_roots.m;