notes

uni notes
git clone git://popovic.xyz/notes.git
Log | Files | Refs

commit dcfa6316d2dd13b2f4f9c2fb3635f2bd36fcd092
parent a22856d713f9558189bb47e6b22f3375fabb908f
Author: miksa234 <milutin@popovic.xyz>
Date:   Mon, 25 Apr 2022 12:29:42 +0200

prb 6 done

Diffstat:
Anum_ana/prog/.ipynb_checkpoints/prb6_p-checkpoint.ipynb | 6++++++
Anum_ana/prog/prb6_p.ipynb | 196+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 202 insertions(+), 0 deletions(-)

diff --git a/num_ana/prog/.ipynb_checkpoints/prb6_p-checkpoint.ipynb b/num_ana/prog/.ipynb_checkpoints/prb6_p-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/num_ana/prog/prb6_p.ipynb b/num_ana/prog/prb6_p.ipynb @@ -0,0 +1,196 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "9518bc9b", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from matplotlib import pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "1f94b10c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6488 6480]\t1.9987623950544455\n" + ] + } + ], + "source": [ + "def power_iter(A: np.ndarray, x_0 :np.array, N :int=10):\n", + " x_k = x_0\n", + " for i in range(N):\n", + " x_k1 = A@x_k\n", + " r_k = (x_k1 @ x_k)/(x_k @ x_k)\n", + " x_k = x_k1\n", + " return x_k, r_k\n", + "A = np.array([[1, 1], [2, 0]])\n", + "x_0 = np.random.randint(10, size=len(A))\n", + "x_k, r_k = power_iter(A, x_0)\n", + "print(x_k, r_k, sep='\\t')" + ] + }, + { + "cell_type": "code", + "execution_count": 187, + "id": "742e8c5e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1.8852459 0.26229508]\t[ 1.26229508 -0.8852459 ]\n" + ] + } + ], + "source": [ + "def QR_alg(A:np.ndarray, N:int=10):\n", + " A_k = A\n", + " for i in range(N):\n", + " Q, R = np.linalg.qr(A_k)\n", + " A_k = R@Q\n", + " return A_k, Q, R\n", + " \n", + "A_k, Q, R = QR_alg(A, N=3)\n", + "print(A_k[:,0], A_k[:,1], sep='\\t')" + ] + }, + { + "cell_type": "code", + "execution_count": 188, + "id": "04812e53", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 37375., 25025.],\n", + " [ 5200., -17550.]])" + ] + }, + "execution_count": 188, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "A_k*np.sqrt(793)*5*5*np.sqrt(793)" + ] + }, + { + "cell_type": "code", + "execution_count": 154, + "id": "233539a6", + "metadata": {}, + "outputs": [], + "source": [ + "a1 = 1/5*np.array([7, 4])\n", + "e1 = a1/np.linalg.norm(a1)\n", + "a2 = 1/5 * np.array([9, -2])" + ] + }, + { + "cell_type": "code", + "execution_count": 177, + "id": "3e998ed0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[135., -25.],\n", + " [ 40., -70.]])" + ] + }, + "execution_count": 177, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "A_k*np.sqrt(65)*np.sqrt(65)" + ] + }, + { + "cell_type": "code", + "execution_count": 179, + "id": "26fc5985", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "140.80127840328723" + ] + }, + "execution_count": 179, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "135**2 + 40**2" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "id": "4cd82a72", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 169, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "9*7-4*2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c09f8ff", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}