This commit is contained in:
s487174 2023-06-16 05:56:26 +02:00
parent 767665bd3b
commit 4b838d794f

View File

@ -1,6 +1,7 @@
{ {
"cells": [ "cells": [
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@ -8,45 +9,7 @@
] ]
}, },
{ {
"cell_type": "code", "attachments": {},
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'matrix' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m A\u001b[38;5;241m=\u001b[39m\u001b[43mmatrix\u001b[49m(QQ,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m3\u001b[39m,[\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m6\u001b[39m, \u001b[38;5;241m8\u001b[39m, \u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m12\u001b[39m, \u001b[38;5;241m14\u001b[39m, \u001b[38;5;241m16\u001b[39m, \u001b[38;5;241m18\u001b[39m, \u001b[38;5;241m20\u001b[39m, \u001b[38;5;241m22\u001b[39m, \u001b[38;5;241m24\u001b[39m, \u001b[38;5;241m26\u001b[39m, \u001b[38;5;241m28\u001b[39m, \u001b[38;5;241m31\u001b[39m])\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA =\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(A, \u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
"\u001b[1;31mNameError\u001b[0m: name 'matrix' is not defined"
]
}
],
"source": [
"A=matrix(QQ,5,3,[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 31])\n",
"print('A =')\n",
"print(A, '\\n')\n",
"\n",
"print('b =')\n",
"b=vector(QQ,[-1,0,1,0,1])\n",
"print(b, '\\n')\n",
"\n",
"print('A^T * A =')\n",
"print(A.transpose()*A, '\\n')\n",
"print('Macierz A^T*A jest kwadratowa, więc rozwiązanie istnieje\\n')\n",
"\n",
"u=(A.transpose()*A)^(-1)*A.transpose()*b\n",
"print('u = (A^T * A)^-1 * A^T * b =')\n",
"print(u, '\\n')\n",
"\n",
"\n",
"print('b - A * u = ')\n",
"print(b - A * u, '\\n')"
]
},
{
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@ -66,13 +29,97 @@
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "code",
"execution_count": 126,
"metadata": {}, "metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A =\n",
"[[ 2 4 6]\n",
" [ 8 10 12]\n",
" [14 16 18]\n",
" [20 22 24]\n",
" [26 28 31]] \n",
"\n",
"b =\n",
"[-1 0 1 0 1] \n",
"\n",
"A^T * A =\n",
"Matrix([[1340, 1480, 1646], [1480, 1640, 1828], [1646, 1828, 2041]]) \n",
"\n",
"Macierz A^T*A jest kwadratowa, więc rozwiązanie istnieje\n",
"\n",
"(A^T * A)^(-1)*A.transpose() =\n",
"[[0.050000 -0.233333 -0.516667 -0.800000 1.000000]\n",
" [-0.600000 0.216667 1.033333 1.850000 -2.000000]\n",
" [0.500000 0.000000 -0.500000 -1.000000 1.000000]] \n",
"\n",
"Rozwiązanie:\n",
"\n",
"x = (A^T * A)^(-1) * A^T * b =\n",
"[0.433333 -0.366667 -0.000000] \n",
"\n",
"\n"
]
}
],
"source": [ "source": [
"Zadanie 4.7\n" "import numpy as np\n",
"from sympy import symbols, Matrix, vector\n",
"from numpy.linalg import eig\n",
"\n",
"m = np.array([[2, 4, 6],\n",
" [8, 10, 12],\n",
" [14, 16, 18],\n",
" [20, 22, 24],\n",
" [26, 28, 31]])\n",
"\n",
"A=Matrix(m)\n",
"\n",
"np.set_printoptions(formatter={'float_kind':'{:f}'.format})\n",
"\n",
"print('A =')\n",
"print(m, '\\n')\n",
"\n",
"print('b =')\n",
"b=np.array([-1,0,1,0,1])\n",
"print(b, '\\n')\n",
"\n",
"print('A^T * A =')\n",
"print(A.transpose()*A, '\\n')\n",
"print('Macierz A^T*A jest kwadratowa, więc rozwiązanie istnieje\\n')\n",
"\n",
"#print('(A^T * A)^(-1) =')\n",
"#print(np.linalg.inv(m.transpose() @ m), '\\n')\n",
"\n",
"print('(A^T * A)^(-1)*A.transpose() =')\n",
"print(np.linalg.inv(m.transpose() @ m) @ m.transpose(), '\\n')\n",
"\n",
"print('Rozwiązanie:\\n')\n",
"#u=(A.transpose()*A)^(-1)*A.transpose()*b\n",
"x = np.linalg.inv(m.transpose() @ m) @ m.transpose() @ b\n",
"print('x = (A^T * A)^(-1) * A^T * b =')\n",
"print(x, '\\n\\n')\n",
"\n",
"# SPRAWDZENIE\n",
"x, residuals, _, _ = np.linalg.lstsq(m, b, rcond=None)\n",
"#print(\"Przybliżone rozwiązanie:\")\n",
"#print(x)"
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Zadanie 4.7"
]
},
{
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@ -83,29 +130,84 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 127,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"zbior punktów = [(1, 1), (2, 3), (4, 5)]\n" "Przybliżone parametry:\n",
] "a = 1.641485981693081\n",
}, "b = 0.06298603382678646\n"
{
"ename": "NameError",
"evalue": "name 'matrix' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m zbior\u001b[38;5;241m=\u001b[39m[(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m1\u001b[39m),(\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m),(\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m)]\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mzbior punktów = \u001b[39m\u001b[38;5;124m'\u001b[39m, zbior)\n\u001b[1;32m----> 3\u001b[0m m\u001b[38;5;241m=\u001b[39m\u001b[43mmatrix\u001b[49m(\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m2\u001b[39m,[\u001b[38;5;241m1\u001b[39m,exp(\u001b[38;5;241m1.0\u001b[39m),\u001b[38;5;241m1\u001b[39m,exp(\u001b[38;5;241m2.0\u001b[39m),\u001b[38;5;241m1\u001b[39m,exp(\u001b[38;5;241m4.0\u001b[39m)])\n\u001b[0;32m 5\u001b[0m a,b,t\u001b[38;5;241m=\u001b[39mvar(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma,b,t\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 7\u001b[0m m\u001b[38;5;241m*\u001b[39mvector([a,b])\u001b[38;5;241m-\u001b[39mvector([\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m5\u001b[39m])\n",
"\u001b[1;31mNameError\u001b[0m: name 'matrix' is not defined"
] ]
} }
], ],
"source": [ "source": [
"import numpy as np\n",
"from scipy.optimize import curve_fit\n",
"\n",
"# Definicja funkcji, której chcemy dokonać przybliżenia\n",
"def func(t, a, b):\n",
" return a + b * np.exp(t)\n",
"\n",
"# Dane punktów\n",
"t_data = np.array([1, 2, 4])\n",
"y_data = np.array([1, 3, 5])\n",
"\n",
"# Przybliżanie funkcji do danych punktów\n",
"params, _ = curve_fit(func, t_data, y_data)\n",
"\n",
"# Rozwiązania parametrów a i b\n",
"a = params[0]\n",
"b = params[1]\n",
"\n",
"# Wyświetlenie wyników\n",
"print(\"Przybliżone parametry:\")\n",
"print(\"a =\", a)\n",
"print(\"b =\", b)\n"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'module' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[128], line 9\u001b[0m\n\u001b[0;32m 5\u001b[0m m1\u001b[39m=\u001b[39mnp\u001b[39m.\u001b[39marray([[\u001b[39m1\u001b[39m,np\u001b[39m.\u001b[39mexp(\u001b[39m1.0\u001b[39m)],[\u001b[39m1\u001b[39m,np\u001b[39m.\u001b[39mexp(\u001b[39m2.0\u001b[39m)],[\u001b[39m1\u001b[39m,np\u001b[39m.\u001b[39mexp(\u001b[39m4.0\u001b[39m)]])\n\u001b[0;32m 7\u001b[0m \u001b[39m#a,b,t=var('a,b,t')\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m m1\u001b[39m*\u001b[39mvector([a,b])\u001b[39m-\u001b[39mvector([\u001b[39m1\u001b[39m,\u001b[39m3\u001b[39m,\u001b[39m5\u001b[39m])\n\u001b[0;32m 11\u001b[0m M1\u001b[39m=\u001b[39mm1\u001b[39m.\u001b[39mtranspose()\u001b[39m*\u001b[39mm1\n\u001b[0;32m 12\u001b[0m M1\u001b[39m.\u001b[39mdet()\n",
"\u001b[1;31mTypeError\u001b[0m: 'module' object is not callable"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"zb1=np.array([[1,1],[2,3],[4,5]])\n",
"\n",
"m1=np.array([[1,np.exp(1.0)],[1,np.exp(2.0)],[1,np.exp(4.0)]])\n",
"\n",
"#a,b,t=var('a,b,t')\n",
"\n",
"m1*vector([a,b])-vector([1,3,5])\n",
"\n",
"M1=m1.transpose()*m1\n",
"M1.det()\n",
"\n",
"M1^(-1)*m1.transpose()*vector([1,3,5])\n",
"\n",
"##########################################################\n",
"\n",
"def func(t, a, b):\n",
" return a + b * np.exp(t)\n",
"\n",
"x = np.linalg.inv(m.transpose() @ m) @ m.transpose() @ b\n",
"\n",
"zbior=[(1,1),(2,3),(4,5)]\n", "zbior=[(1,1),(2,3),(4,5)]\n",
"print('zbior punktów = ', zbior)\n", "print('zbior punktów = ', zbior)\n",
"m=matrix(3,2,[1,exp(1.0),1,exp(2.0),1,exp(4.0)])\n", "m=matrix(3,2,[1,exp(1.0),1,exp(2.0),1,exp(4.0)])\n",
@ -122,6 +224,7 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@ -129,6 +232,7 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@ -145,48 +249,207 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "NameError", "name": "stdout",
"evalue": "name 'matrix' is not defined", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "Macierz A=\n",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "[[1 1 0]\n",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", " [1 2 2]\n",
"Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m m\u001b[38;5;241m=\u001b[39m\u001b[43mmatrix\u001b[49m(\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m3\u001b[39m,[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m])\n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m#wartosci wlasne\u001b[39;00m\n\u001b[0;32m 4\u001b[0m eigenvalues \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mm\u001b[38;5;241m.\u001b[39meigvals(matrix)\n", " [0 2 3]]\n",
"\u001b[1;31mNameError\u001b[0m: name 'matrix' is not defined" "Baza ortonormalnych wektorów własnych:\n",
"[[-0.593233 -0.786436 0.172027]\n",
" [0.679313 -0.374362 0.631179]\n",
" [-0.431981 0.491296 0.756320]]\n"
] ]
} }
], ],
"source": [ "source": [
"m=matrix(3,3,[1,1,0,1,2,2,0,2,3])\n", "import numpy as np\n",
"from sympy import symbols, Matrix\n",
"from numpy.linalg import eig\n",
"\n", "\n",
"#wartosci wlasne\n", "#Definicja macierzy A\n",
"eigenvalues = np.m.eigvals(matrix)\n", "A = np.array([[1,1,0],[1,2,2],[0,2,3]])\n",
"#A = Matrix([[1,1,0],[1,2,2],[0,2,3]])\n",
"print(\"Macierz A=\")\n",
"print(A)\n",
"\n", "\n",
"eigen=m.right_eigenvectors()\n", "def qr_eigval(matrix, epsilon=1e-10, max_iterations=1000):\n",
"e1=eigen[0][1][0]\n",
"e2=eigen[1][1][0]\n",
"print(e1.dot_product(e2))\n",
"e3=eigen[2][1][0]\n",
"print(e3.dot_product(e1))\n",
"print(e2.dot_product(e3))\n",
"\n", "\n",
"#znormalizuj wektor wlasny\n", " eigenv = np.diag(matrix)\n",
"\n", "\n",
"#czy wektory wlasne sa ortogonalne?\n", " for _ in range(max_iterations):\n",
"\n", "\n",
"#postac ortonormalna i normalizacja" " q, r = np.linalg.qr(matrix)\n",
" matrix = np.dot(r, q)\n",
" new_eigenv = np.diag(matrix)\n",
"\n",
" if np.allclose(eigenv, new_eigenv, atol=epsilon):\n",
" break\n",
" eigenv = new_eigenv\n",
"\n",
" return eigenv\n",
"\n",
"#Obliczenie wektorów własnych i wartości własnych\n",
"#eigenvalues = qr_eigval(matrix)\n",
"eigenvalues, eigenvectors = np.linalg.eig(A)\n",
"\n",
"#Dekompozycja\n",
"Q, R = np.linalg.qr(eigenvectors)\n",
"\n",
"#Normalizacja wektora własnego\n",
"orthonormal_basis = Q\n",
"\n",
"print(\"Baza ortonormalnych wektorów własnych:\")\n",
"print(orthonormal_basis)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Zadanie 3.9"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Zadanie 9\n",
"\n",
"Oblicz metodą ''power iteration'' wartości własne macierzy\n",
"\n",
"$$\\left(\\begin{array}{rrr}\n",
"1 & 2 & 3 \\\\\n",
"4 & 5 & 6 \\\\\n",
"7 & 8 & 9\n",
"\\end{array}\\right)$$"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
"source": [] {
"name": "stdout",
"output_type": "stream",
"text": [
"Dominująca wartość własna (power iteration):\n",
"16.116843969807043\n",
"Dominujący wektor własny:\n",
"[[0.107761 0.132408 0.157054]\n",
" [0.244037 0.299852 0.355666]\n",
" [0.380312 0.467295 0.554278]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"A = np.array([[1,2,3],\n",
" [4,5,6],\n",
" [7,8,9]])\n",
"\n",
"S = np.array([[1,1,1]])\n",
"\n",
"def power_iteration(m, n, s):\n",
" #punkt startowy\n",
" st = s\n",
" eigv = 0\n",
" for i in range(n):\n",
" st = np.dot(A, st)\n",
" eigv = np.max(st)\n",
" st = st/eigv\n",
" return eigv\n",
"\n",
"def power_iteration_vec(m, n, s):\n",
" tolerance = 1e-6\n",
" #punkt startowy\n",
" x = s\n",
" for i in range(n): \n",
" y = np.dot(m, x)\n",
" #Normalizacja wektora\n",
" x_new = y / np.linalg.norm(y)\n",
" #Sprawdzenie warunku zbieżności\n",
" if np.linalg.norm(x - x_new) < tolerance:\n",
" break\n",
" #Aktualizacja\n",
" x = x_new\n",
" return x\n",
"\n",
"y1 = power_iteration(A, 1000, A)\n",
"print(\"Dominująca wartość własna (power iteration):\")\n",
"print(y1)\n",
"\n",
"y2 = power_iteration_vec(A, 1000, A)\n",
"print(\"Dominujący wektor własny:\")\n",
"print(y2)\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": {