{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sensitivity of polynomial roots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the polynomial\n", "$$\n", "x^3 - 21 x^2 + 120 x - 100 = 0\n", "$$\n", "The roots are 1, 10, 10." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from sympy import N, roots\n", "from sympy import Integer as Int" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Roots = {1: 1, 10: 2}\n", "1\n", "10\n" ] } ], "source": [ "r = roots([1,-21,120,-100])\n", "print('Roots = ',r)\n", "for x in r:\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output shows that the root 1 has multiplicity one and root 10 has multiplicity of two.\n", "\n", "**Note**: The type of return value `r` is `dict`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perturb the coefficient of $x^3$ \n", "$$\n", "\\frac{99}{100}x^3 - 21 x^2 + 120 x - 100 = 0\n", "$$" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0001235059284 - 0.e-22*I\n", "11.1706986297466 - 0.e-22*I\n", "9.0412990764462 + 0.e-22*I\n" ] } ], "source": [ "r = roots([Int(99)/100,-21,120,-100])\n", "for x in r:\n", " print(N(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The double roots are very sensitive. Now perturb the coefficient in the other direction\n", "$$\n", "\\frac{101}{100}x^3 - 21 x^2 + 120 x - 100 = 0\n", "$$" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.999876592295142\n", "9.89610130781283 - 1.04369535224477*I\n", "9.89610130781283 + 1.04369535224477*I\n" ] } ], "source": [ "r = roots([Int(101)/100,-21,120,-100])\n", "for x in r:\n", " print(N(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The double roots become complex, again showing they are very sensitive to the coefficient of $x^3$." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 1 }