This commit is contained in:
s487174 2023-06-16 06:07:56 +02:00
parent 4b838d794f
commit f7e35d41c5

View File

@ -334,19 +334,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 146,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Dominująca wartość własna (power iteration):\n",
"16.116843969807043\n",
"Dominujący wektor własny:\n", "Dominujący wektor własny:\n",
"[[0.107761 0.132408 0.157054]\n", "[[0.231970]\n",
" [0.244037 0.299852 0.355666]\n", " [0.525322]\n",
" [0.380312 0.467295 0.554278]]\n" " [0.818674]]\n",
"Dominująca wartość własna (power iteration):\n",
"16.116843969807043\n"
] ]
} }
], ],
@ -357,10 +357,10 @@
" [4,5,6],\n", " [4,5,6],\n",
" [7,8,9]])\n", " [7,8,9]])\n",
"\n", "\n",
"S = np.array([[1,1,1]])\n", "S = np.array([[1], [1], [1]])\n",
"\n", "\n",
"def power_iteration(m, n, s):\n", "def power_iteration(m, n, s):\n",
" #punkt startowy\n", " #Punkt startowy\n",
" st = s\n", " st = s\n",
" eigv = 0\n", " eigv = 0\n",
" for i in range(n):\n", " for i in range(n):\n",
@ -371,7 +371,7 @@
"\n", "\n",
"def power_iteration_vec(m, n, s):\n", "def power_iteration_vec(m, n, s):\n",
" tolerance = 1e-6\n", " tolerance = 1e-6\n",
" #punkt startowy\n", " #Punkt startowy\n",
" x = s\n", " x = s\n",
" for i in range(n): \n", " for i in range(n): \n",
" y = np.dot(m, x)\n", " y = np.dot(m, x)\n",
@ -384,72 +384,15 @@
" x = x_new\n", " x = x_new\n",
" return x\n", " return x\n",
"\n", "\n",
"y1 = power_iteration(A, 1000, A)\n", "y1 = power_iteration_vec(A, 1000, S)\n",
"print(\"Dominująca wartość własna (power iteration):\")\n", "print(\"Dominujący wektor własny:\")\n",
"print(y1)\n", "print(y1)\n",
"\n", "\n",
"y2 = power_iteration_vec(A, 1000, A)\n", "y2 = power_iteration(A, 1000, S)\n",
"print(\"Dominujący wektor własny:\")\n", "print(\"Dominująca wartość własna (power iteration):\")\n",
"print(y2)\n", "print(y2)\n",
"\n",
"\n" "\n"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dominująca wartość własna:\n",
"16.116840810027913\n",
"Dominujący wektor własny:\n",
"[0.231970 0.525322 0.818674]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# Definicja macierzy A\n",
"A = np.array([[1,2,3],\n",
" [4,5,6],\n",
" [7,8,9]])\n",
" \n",
"# Definicja wektora startowego\n",
"x = np.array([1, 1, 1])\n",
"\n",
"# Parametry iteracji\n",
"max_iterations = 1000\n",
"tolerance = 1e-6\n",
"\n",
"# Iteracyjne obliczanie dominującej wartości własnej i wektora własnego\n",
"for _ in range(max_iterations):\n",
" # Mnożenie macierzy przez wektor\n",
" y = np.dot(A, x)\n",
"\n",
" # Normalizacja wektora\n",
" x_new = y / np.linalg.norm(y)\n",
"\n",
" # Sprawdzenie warunku zbieżności\n",
" if np.linalg.norm(x - x_new) < tolerance:\n",
" break\n",
"\n",
" # Aktualizacja wektora\n",
" x = x_new\n",
"\n",
"# Obliczenie dominującej wartości własnej\n",
"eigenvalue = np.dot(np.dot(A, x), x) / np.dot(x, x)\n",
"\n",
"# Wyświetlenie wyników\n",
"print(\"Dominująca wartość własna:\")\n",
"print(eigenvalue)\n",
"print(\"Dominujący wektor własny:\")\n",
"print(x)\n"
]
} }
], ],
"metadata": { "metadata": {