├── #Test.py ├── .vscode ├── c_cpp_properties.json ├── launch.json └── settings.json ├── 14 - SVD.py ├── Algorithm ├── .ipynb_checkpoints │ ├── Bài 1-checkpoint.ipynb │ ├── Bài 2-checkpoint.ipynb │ ├── Bài 3-checkpoint.ipynb │ ├── Bài 5-checkpoint.ipynb │ ├── Bài 6-checkpoint.ipynb │ └── note-checkpoint.ipynb ├── Bài 1.ipynb ├── Bài 2.ipynb ├── Bài 3.ipynb ├── Bài 5.ipynb ├── Bài 6.ipynb ├── note.ipynb └── output_tn.txt ├── Bipartiteness_Graph.py ├── Bisection.png ├── Bisection_Dia.png ├── Bisection_Method.py ├── Bowstring_method.py ├── Gauss.py ├── Insert a node in BST.pptx ├── Iteration_method.py ├── KMean ├── pro.py └── test.py ├── README.md ├── input_xuongthang.txt └── result_Iteration_method.png /#Test.py: -------------------------------------------------------------------------------- 1 | print("Hello World!") -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "windows-gcc-x64", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gcc.exe", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "windows-gcc-x64", 12 | "compilerArgs": [ 13 | "" 14 | ] 15 | } 16 | ], 17 | "version": 4 18 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "C/C++ Runner: Debug Session", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "externalConsole": true, 11 | "cwd": "d:/algo", 12 | "program": "d:/algo/build/Debug/outDebug", 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp_Runner.cCompilerPath": "gcc", 3 | "C_Cpp_Runner.cppCompilerPath": "g++", 4 | "C_Cpp_Runner.debuggerPath": "gdb", 5 | "C_Cpp_Runner.cStandard": "", 6 | "C_Cpp_Runner.cppStandard": "", 7 | "C_Cpp_Runner.msvcBatchPath": "", 8 | "C_Cpp_Runner.useMsvc": false, 9 | "C_Cpp_Runner.warnings": [ 10 | "-Wall", 11 | "-Wextra", 12 | "-Wpedantic", 13 | "-Wshadow", 14 | "-Wformat=2", 15 | "-Wconversion", 16 | "-Wnull-dereference", 17 | "-Wsign-conversion" 18 | ], 19 | "C_Cpp_Runner.enableWarnings": true, 20 | "C_Cpp_Runner.warningsAsError": false, 21 | "C_Cpp_Runner.compilerArgs": [], 22 | "C_Cpp_Runner.linkerArgs": [], 23 | "C_Cpp_Runner.includePaths": [], 24 | "C_Cpp_Runner.includeSearch": [ 25 | "*", 26 | "**/*" 27 | ], 28 | "C_Cpp_Runner.excludeSearch": [ 29 | "**/build", 30 | "**/build/**", 31 | "**/.*", 32 | "**/.*/**", 33 | "**/.vscode", 34 | "**/.vscode/**" 35 | ], 36 | "C_Cpp_Runner.useAddressSanitizer": false, 37 | "git.ignoreLimitWarning": true 38 | } -------------------------------------------------------------------------------- /14 - SVD.py: -------------------------------------------------------------------------------- 1 | # In[5]: 2 | 3 | 4 | import numpy as np 5 | from scipy.linalg import null_space 6 | from numpy import linalg as LA 7 | from scipy.linalg import svd 8 | import math as mt 9 | 10 | data=np.genfromtxt('input.txt', delimiter=' ') 11 | A=np.array(data) 12 | 13 | m=len(A) 14 | n=len(A[0]) 15 | 16 | def SVD(A): 17 | # gán ma trân sigma bằng ma trận 0 cở m.n 18 | sigma = np.zeros((m,n)) 19 | v = np.eye(n) 20 | # r = rank ma trận -1 21 | r= np.linalg.matrix_rank(A) -1 22 | # nếu A ko full rank thì sẽ tìm ker(A) và ker(A.T) 23 | if r != n-1: 24 | ns = null_space(A) 25 | ns =ns.T 26 | nd = null_space(A.T) 27 | nd = nd.T 28 | # m >=n ta tính U trước V sau 29 | if m>=n: 30 | # ta tìm trị riêng w của A.AT và tìm vecto riêng tương ứng là u 31 | w,u = LA.eig(A@A.T) 32 | # sắp xếp trị riêng và vetor riêng 33 | for i in range(n-1): 34 | for j in range(i+1, n): 35 | if(w[j-1] < w[j]): 36 | w[j], w[j-1] = w[j-1], w[j] 37 | u[:,[j-1,j]] = u[:,[j,j-1]] 38 | U = u.T 39 | # ta tìm r vector riêng đầu tiên ứng với U 40 | V= np.zeros((r+1,n)) 41 | for i in range(r+1): 42 | V[i,:n]=(U[i,:m]@A)/(np.sqrt(w[i])) 43 | np.fill_diagonal(sigma, np.sqrt(w)) 44 | #sau đó tìm n - r vector còn lại 45 | if r != n-1: 46 | V = np.concatenate((V,ns), axis =0) 47 | 48 | 49 | # m $ e = \\displaystyle\\sum_{n=0}^{\\infty} \\dfrac{1}{n!} = 1 + \\dfrac{1}{1!} + \\dfrac{1}{2!} + \\dfrac{1}{3!} + \\dfrac{1}{4!} + $ ...\n", 13 | "\n", 14 | "Ta cần tìm số nguyên dương n sao cho\n", 15 | "$|e-e_n| < \\varepsilon $, trong đó $e_n$ là tổng giá trị của n số hạng đầu tiên của công thức trên.\n", 16 | "\n", 17 | "\n", 18 | "- Tương tự ta cũng có công thức tính số pi như sau:\n", 19 | "![](http://www.seriesmathstudy.com/toantructuyen/hangsopi_files/image006.jpg)\n", 20 | "> $\\dfrac{\\pi}{4} =1 - \\dfrac{1}{3} + \\dfrac{1}{5} - \\dfrac{1}{7} + ... + \\dfrac{(-1)^n}{2n+1}$\n", 21 | "\n", 22 | "Cần tìm số nguyên n thỏa mãn\n", 23 | "$|\\pi -\\pi{_n}| < \\varepsilon$\n", 24 | "\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 3, 30 | "id": "db7bdcd2", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "e = 2.7182818011463845\n", 38 | "math.e = 2.718281828459045\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "import math\n", 44 | "\n", 45 | "def calc_e(epsilon):\n", 46 | " e = 1 # sum = 1\n", 47 | " term = 1\n", 48 | " n = 1\n", 49 | " while abs(term) > epsilon:\n", 50 | " term /= n\n", 51 | " e += term\n", 52 | " n += 1\n", 53 | " return e\n", 54 | "\n", 55 | "epsilon = 1e-6 \n", 56 | "e = calc_e(epsilon)\n", 57 | "print(\"e = \", e)\n", 58 | "print(\"math.e = \", math.e)\n", 59 | "\n", 60 | "\n" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "58c41c64", 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "import math\n", 71 | "\n", 72 | "def calc_pi(epsilon):\n", 73 | " pi = 0\n", 74 | " term = 1\n", 75 | " n = 0\n", 76 | " while abs(term) > epsilon:\n", 77 | " pi += term\n", 78 | " n += 1\n", 79 | " term = (-1)**n/ (2*n + 1)\n", 80 | "\n", 81 | " return 4 * pi\n", 82 | "\n", 83 | "epsilon = 1e-6\n", 84 | "pi = calc_pi(epsilon)\n", 85 | "print(\"pi = \", pi)\n", 86 | "print(\"math.pi = \", math.pi)\n" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "e20ff63f", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "kernelspec": { 100 | "display_name": "Python 3 (ipykernel)", 101 | "language": "python", 102 | "name": "python3" 103 | }, 104 | "language_info": { 105 | "codemirror_mode": { 106 | "name": "ipython", 107 | "version": 3 108 | }, 109 | "file_extension": ".py", 110 | "mimetype": "text/x-python", 111 | "name": "python", 112 | "nbconvert_exporter": "python", 113 | "pygments_lexer": "ipython3", 114 | "version": "3.9.13" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 5 119 | } 120 | -------------------------------------------------------------------------------- /Algorithm/.ipynb_checkpoints/Bài 2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "id": "7003c531", 7 | "metadata": {}, 8 | "source": [ 9 | "# Bài 2: Tìm thuật toán tính $\\sqrt[n]{a} $ trong đó n là số tự nhiên, a là số thực cho trước, sai số cho trước.\n", 10 | "\n", 11 | "- Sử dụng phương pháp chia đôi đã code trước đó\n", 12 | "\n", 13 | "> Tính $\\sqrt[n]{a} $ ta quy về bài toán tìm nghiệm của phương trình $x^n - a = 0$ với n, a là các số cho trước\n" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 19, 19 | "id": "01ba6edb", 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "Tìm nghiệm của hàm số f(x) = x^n -a trên khoảng: \n", 27 | "Nhap vao khoang phân ly (a, b): 0 2\n", 28 | "Nhập a: 5\n", 29 | "Nhập n: 100\n", 30 | "Nhap vao sai so: 0.0001\n", 31 | "số lần lặp là: 15\n", 32 | "Cách thuật toán chạy:\n", 33 | "lần 0: a = 0, b = 2, c = 1.0, f(c) = -4.0\n", 34 | "lần 1: a = 1.0, b = 2, c = 1.5, f(c) = 432.8938903808594\n", 35 | "lần 2: a = 1.0, b = 1.5, c = 1.25, f(c) = 23.421709430404007\n", 36 | "lần 3: a = 1.0, b = 1.25, c = 1.125, f(c) = 0.8517779306910427\n", 37 | "lần 4: a = 1.0, b = 1.125, c = 1.0625, f(c) = -2.5172437671843766\n", 38 | "lần 5: a = 1.0625, b = 1.125, c = 1.09375, f(c) = -1.1649503535861059\n", 39 | "lần 6: a = 1.09375, b = 1.125, c = 1.109375, f(c) = -0.255662008657632\n", 40 | "lần 7: a = 1.109375, b = 1.125, c = 1.1171875, f(c) = 0.27097573886192095\n", 41 | "lần 8: a = 1.109375, b = 1.1171875, c = 1.11328125, f(c) = 0.001190763780432036\n", 42 | "lần 9: a = 1.109375, b = 1.11328125, c = 1.111328125, f(c) = -0.1288154758259452\n", 43 | "lần 10: a = 1.111328125, b = 1.11328125, c = 1.1123046875, f(c) = -0.06421184308091021\n", 44 | "lần 11: a = 1.1123046875, b = 1.11328125, c = 1.11279296875, f(c) = -0.03161098210749369\n", 45 | "lần 12: a = 1.11279296875, b = 1.11328125, c = 1.113037109375, f(c) = -0.015235291443606691\n", 46 | "lần 13: a = 1.113037109375, b = 1.11328125, c = 1.1131591796875, f(c) = -0.007028568380474276\n", 47 | "lần 14: a = 1.1131591796875, b = 1.11328125, c = 1.11322021484375, f(c) = -0.0029204795608945844\n", 48 | "Nghiệm của phương trình là:x = 1.11322021484375\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "# bisection method\n", 54 | "\n", 55 | "import math\n", 56 | "\n", 57 | "print(\"Tìm nghiệm của hàm số f(x) = x^n -a trên khoảng: \")\n", 58 | "print(\"Nhap vao khoang phân ly (a, b): \", end = \"\")\n", 59 | "a, b = map(int, input().split())\n", 60 | "\n", 61 | "k = float(input(\"Nhập a: \"))\n", 62 | "n = int(input(\"Nhập n: \"))\n", 63 | "\n", 64 | "epsilon = float(input(\"Nhap vao sai so: \"))\n", 65 | "\n", 66 | "n = float(math.ceil(math.log((b-a)*(epsilon**(-1)), 2)))\n", 67 | "\n", 68 | "print(\"số lần lặp: \", int(n))\n", 69 | "\n", 70 | "def f(x,n,k):\n", 71 | " return x**n-k\n", 72 | "\n", 73 | "def test(a, b):\n", 74 | " i = 0\n", 75 | " while abs(b - a) >= epsilon:\n", 76 | " c = (a+b)/2\n", 77 | " if f(a, n, k)*f(c, n, k) < 0:\n", 78 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c, n, k))\n", 79 | " b = c\n", 80 | " else:\n", 81 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c, n, k))\n", 82 | " a = c\n", 83 | " i +=1\n", 84 | " return c \n", 85 | "print(\"Cách thuật toán chạy:\")\n", 86 | "print(\"Nghiệm của phương trình là:x = \",test(a, b))\n", 87 | "\n", 88 | "\n", 89 | "\n" 90 | ] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Python 3 (ipykernel)", 96 | "language": "python", 97 | "name": "python3" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 3 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython3", 109 | "version": "3.9.13" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 5 114 | } 115 | -------------------------------------------------------------------------------- /Algorithm/.ipynb_checkpoints/Bài 3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "416f289c", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 3: Viết thuật toán cho phương pháp dây cung (tự xác định input và output)\n", 9 | "\n", 10 | "Nguồn: copy tạm chatGPT\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "c074aed7", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "Nghiệm của phương trình là: 2.094552485185284\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | " def secant_method(f, x0, x1, epsilon, N=1000):\n", 29 | " k = 0\n", 30 | " while k < N:\n", 31 | " if abs(f(x1)) < epsilon:\n", 32 | " return x1\n", 33 | " if abs(f(x0) - f(x1)) < epsilon:\n", 34 | " return x1\n", 35 | " x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))\n", 36 | " if abs(f(x2)) < epsilon:\n", 37 | " return x2\n", 38 | " x0 = x1\n", 39 | " x1 = x2\n", 40 | " k += 1\n", 41 | " return x2\n", 42 | "\n", 43 | "# Ví dụ, để tìm nghiệm của phương trình f(x) = x^3 - 2x - 5 = 0 \n", 44 | "#bằng phương pháp dây cung với giá trị ban đầu x0 = 1 và x1 = 2 và sai số epsilon = 0.0001, \n", 45 | "#ta có thể sử dụng code như sau:\n", 46 | "\n", 47 | "def f(x):\n", 48 | " return x**3 - 2*x - 5\n", 49 | "\n", 50 | "x0 = 1\n", 51 | "x1 = 2\n", 52 | "epsilon = 0.0001\n", 53 | "\n", 54 | "solution = secant_method(f, x0, x1, epsilon)\n", 55 | "print(\"Nghiệm của phương trình là:\", solution)\n" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3 (ipykernel)", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.9.13" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 5 80 | } 81 | -------------------------------------------------------------------------------- /Algorithm/.ipynb_checkpoints/Bài 5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "id": "c963d961", 7 | "metadata": {}, 8 | "source": [ 9 | "# Bài 5 Xây dựng thuật toán tìm min, max của hàm cho trước với sai số cho trước\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "0625b868", 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "Giá trị nhỏ nhất của hàm là: 9.313225746154785e-10\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "def f(x):\n", 28 | " return x**2\n", 29 | "\n", 30 | "a = 0\n", 31 | "b = 1\n", 32 | "epsilon = 0.0001\n", 33 | "\n", 34 | "k = (a+b)/2\n", 35 | "fk = f(k)\n", 36 | "while abs(b-a) > epsilon:\n", 37 | " fl = f(k-epsilon)\n", 38 | " fr = f(k+epsilon)\n", 39 | " if fl < fk:\n", 40 | " b = k\n", 41 | " k = (a+b)/2\n", 42 | " fk = f(k)\n", 43 | " elif fr < fk:\n", 44 | " a = k\n", 45 | " k = (a+b)/2\n", 46 | " fk = f(k)\n", 47 | " else:\n", 48 | " break\n", 49 | "\n", 50 | "print(\"Giá trị nhỏ nhất của hàm là:\", fk)\n" 51 | ] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3 (ipykernel)", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.9.13" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 5 75 | } 76 | -------------------------------------------------------------------------------- /Algorithm/.ipynb_checkpoints/Bài 6-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d925bb1b", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 6: Viết thuật toán tính đạo hàm của một hàm cho trước\n", 9 | "\n", 10 | "Trong toán học đạo hàm là 1 đại lượng mô tả sự biến thiên của 1 hàm số nào đó tại 1 điểm\n", 11 | "\n", 12 | "Dựa theo công thức tiếp tuyến:\n", 13 | "> $ y = f'(x_0)(x-x_0) + f(x_0) $\n", 14 | "\n", 15 | "Từ đó suy ra:\n", 16 | "> $f'(x_0) = \\dfrac{y-f(x_0)}{x-x_0} $\n", 17 | "\n", 18 | "hay\n", 19 | "\n", 20 | "> $f'(x_0) = \\displaystyle\\lim_{\\triangle x \\rightarrow 0}\\dfrac{y-f(x_0)}{x-x_0} $\n", 21 | "> Trong đó:\n", 22 | "> - $\\triangle x $ là số gia của đối số tại $x_0$\n", 23 | "> - $\\triangle y $ là số gia tương ứng của hàm số\n", 24 | "\n", 25 | "Định lý: Nếu hàm số y = f(x) có đạo hàm tại x0 thì nó liên tục tại x0.\n", 26 | "\n", 27 | "Lưu ý: Nếu hàm số y = f(x) gián đoạn tại x0 thì nó không có đạo hàm tại điểm x0. Tuy nhiên, một hàm số liên tục tại một điểm cũng có thể không có đạo hàm tại điểm đó." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "id": "687c8db2", 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "eps = float(input())\n", 38 | "def Derivate(f, x):\n", 39 | " return (f(x + eps) - f(x)) / eps\n", 40 | "\n", 41 | "def f(x):\n", 42 | " return x ** 2 \n", 43 | "\n", 44 | "# Tính đạo hàm của hàm số f(x) tại điểm x = 2\n", 45 | "df = Derivate(f, 2)\n", 46 | "print(df)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "f48ca66c", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3 (ipykernel)", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.9.13" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 5 79 | } 80 | -------------------------------------------------------------------------------- /Algorithm/.ipynb_checkpoints/note-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 5 6 | } 7 | -------------------------------------------------------------------------------- /Algorithm/Bài 1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "610574e3", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 1: Xây dựng thuật toán tính số e và $\\pi$ với sai số $\\varepsilon$ cho trước. Viết code chạy thử theo thuật toán bạn đã xây dựng.\n", 9 | "\n", 10 | "- Theo công thức :Số e là tổng của chuỗi vô hạn trong đó n! là giai thừa của n.\n", 11 | "> $ e = \\displaystyle\\sum_{n=0}^{\\infty} \\dfrac{1}{n!} = 1 + \\dfrac{1}{1!} + \\dfrac{1}{2!} + \\dfrac{1}{3!} + \\dfrac{1}{4!} + $ ...\n", 12 | "\n", 13 | "Ta cần tìm số nguyên dương n sao cho\n", 14 | "$|e-e_n| < \\varepsilon $, trong đó $e_n$ là tổng giá trị của n số hạng đầu tiên của công thức trên.\n", 15 | "\n", 16 | "\n", 17 | "- Tương tự ta cũng có công thức tính số pi như sau:\n", 18 | "![](http://www.seriesmathstudy.com/toantructuyen/hangsopi_files/image006.jpg)\n", 19 | "> $\\dfrac{\\pi}{4} =1 - \\dfrac{1}{3} + \\dfrac{1}{5} - \\dfrac{1}{7} + ... + \\dfrac{(-1)^n}{2n+1}$\n", 20 | "\n", 21 | "Cần tìm số nguyên n thỏa mãn\n", 22 | "$|\\pi -\\pi{_n}| < \\varepsilon$\n", 23 | "\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "id": "4f97e311", 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "Requirement already satisfied: jupyter-collaboration in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (1.0.1)\n", 37 | "Requirement already satisfied: jupyter-ydoc<2.0.0,>=1.0.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-collaboration) (1.0.2)\n", 38 | "Requirement already satisfied: jupyter-server-fileid<1,>=0.6.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-collaboration) (0.9.0)\n", 39 | "Requirement already satisfied: jupyter-server<3.0.0,>=2.0.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-collaboration) (2.7.0)\n", 40 | "Requirement already satisfied: jupyter-events in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-collaboration) (0.6.3)\n", 41 | "Requirement already satisfied: ypy-websocket<0.13.0,>=0.12.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-collaboration) (0.12.1)\n", 42 | "Requirement already satisfied: argon2-cffi in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (21.3.0)\n", 43 | "Requirement already satisfied: tornado>=6.2.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (6.3.2)\n", 44 | "Requirement already satisfied: jinja2 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.11.3)\n", 45 | "Requirement already satisfied: traitlets>=5.6.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (5.9.0)\n", 46 | "Requirement already satisfied: jupyter-client>=7.4.4 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (8.3.0)\n", 47 | "Requirement already satisfied: nbconvert>=6.4.4 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (6.4.4)\n", 48 | "Requirement already satisfied: send2trash in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (1.8.0)\n", 49 | "Requirement already satisfied: pywinpty in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.0.11)\n", 50 | "Requirement already satisfied: prometheus-client in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.14.1)\n", 51 | "Requirement already satisfied: jupyter-server-terminals in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.4.4)\n", 52 | "Requirement already satisfied: nbformat>=5.3.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (5.5.0)\n", 53 | "Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (5.3.1)\n", 54 | "Requirement already satisfied: packaging in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (21.3)\n", 55 | "Requirement already satisfied: anyio>=3.1.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (3.7.1)\n", 56 | "Requirement already satisfied: overrides in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (7.3.1)\n", 57 | "Requirement already satisfied: terminado>=0.8.3 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.13.1)\n", 58 | "Requirement already satisfied: pyzmq>=24 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (25.1.0)\n", 59 | "Requirement already satisfied: websocket-client in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.58.0)\n", 60 | "Requirement already satisfied: rfc3986-validator>=0.1.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-events->jupyter-collaboration) (0.1.1)\n", 61 | "Requirement already satisfied: python-json-logger>=2.0.4 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-events->jupyter-collaboration) (2.0.7)\n", 62 | "Requirement already satisfied: jsonschema[format-nongpl]>=3.2.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-events->jupyter-collaboration) (4.16.0)\n", 63 | "Requirement already satisfied: rfc3339-validator in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-events->jupyter-collaboration) (0.1.4)\n", 64 | "Requirement already satisfied: pyyaml>=5.3 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-events->jupyter-collaboration) (6.0)\n", 65 | "Requirement already satisfied: y-py<0.7.0,>=0.6.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-ydoc<2.0.0,>=1.0.1->jupyter-collaboration) (0.6.0)\n", 66 | "Requirement already satisfied: importlib-metadata>=3.6 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-ydoc<2.0.0,>=1.0.1->jupyter-collaboration) (4.11.3)\n", 67 | "Requirement already satisfied: aiosqlite<1,>=0.18.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from ypy-websocket<0.13.0,>=0.12.1->jupyter-collaboration) (0.19.0)\n", 68 | "Requirement already satisfied: idna>=2.8 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from anyio>=3.1.0->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (3.3)\n", 69 | "Requirement already satisfied: sniffio>=1.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from anyio>=3.1.0->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (1.2.0)\n", 70 | "Requirement already satisfied: exceptiongroup in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from anyio>=3.1.0->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (1.1.2)\n", 71 | "Requirement already satisfied: zipp>=0.5 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from importlib-metadata>=3.6->jupyter-ydoc<2.0.0,>=1.0.1->jupyter-collaboration) (3.8.0)\n", 72 | "Requirement already satisfied: attrs>=17.4.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (21.4.0)\n", 73 | "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (0.18.0)\n", 74 | "Requirement already satisfied: isoduration in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (20.11.0)\n", 75 | "Requirement already satisfied: webcolors>=1.11 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (1.13)\n", 76 | "Requirement already satisfied: jsonpointer>1.13 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (2.4)\n", 77 | "Requirement already satisfied: fqdn in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (1.5.1)\n", 78 | "Requirement already satisfied: uri-template in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (1.3.0)\n", 79 | "Requirement already satisfied: python-dateutil>=2.8.2 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-client>=7.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.8.2)\n", 80 | "Requirement already satisfied: platformdirs>=2.5 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-core!=5.0.*,>=4.12->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.5.2)\n", 81 | "Requirement already satisfied: pywin32>=300 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jupyter-core!=5.0.*,>=4.12->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (302)\n", 82 | "Requirement already satisfied: pygments>=2.4.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.11.2)\n", 83 | "Requirement already satisfied: entrypoints>=0.2.2 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.4)\n", 84 | "Requirement already satisfied: testpath in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.6.0)\n", 85 | "Requirement already satisfied: mistune<2,>=0.8.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.8.4)\n", 86 | "Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.5.13)\n", 87 | "Requirement already satisfied: pandocfilters>=1.4.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (1.5.0)\n", 88 | "Requirement already satisfied: jupyterlab-pygments in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.1.2)\n", 89 | "Requirement already satisfied: bleach in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (4.1.0)\n", 90 | "Requirement already satisfied: beautifulsoup4 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (4.11.1)\n", 91 | "Requirement already satisfied: defusedxml in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.7.1)\n", 92 | "Requirement already satisfied: MarkupSafe>=0.23 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from jinja2->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.0.1)\n", 93 | "Requirement already satisfied: fastjsonschema in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbformat>=5.3.0->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.16.2)\n", 94 | "Requirement already satisfied: argon2-cffi-bindings in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from argon2-cffi->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (21.2.0)\n", 95 | "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from packaging->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (3.0.9)\n", 96 | "Requirement already satisfied: six in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from rfc3339-validator->jupyter-events->jupyter-collaboration) (1.16.0)\n", 97 | "Requirement already satisfied: nest-asyncio in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (1.5.5)\n", 98 | "Requirement already satisfied: cffi>=1.0.1 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from argon2-cffi-bindings->argon2-cffi->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (1.15.1)\n", 99 | "Requirement already satisfied: soupsieve>1.2 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from beautifulsoup4->nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.3.1)\n", 100 | "Requirement already satisfied: webencodings in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from bleach->nbconvert>=6.4.4->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (0.5.1)\n", 101 | "Requirement already satisfied: arrow>=0.15.0 in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from isoduration->jsonschema[format-nongpl]>=3.2.0->jupyter-events->jupyter-collaboration) (1.2.2)\n", 102 | "Requirement already satisfied: pycparser in c:\\users\\hi.welcome to net\\anaconda3\\lib\\site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->jupyter-server<3.0.0,>=2.0.0->jupyter-collaboration) (2.21)\n", 103 | "Note: you may need to restart the kernel to use updated packages.\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "pip install jupyter-collaboration" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 3, 114 | "id": "2f78a6e8", 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "e = 2.7182818011463845\n", 122 | "math.e = 2.718281828459045\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "import math\n", 128 | "\n", 129 | "def calc_e(epsilon):\n", 130 | " e = 1 # sum = 1\n", 131 | " term = 1\n", 132 | " n = 1\n", 133 | " while abs(term) > epsilon:\n", 134 | " term /= n\n", 135 | " e += term\n", 136 | " n += 1\n", 137 | " return e\n", 138 | "\n", 139 | "epsilon = 1e-6 \n", 140 | "e = calc_e(epsilon)\n", 141 | "print(\"e = \", e)\n", 142 | "print(\"math.e = \", math.e)\n", 143 | "\n", 144 | "\n" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "id": "d5628206", 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "import math\n", 155 | "\n", 156 | "def calc_pi(epsilon):\n", 157 | " pi = 0\n", 158 | " term = 1\n", 159 | " n = 0\n", 160 | " while abs(term) > epsilon:\n", 161 | " pi += term\n", 162 | " n += 1\n", 163 | " term = (-1)**n/ (2*n + 1)\n", 164 | "\n", 165 | " return 4 * pi\n", 166 | "\n", 167 | "epsilon = 1e-6\n", 168 | "pi = calc_pi(epsilon)\n", 169 | "print(\"pi = \", pi)\n", 170 | "print(\"math.pi = \", math.pi)\n" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "id": "6f94d4d2", 176 | "metadata": {}, 177 | "source": [ 178 | "# code lại 2 bài trên bằng cách sử dụng thuật toán chia đôi (besection method)\n", 179 | "\n", 180 | " $\\ln x - 1 = 0$ là phương trình để tính gần đúng số e\n", 181 | "\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 2, 187 | "id": "def09651", 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "Tìm nghiệm của hàm số f(x) = lnx - 1 trên khoảng: \n", 195 | "Nhap vao khoang phân ly (a, b): 0 100\n", 196 | "Nhap vao sai so: 0.0000001\n", 197 | "số lần lặp: 30\n", 198 | "Cách thuật toán chạy:\n", 199 | "lần 0: a = 0, b = 100, c = 50.0, f(c) = 568757485.0\n", 200 | "lần 1: a = 0, b = 50.0, c = 25.0, f(c) = 25783110.0\n", 201 | "lần 2: a = 0, b = 25.0, c = 12.5, f(c) = 1306606.09375\n", 202 | "lần 3: a = 0, b = 12.5, c = 6.25, f(c) = 72199.9658203125\n", 203 | "lần 4: a = 0, b = 6.25, c = 3.125, f(c) = 4222.384796142578\n", 204 | "lần 5: a = 0, b = 3.125, c = 1.5625, f(c) = 246.01648807525635\n", 205 | "lần 6: a = 0, b = 1.5625, c = 0.78125, f(c) = 2.3957832157611847\n", 206 | "lần 7: a = 0, b = 0.78125, c = 0.390625, f(c) = -13.578535742126405\n", 207 | "lần 8: a = 0.390625, b = 0.78125, c = 0.5859375, f(c) = -9.068275937024737\n", 208 | "lần 9: a = 0.5859375, b = 0.78125, c = 0.68359375, f(c) = -4.49566581416093\n", 209 | "lần 10: a = 0.68359375, b = 0.78125, c = 0.732421875, f(c) = -1.3813435614368075\n", 210 | "lần 11: a = 0.732421875, b = 0.78125, c = 0.7568359375, f(c) = 0.41884353693345844\n", 211 | "lần 12: a = 0.732421875, b = 0.7568359375, c = 0.74462890625, f(c) = -0.5026384815572484\n", 212 | "lần 13: a = 0.74462890625, b = 0.7568359375, c = 0.750732421875, f(c) = -0.04733185816789032\n", 213 | "lần 14: a = 0.750732421875, b = 0.7568359375, c = 0.7537841796875, f(c) = 0.18438625286338706\n", 214 | "lần 15: a = 0.750732421875, b = 0.7537841796875, c = 0.75225830078125, f(c) = 0.06818617811434713\n", 215 | "lần 16: a = 0.750732421875, b = 0.75225830078125, c = 0.751495361328125, f(c) = 0.010342077020927931\n", 216 | "lần 17: a = 0.750732421875, b = 0.751495361328125, c = 0.7511138916015625, f(c) = -0.01851613984945466\n", 217 | "lần 18: a = 0.7511138916015625, b = 0.751495361328125, c = 0.7513046264648438, f(c) = -0.004092346415131232\n", 218 | "lần 19: a = 0.7513046264648438, b = 0.751495361328125, c = 0.7513999938964844, f(c) = 0.0031235362173696046\n", 219 | "lần 20: a = 0.7513046264648438, b = 0.7513999938964844, c = 0.7513523101806641, f(c) = -0.00048473732834786176\n", 220 | "lần 21: a = 0.7513523101806641, b = 0.7513999938964844, c = 0.7513761520385742, f(c) = 0.0013193163819078535\n", 221 | "lần 22: a = 0.7513523101806641, b = 0.7513761520385742, c = 0.7513642311096191, f(c) = 0.0004172687617849391\n", 222 | "lần 23: a = 0.7513523101806641, b = 0.7513642311096191, c = 0.7513582706451416, f(c) = -3.3739474449845375e-05\n", 223 | "lần 24: a = 0.7513582706451416, b = 0.7513642311096191, c = 0.7513612508773804, f(c) = 0.0001917633458656809\n", 224 | "lần 25: a = 0.7513582706451416, b = 0.7513612508773804, c = 0.751359760761261, f(c) = 7.901161125900558e-05\n", 225 | "lần 26: a = 0.7513582706451416, b = 0.751359760761261, c = 0.7513590157032013, f(c) = 2.26359872925741e-05\n", 226 | "lần 27: a = 0.7513582706451416, b = 0.7513590157032013, c = 0.7513586431741714, f(c) = -5.551763855748959e-06\n", 227 | "lần 28: a = 0.7513586431741714, b = 0.7513590157032013, c = 0.7513588294386864, f(c) = 8.542106650466508e-06\n", 228 | "lần 29: a = 0.7513586431741714, b = 0.7513588294386864, c = 0.7513587363064289, f(c) = 1.4951701281518126e-06\n", 229 | "Nghiệm của phương trình là:x = 0.7513587363064289\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "### bisection method\n", 235 | "\n", 236 | "import math\n", 237 | "\n", 238 | "print(\"Tìm nghiệm của hàm số f(x) = lnx - 1 trên khoảng: \")\n", 239 | "print(\"Nhap vao khoang phân ly (a, b): \", end = \"\")\n", 240 | "a, b = map(int, input().split())\n", 241 | "\n", 242 | "epsilon = float(input(\"Nhap vao sai so: \"))\n", 243 | "\n", 244 | "n = float(math.ceil(math.log((b-a)*(epsilon**(-1)), 2)))\n", 245 | "\n", 246 | "print(\"số lần lặp: \", int(n))\n", 247 | "\n", 248 | "def f(x):\n", 249 | " return math.pow(x, 5)+ 41*(math.pow(x, 4))+ 3*(math.pow(x, 2)) - 15\n", 250 | "\n", 251 | "def test(a, b):\n", 252 | " i = 0\n", 253 | " while abs(b - a) >= epsilon:\n", 254 | " c = (a+b)/2\n", 255 | " if f(a)*f(c) < 0:\n", 256 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c))\n", 257 | " b = c\n", 258 | " else:\n", 259 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c))\n", 260 | " a = c\n", 261 | " i +=1\n", 262 | " return c \n", 263 | "print(\"Cách thuật toán chạy:\")\n", 264 | "print(\"Nghiệm của phương trình là:x = \",test(a, b))\n", 265 | "\n", 266 | "\n", 267 | "\n" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "id": "944a0080", 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "# bisection method\n", 278 | "\n", 279 | "import math\n", 280 | "\n", 281 | "print(\"Tìm nghiệm của hàm số f(x) = x^x -10 trên khoảng: \")\n", 282 | "# (a, b)\n", 283 | "print(\"Nhap vao khoang (a, b): \", end = \"\")\n", 284 | "a, b = map(int, input().split())\n", 285 | "\n", 286 | "# enter error value\n", 287 | "\n", 288 | "error = float(input(\"Nhap vao sai so: \"))\n", 289 | "\n", 290 | "n = float(math.ceil(math.log((b-a)*(error**(-1)), 2)))\n", 291 | "\n", 292 | "print(\"số lần lặp là: \", int(n))\n", 293 | "\n", 294 | "def f(x):\n", 295 | " return x**x-10\n", 296 | "\n", 297 | "def test(a, b):\n", 298 | " i = 0\n", 299 | " while abs(b - a) >= error:\n", 300 | " c = (a+b)/2\n", 301 | " if f(a)*f(c) < 0:\n", 302 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c))\n", 303 | " b = c\n", 304 | " else:\n", 305 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c))\n", 306 | " a = c\n", 307 | " i +=1\n", 308 | " return c \n", 309 | "print(\"Cách thuật toán chạy:\")\n", 310 | "print(\"Nghiệm của phương trình là: \",test(a, b))\n", 311 | "\n", 312 | "\n", 313 | "\n" 314 | ] 315 | } 316 | ], 317 | "metadata": { 318 | "kernelspec": { 319 | "display_name": "Python 3 (ipykernel)", 320 | "language": "python", 321 | "name": "python3" 322 | }, 323 | "language_info": { 324 | "codemirror_mode": { 325 | "name": "ipython", 326 | "version": 3 327 | }, 328 | "file_extension": ".py", 329 | "mimetype": "text/x-python", 330 | "name": "python", 331 | "nbconvert_exporter": "python", 332 | "pygments_lexer": "ipython3", 333 | "version": "3.9.13" 334 | } 335 | }, 336 | "nbformat": 4, 337 | "nbformat_minor": 5 338 | } 339 | -------------------------------------------------------------------------------- /Algorithm/Bài 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7003c531", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 2: Tìm thuật toán tính $\\sqrt[n]{a} $ trong đó n là số tự nhiên, a là số thực cho trước, sai số cho trước.\n", 9 | "\n", 10 | "- Sử dụng phương pháp chia đôi đã code trước đó\n", 11 | "\n", 12 | "> Tính $\\sqrt[n]{a} $ ta quy về bài toán tìm nghiệm của phương trình $x^n - a = 0$ với n, a là các số cho trước\n", 13 | "\n", 14 | "1 số ví dụ: $\\ln x - 1 = 0$ là phương trình để tính gần đúng số e\n", 15 | "\n", 16 | "$\\sin x = 0$ \n", 17 | "\n", 18 | "$\\dfrac{\\pi^2}{6} = \\displaystyle \\sum _{n=1}^{\\infty} \\dfrac{1}{n^2}$\n", 19 | "\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 6, 25 | "id": "01ba6edb", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Tìm nghiệm của hàm số f(x) = x^n -a trên khoảng: \n", 33 | "Nhap vao khoang phân ly (a, b): -2 -1\n", 34 | "Nhập k: -3\n", 35 | "Nhập n: 5\n", 36 | "Nhap vao sai so: 0.00000000005\n", 37 | "số lần lặp: 35\n", 38 | "Cách thuật toán chạy:\n", 39 | "lần 0: a = -2, b = -1, c = -1.5, f(c) = -4.59375\n", 40 | "lần 1: a = -1.5, b = -1, c = -1.25, f(c) = -0.0517578125\n", 41 | "lần 2: a = -1.25, b = -1, c = -1.125, f(c) = 1.197967529296875\n", 42 | "lần 3: a = -1.25, b = -1.125, c = -1.1875, f(c) = 0.6386079788208008\n", 43 | "lần 4: a = -1.25, b = -1.1875, c = -1.21875, f(c) = 0.311109334230423\n", 44 | "lần 5: a = -1.25, b = -1.21875, c = -1.234375, f(c) = 0.13426791224628687\n", 45 | "lần 6: a = -1.25, b = -1.234375, c = -1.2421875, f(c) = 0.042424953571753576\n", 46 | "lần 7: a = -1.25, b = -1.2421875, c = -1.24609375, f(c) = -0.004371190035271866\n", 47 | "lần 8: a = -1.24609375, b = -1.2421875, c = -1.244140625, f(c) = 0.019100344830434324\n", 48 | "lần 9: a = -1.24609375, b = -1.244140625, c = -1.2451171875, f(c) = 0.00738298642754831\n", 49 | "lần 10: a = -1.24609375, b = -1.2451171875, c = -1.24560546875, f(c) = 0.0015105058691111672\n", 50 | "lần 11: a = -1.24609375, b = -1.24560546875, c = -1.245849609375, f(c) = -0.0014291894874376432\n", 51 | "lần 12: a = -1.245849609375, b = -1.24560546875, c = -1.2457275390625, f(c) = 4.0946255051910185e-05\n", 52 | "lần 13: a = -1.245849609375, b = -1.2457275390625, c = -1.24578857421875, f(c) = -0.000694049589553547\n", 53 | "lần 14: a = -1.24578857421875, b = -1.2457275390625, c = -1.245758056640625, f(c) = -0.0003265336619144854\n", 54 | "lần 15: a = -1.245758056640625, b = -1.2457275390625, c = -1.2457427978515625, f(c) = -0.00014278920226251657\n", 55 | "lần 16: a = -1.2457427978515625, b = -1.2457275390625, c = -1.2457351684570312, f(c) = -5.09203483338716e-05\n", 56 | "lần 17: a = -1.2457351684570312, b = -1.2457275390625, c = -1.2457313537597656, f(c) = -4.98676532600939e-06\n", 57 | "lần 18: a = -1.2457313537597656, b = -1.2457275390625, c = -1.2457294464111328, f(c) = 1.797981519136016e-05\n", 58 | "lần 19: a = -1.2457313537597656, b = -1.2457294464111328, c = -1.2457304000854492, f(c) = 6.496542515055381e-06\n", 59 | "lần 20: a = -1.2457313537597656, b = -1.2457304000854492, c = -1.2457308769226074, f(c) = 7.548929903400392e-07\n", 60 | "lần 21: a = -1.2457313537597656, b = -1.2457308769226074, c = -1.2457311153411865, f(c) = -2.1159350689359258e-06\n", 61 | "lần 22: a = -1.2457311153411865, b = -1.2457308769226074, c = -1.245730996131897, f(c) = -6.80520764628767e-07\n", 62 | "lần 23: a = -1.245730996131897, b = -1.2457308769226074, c = -1.2457309365272522, f(c) = 3.718618124537443e-08\n", 63 | "lần 24: a = -1.245730996131897, b = -1.2457309365272522, c = -1.2457309663295746, f(c) = -3.216672745942617e-07\n", 64 | "lần 25: a = -1.2457309663295746, b = -1.2457309365272522, c = -1.2457309514284134, f(c) = -1.4224054245559614e-07\n", 65 | "lần 26: a = -1.2457309514284134, b = -1.2457309365272522, c = -1.2457309439778328, f(c) = -5.252717949488783e-08\n", 66 | "lần 27: a = -1.2457309439778328, b = -1.2457309365272522, c = -1.2457309402525425, f(c) = -7.670498902712097e-09\n", 67 | "lần 28: a = -1.2457309402525425, b = -1.2457309365272522, c = -1.2457309383898973, f(c) = 1.4757841615420375e-08\n", 68 | "lần 29: a = -1.2457309402525425, b = -1.2457309383898973, c = -1.24573093932122, f(c) = 3.543671578398744e-09\n", 69 | "lần 30: a = -1.2457309402525425, b = -1.24573093932122, c = -1.2457309397868812, f(c) = -2.0634138842012817e-09\n", 70 | "lần 31: a = -1.2457309397868812, b = -1.24573093932122, c = -1.2457309395540506, f(c) = 7.401288470987311e-10\n", 71 | "lần 32: a = -1.2457309397868812, b = -1.2457309395540506, c = -1.2457309396704659, f(c) = -6.616422965066704e-10\n", 72 | "lần 33: a = -1.2457309396704659, b = -1.2457309395540506, c = -1.2457309396122582, f(c) = 3.9243275296030333e-11\n", 73 | "lần 34: a = -1.2457309396704659, b = -1.2457309396122582, c = -1.245730939641362, f(c) = -3.1119951060532003e-10\n", 74 | "Nghiệm của phương trình là:x = -1.245730939641362\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "# bisection method\n", 80 | "\n", 81 | "import math\n", 82 | "\n", 83 | "print(\"Tìm nghiệm của hàm số f(x) = x^n -a trên khoảng: \")\n", 84 | "print(\"Nhap vao khoang phân ly (a, b): \", end = \"\")\n", 85 | "a, b = map(int, input().split())\n", 86 | "\n", 87 | "k = float(input(\"Nhập k: \"))\n", 88 | "n = int(input(\"Nhập n: \"))\n", 89 | "\n", 90 | "epsilon = float(input(\"Nhap vao sai so: \"))\n", 91 | "\n", 92 | "t = float(math.ceil(math.log((b-a)*(epsilon**(-1)), 2)))\n", 93 | "\n", 94 | "print(\"số lần lặp: \", int(t))\n", 95 | "\n", 96 | "def f(x,n,k):\n", 97 | " return x**n-k\n", 98 | "\n", 99 | "def test(a, b):\n", 100 | " i = 0\n", 101 | " while abs(b - a) >= epsilon:\n", 102 | " c = (a+b)/2\n", 103 | " if f(a, n, k)*f(c, n, k) < 0:\n", 104 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c, n, k))\n", 105 | " b = c\n", 106 | " else:\n", 107 | " print(f\"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = \", f(c, n, k))\n", 108 | " a = c\n", 109 | " i +=1\n", 110 | " return c \n", 111 | "print(\"Cách thuật toán chạy:\")\n", 112 | "print(\"Nghiệm của phương trình là:x = \",test(a, b))\n", 113 | "\n", 114 | "\n", 115 | "\n" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3 (ipykernel)", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.9.13" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 5 140 | } 141 | -------------------------------------------------------------------------------- /Algorithm/Bài 3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "416f289c", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 3: Viết thuật toán cho phương pháp dây cung (tự xác định input và output)\n", 9 | "\n", 10 | "Nguồn: copy tạm chatGPT\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "c074aed7", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "Nghiệm của phương trình là: 2.094552485185284\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | " def secant_method(f, x0, x1, epsilon, N=1000):\n", 29 | " k = 0\n", 30 | " while k < N:\n", 31 | " if abs(f(x1)) < epsilon:\n", 32 | " return x1\n", 33 | " if abs(f(x0) - f(x1)) < epsilon:\n", 34 | " return x1\n", 35 | " x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))\n", 36 | " if abs(f(x2)) < epsilon:\n", 37 | " return x2\n", 38 | " x0 = x1\n", 39 | " x1 = x2\n", 40 | " k += 1\n", 41 | " return x2\n", 42 | "\n", 43 | "# Ví dụ, để tìm nghiệm của phương trình f(x) = x^3 - 2x - 5 = 0 \n", 44 | "#bằng phương pháp dây cung với giá trị ban đầu x0 = 1 và x1 = 2 và sai số epsilon = 0.0001, \n", 45 | "#ta có thể sử dụng code như sau:\n", 46 | "\n", 47 | "def f(x):\n", 48 | " return x**3 - 2*x - 5\n", 49 | "\n", 50 | "x0 = 1\n", 51 | "x1 = 2\n", 52 | "epsilon = 0.0001\n", 53 | "\n", 54 | "solution = secant_method(f, x0, x1, epsilon)\n", 55 | "print(\"Nghiệm của phương trình là:\", solution)\n" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3 (ipykernel)", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.9.13" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 5 80 | } 81 | -------------------------------------------------------------------------------- /Algorithm/Bài 5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c963d961", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 5 Xây dựng thuật toán tìm min, max của hàm cho trước với sai số cho trước\n", 9 | "\n", 10 | "> Sử dụng thuật toán Gradient Desent\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "0625b868", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "Giá trị nhỏ nhất của hàm là: 9.313225746154785e-10\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "def f(x):\n", 29 | " return x**2\n", 30 | "\n", 31 | "a = 0\n", 32 | "b = 1\n", 33 | "epsilon = 0.0001\n", 34 | "\n", 35 | "k = (a+b)/2\n", 36 | "fk = f(k)\n", 37 | "while abs(b-a) > epsilon:\n", 38 | " fl = f(k-epsilon)\n", 39 | " fr = f(k+epsilon)\n", 40 | " if fl < fk:\n", 41 | " b = k\n", 42 | " k = (a+b)/2\n", 43 | " fk = f(k)\n", 44 | " elif fr < fk:\n", 45 | " a = k\n", 46 | " k = (a+b)/2a\n", 47 | " fk = f(k)\n", 48 | " else:\n", 49 | " break\n", 50 | "\n", 51 | "print(\"Giá trị nhỏ nhất của hàm là:\", fk)\n" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 1, 57 | "id": "0f92d3a3", 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "Làm tròn đến chữ số thứ : 5\n", 65 | " Nhập cận dưới là: 0\n", 66 | " Nhập cận trên là : 1\n", 67 | "Enter function y = f(x)\n", 68 | "y = x^2\n", 69 | "1.0\n", 70 | "0.0\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "from sympy import *\n", 76 | "from math import log\n", 77 | "\n", 78 | "ETA = 1e-15\n", 79 | "EPSILON = 1e-6\n", 80 | "def f_(f, x,eps=EPSILON):\n", 81 | " return (f(x+eps)-f(x-eps))/(2*eps)\n", 82 | "\n", 83 | "def gradient_descent(f, a, b):\n", 84 | " def fixeta(dfx0,x0):\n", 85 | " eta = ETA\n", 86 | " while sgn*f_(f,x0+sgn*eta*dfx0)>=0 and eta<1:\n", 87 | " eta *= 2\n", 88 | " while sgn*f_(f,x0+sgn*eta*dfx0)<=0:\n", 89 | " eta /= 2\n", 90 | " return eta\n", 91 | " sgn, count = 1 if f(a+EPSILON) > f(a) else -1, 0\n", 92 | " xr = [a]\n", 93 | " x0 = a + 0.5*EPSILON\n", 94 | " eta = ETA\n", 95 | " while x0 < b:\n", 96 | " dx = f_(f, x0)\n", 97 | " while abs(dx) > 0.001*EPSILON:\n", 98 | " dx = f_(f, x0)\n", 99 | " eta = fixeta(dx,x0)\n", 100 | " x0 += sgn*eta*dx\n", 101 | " count += 1\n", 102 | " if x0 > b:\n", 103 | " break\n", 104 | " sgn = -sgn\n", 105 | " if x0 > a and x0 < b:\n", 106 | " xr.append(x0)\n", 107 | " x0 += 0.5*EPSILON\n", 108 | " xr.append(b)\n", 109 | " return xr\n", 110 | "\n", 111 | "def findMax(f, a, b):\n", 112 | " if a>b:\n", 113 | " a,b = b,a\n", 114 | " lastPoints = gradient_descent(f, a, b)\n", 115 | " result = max(map(f, lastPoints))\n", 116 | " return result\n", 117 | "def findMin(f, a, b):\n", 118 | " if a>b:\n", 119 | " a,b = b,a\n", 120 | " lastPoints = gradient_descent(f, a, b)\n", 121 | " result = min(map(f, lastPoints))\n", 122 | " return result\n", 123 | "\n", 124 | "x = symbols(\"x\")\n", 125 | "e = int(input(\"Làm tròn đến chữ số thứ : \"))\n", 126 | "\n", 127 | "while True:\n", 128 | " a = float(input(\" Nhập cận dưới là: \"))\n", 129 | " b = float(input(\" Nhập cận trên là : \"))\n", 130 | " if a < b:\n", 131 | " break\n", 132 | "\n", 133 | "hamso = input(\"Enter function y = f(x)\\ny = \")\n", 134 | "f = lambdify(x, sympify(hamso), \"math\")\n", 135 | "\n", 136 | " \n", 137 | "print(findMax(f, a,b))\n", 138 | "print(findMin(f, a,b))\n", 139 | "print(gradient_desent(f, a, b))\n" 140 | ] 141 | } 142 | ], 143 | "metadata": { 144 | "kernelspec": { 145 | "display_name": "Python 3 (ipykernel)", 146 | "language": "python", 147 | "name": "python3" 148 | }, 149 | "language_info": { 150 | "codemirror_mode": { 151 | "name": "ipython", 152 | "version": 3 153 | }, 154 | "file_extension": ".py", 155 | "mimetype": "text/x-python", 156 | "name": "python", 157 | "nbconvert_exporter": "python", 158 | "pygments_lexer": "ipython3", 159 | "version": "3.9.13" 160 | } 161 | }, 162 | "nbformat": 4, 163 | "nbformat_minor": 5 164 | } 165 | -------------------------------------------------------------------------------- /Algorithm/Bài 6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d925bb1b", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bài 6: Viết thuật toán tính đạo hàm của một hàm cho trước\n", 9 | "\n", 10 | "Trong toán học đạo hàm là 1 đại lượng mô tả sự biến thiên của 1 hàm số nào đó tại 1 điểm\n", 11 | "\n", 12 | "Dựa theo công thức tiếp tuyến:\n", 13 | "> $ y = f'(x_0)(x-x_0) + f(x_0) $\n", 14 | "\n", 15 | "Từ đó suy ra:\n", 16 | "> $f'(x_0) = \\dfrac{y-f(x_0)}{x-x_0} $\n", 17 | "\n", 18 | "hay\n", 19 | "\n", 20 | "> $f'(x_0) = \\displaystyle\\lim_{\\triangle x \\rightarrow 0}\\dfrac{y-f(x_0)}{x-x_0} $\n", 21 | "> Trong đó:\n", 22 | "> - $\\triangle x $ là số gia của đối số tại $x_0$\n", 23 | "> - $\\triangle y $ là số gia tương ứng của hàm số\n", 24 | "\n", 25 | "Định lý: Nếu hàm số y = f(x) có đạo hàm tại x0 thì nó liên tục tại x0.\n", 26 | "\n", 27 | "Lưu ý: Nếu hàm số y = f(x) gián đoạn tại x0 thì nó không có đạo hàm tại điểm x0. Tuy nhiên, một hàm số liên tục tại một điểm cũng có thể không có đạo hàm tại điểm đó." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "id": "687c8db2", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "0.000001\n", 41 | "4.0000010006480125\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "eps = float(input(\"Nhap vao sai so: \"))\n", 47 | "def Derivate(f, x):\n", 48 | " return (f(x + eps) - f(x)) / eps\n", 49 | "\n", 50 | "def f(x):\n", 51 | " return x ** 2 \n", 52 | "\n", 53 | "# Tính đạo hàm của hàm số f(x) tại điểm x = 2\n", 54 | "df = Derivate(f, 2)\n", 55 | "print(df)\n" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3 (ipykernel)", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.9.13" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 5 80 | } 81 | -------------------------------------------------------------------------------- /Algorithm/note.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7b498b86", 6 | "metadata": {}, 7 | "source": [ 8 | "# note kiến thức\n", 9 | "\n", 10 | "|nghiệm| < 1 + $\\dfrac{max{|a_0|, |a_1|,...|a_{n-1}}}{|a_n|}$\n", 11 | "\n" 12 | ] 13 | } 14 | ], 15 | "metadata": { 16 | "kernelspec": { 17 | "display_name": "Python 3 (ipykernel)", 18 | "language": "python", 19 | "name": "python3" 20 | }, 21 | "language_info": { 22 | "codemirror_mode": { 23 | "name": "ipython", 24 | "version": 3 25 | }, 26 | "file_extension": ".py", 27 | "mimetype": "text/x-python", 28 | "name": "python", 29 | "nbconvert_exporter": "python", 30 | "pygments_lexer": "ipython3", 31 | "version": "3.9.13" 32 | } 33 | }, 34 | "nbformat": 4, 35 | "nbformat_minor": 5 36 | } 37 | -------------------------------------------------------------------------------- /Algorithm/output_tn.txt: -------------------------------------------------------------------------------- 1 | 2 | _____________________________________________________________________________________ 3 | | Lần lặp | Nghiệm x | f(x) | Sai số | 4 | _____________________________________________________________________________________ 5 | | 1 | 4.6251092 | -0.0014201 | 0.0120471 | 6 | | 2 | 4.6362646 | -2.64e-05 | 0.0002242 | 7 | | 3 | 4.6364722 | -5e-07 | 4.2e-06 | 8 | _____________________________________________________________________________________ 9 | Nghiệm của phương trình: 4.636472153352888 -------------------------------------------------------------------------------- /Bipartiteness_Graph.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | def is_bipartite(graph, start, color): 4 | queue = deque() 5 | 6 | color[start] = 1 7 | queue.append(start) 8 | 9 | while queue: 10 | current_vertex = queue.popleft() 11 | 12 | for neighbor in range(len(graph)): 13 | if graph[current_vertex][neighbor] == 1: 14 | if neighbor not in color: 15 | color[neighbor] = 3 - color[current_vertex] # Gán màu khác với đỉnh hiện tại 16 | queue.append(neighbor) 17 | elif color[neighbor] == color[current_vertex]: 18 | return False # Đồ thị không là đồ thị hai phía 19 | 20 | return True # Đồ thị là đồ thị hai phía 21 | 22 | def is_bipartite_graph(adj_matrix, start): 23 | n = len(adj_matrix) 24 | color = {} # Màu của mỗi đỉnh: 1 hoặc 2 25 | 26 | if start not in color: 27 | if not is_bipartite(adj_matrix, start, color): 28 | return False 29 | return True 30 | 31 | # Example usage: 32 | n = 6 # Số đỉnh 33 | adj_matrix = [ 34 | [0, 1, 1, 0, 0, 0], 35 | [1, 0, 0, 1, 0, 0], 36 | [1, 0, 0, 0, 1, 0], 37 | [0, 1, 0, 0, 0, 1], 38 | [0, 0, 1, 0, 0, 1], 39 | [0, 0, 0, 1, 1, 0] 40 | ] 41 | 42 | start_vertex = 0 # Đỉnh bắt đầu 43 | 44 | result = is_bipartite_graph(adj_matrix, start_vertex) 45 | print("Is the graph bipartite?", result) 46 | -------------------------------------------------------------------------------- /Bisection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyvu15/Algo/d183dbe7255651eb111f98cb96618596fcf35e3f/Bisection.png -------------------------------------------------------------------------------- /Bisection_Dia.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyvu15/Algo/d183dbe7255651eb111f98cb96618596fcf35e3f/Bisection_Dia.png -------------------------------------------------------------------------------- /Bisection_Method.py: -------------------------------------------------------------------------------- 1 | # bisection method 2 | 3 | import math 4 | 5 | print("Tìm nghiệm của hàm số f(x) = x^x -10 trên khoảng: ") 6 | # (a, b) 7 | print("Nhap vao khoang (a, b): ", end = "") 8 | a, b = map(int, input().split()) 9 | 10 | # enter error value 11 | 12 | error = float(input("Nhap vao sai so: ")) 13 | 14 | n = float(math.ceil(math.log((b-a)*(error**(-1)), 2))) 15 | 16 | print("số lần lặp là: ", int(n)) 17 | 18 | def f(x): 19 | return x**x-10 20 | 21 | def test(a, b): 22 | i = 0 23 | while abs(b - a) >= error: 24 | c = (a+b)/2 25 | if f(a)*f(c) < 0: 26 | print(f"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = ", f(c)) 27 | b = c 28 | else: 29 | print(f"lần {i}: a = {a}, b = {b}, c = {c}, f(c) = ", f(c)) 30 | a = c 31 | i +=1 32 | return c 33 | print("Cách thuật toán chạy:") 34 | print("Nghiệm của phương trình là: ",test(a, b)) 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Bowstring_method.py: -------------------------------------------------------------------------------- 1 | # bowstring method 2 | 3 | -------------------------------------------------------------------------------- /Gauss.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | n = int(input("Nhập vào số ẩn: ")) 4 | 5 | a = [[2, -1, 1, 4], 6 | [3, 2, -2, -1], 7 | [1, 3, -1, 3]] 8 | 9 | def Gauss(a, n): 10 | for i in n: 11 | for j in n: 12 | if a[i][j] == 0 and i == j: 13 | l = i+1 14 | 15 | if a[l][i] == 0: 16 | l+=1 17 | pass 18 | 19 | 20 | 21 | 22 | if __name__ == '__main__': 23 | pass 24 | -------------------------------------------------------------------------------- /Insert a node in BST.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyvu15/Algo/d183dbe7255651eb111f98cb96618596fcf35e3f/Insert a node in BST.pptx -------------------------------------------------------------------------------- /Iteration_method.py: -------------------------------------------------------------------------------- 1 | #iteration_method 2 | import math 3 | from tabulate import tabulate 4 | 5 | print("Tìm nghiệm của phương trình x**3+x-1000 trên khoảng phân ly: ", end = '') 6 | a, b = map(int, input().split()) 7 | print("Nhap vao ham \u03C6 (x): math.pow(12x+5, 1/3)") # \u03C6 là ký hiệu toán học 8 | error = float(input("Nhập vào sai số: ")) 9 | 10 | def phi(x): 11 | return math.pow(1000-x, 1/3) 12 | 13 | X_n = [[0, 10]] 14 | 15 | def test(a, b): 16 | x_1= phi(b) 17 | i = 1 18 | while abs(x_1-b) > error: 19 | ls = [i, x_1] 20 | X_n.append(ls) 21 | b = x_1 22 | x_1 = phi(b) 23 | i +=1 24 | 25 | col_names = ["n", "X_n"] 26 | 27 | print("Kết quả được ghi trong bảng sau: ") 28 | print(tabulate(X_n, headers=col_names, tablefmt="fancy_grid")) 29 | 30 | test(a, b) 31 | 32 | -------------------------------------------------------------------------------- /KMean/pro.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from random import randint 3 | import math 4 | from sklearn.cluster import KMeans 5 | 6 | def distance(p1,p2): 7 | return math.sqrt((p1[0]-p2[0]) * (p1[0]-p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])) 8 | 9 | pygame.init() 10 | 11 | screen = pygame.display.set_mode((1200,700)) 12 | 13 | pygame.display.set_caption("Kmeans visualization") 14 | 15 | running = True 16 | 17 | clock = pygame.time.Clock() 18 | 19 | BACKGROUND = (214, 214, 214) 20 | BLACK = (0,0,0) 21 | BACKGROUND_PANEL = (249, 255, 230) 22 | WHITE = (255,255,255) 23 | 24 | RED = (255,0,0) 25 | GREEN = (0,255,0) 26 | BLUE = (0,0,255) 27 | YELLOW = (147, 153, 35) 28 | PURPLE = (255,0,255) 29 | SKY = (0,255,255) 30 | ORANGE = (255,125,25) 31 | GRAPE = (100,25,125) 32 | GRASS = (55,155,65) 33 | 34 | COLORS = [RED,GREEN,BLUE,YELLOW,PURPLE,SKY,ORANGE,GRAPE,GRASS] 35 | 36 | font = pygame.font.SysFont('sans', 40) 37 | font_small = pygame.font.SysFont('sans', 20) 38 | text_plus = font.render('+', True, WHITE) 39 | text_minus = font.render('-', True, WHITE) 40 | text_run = font.render("Run", True, WHITE) 41 | text_random = font.render("Random", True, WHITE) 42 | text_algorithm = font.render("Algorithm", True, WHITE) 43 | text_reset = font.render("Reset", True, WHITE) 44 | 45 | K = 0 46 | error = 0 47 | points = [] 48 | clusters = [] 49 | labels = [] 50 | 51 | while running: 52 | clock.tick(60) 53 | screen.fill(BACKGROUND) 54 | mouse_x, mouse_y = pygame.mouse.get_pos() 55 | 56 | # Draw interface 57 | # Draw panel 58 | pygame.draw.rect(screen, BLACK, (50,50,700,500)) 59 | pygame.draw.rect(screen, BACKGROUND_PANEL, (55,55,690,490)) 60 | 61 | # K button + 62 | pygame.draw.rect(screen, BLACK, (850,50,50,50)) 63 | screen.blit(text_plus, (860,50)) 64 | 65 | # K button - 66 | pygame.draw.rect(screen, BLACK, (950,50,50,50)) 67 | screen.blit(text_minus, (960,50)) 68 | 69 | # K value 70 | text_k = font.render("K = " + str(K), True, BLACK) 71 | screen.blit(text_k, (1050,50)) 72 | 73 | # run button 74 | pygame.draw.rect(screen, BLACK, (850,150,150,50)) 75 | screen.blit(text_run, (900,150)) 76 | 77 | # random button 78 | pygame.draw.rect(screen, BLACK, (850,250,150,50)) 79 | screen.blit(text_random, (850,250)) 80 | 81 | # Reset button 82 | pygame.draw.rect(screen, BLACK, (850,550,150,50)) 83 | screen.blit(text_reset, (850,550)) 84 | 85 | # Algorithm button 86 | pygame.draw.rect(screen, BLACK, (850,450,150,50)) 87 | screen.blit(text_algorithm, (850,450)) 88 | 89 | # Draw mouse position when mouse is in panel 90 | if 50 < mouse_x < 750 and 50 < mouse_y < 550: 91 | text_mouse = font_small.render("(" + str(mouse_x - 50) + "," + str(mouse_y - 50) + ")",True, BLACK) 92 | screen.blit(text_mouse, (mouse_x + 10, mouse_y)) 93 | 94 | # End draw interface 95 | 96 | for event in pygame.event.get(): 97 | if event.type == pygame.QUIT: 98 | running = False 99 | if event.type == pygame.MOUSEBUTTONDOWN: 100 | # Create point on panel 101 | if 50 < mouse_x < 750 and 50 < mouse_y < 550: 102 | labels = [] 103 | point = [mouse_x -50, mouse_y-50] 104 | points.append(point) 105 | print(points) 106 | 107 | # Change K button + 108 | if 850 < mouse_x < 900 and 50 < mouse_y < 100: 109 | if K < 8: 110 | K = K+1 111 | print("Press K +") 112 | 113 | # Change K button - 114 | if 950 < mouse_x < 1000 and 50 < mouse_y < 100: 115 | if K > 0: 116 | K -= 1 117 | print("Press K -") 118 | 119 | # Run button 120 | if 850 < mouse_x < 1000 and 150 < mouse_y < 200: 121 | labels = [] 122 | 123 | if clusters == []: 124 | continue 125 | 126 | # Assign points to closet clusters 127 | for p in points: 128 | distances_to_cluster = [] 129 | for c in clusters: 130 | dis = distance(p,c) 131 | distances_to_cluster.append(dis) 132 | 133 | min_distance = min(distances_to_cluster) 134 | label = distances_to_cluster.index(min_distance) 135 | labels.append(label) 136 | 137 | # Update clusters 138 | for i in range(K): 139 | sum_x = 0 140 | sum_y = 0 141 | count = 0 142 | for j in range(len(points)): 143 | if labels[j] == i: 144 | sum_x += points[j][0] 145 | sum_y += points[j][1] 146 | count += 1 147 | 148 | if count != 0: 149 | new_cluster_x = sum_x/count 150 | new_cluster_y = sum_y/count 151 | clusters[i] = [new_cluster_x, new_cluster_y] 152 | 153 | print("run pressed") 154 | 155 | # Random button 156 | if 850 < mouse_x < 1000 and 250 < mouse_y < 300: 157 | labels = [] 158 | clusters = [] 159 | for i in range(K): 160 | random_point = [randint(0,700), randint(0,500)] 161 | clusters.append(random_point) 162 | print("random pressed") 163 | 164 | # Reset button 165 | if 850 < mouse_x < 1000 and 550 < mouse_y < 600: 166 | K = 0 167 | error = 0 168 | points = [] 169 | clusters = [] 170 | labels = [] 171 | print("reset button pressed") 172 | 173 | # Algorithm 174 | if 850 < mouse_x < 1000 and 450 < mouse_y < 500: 175 | try: 176 | kmeans = KMeans(n_clusters=K).fit(points) 177 | labels = kmeans.predict(points) 178 | clusters = kmeans.cluster_centers_ 179 | iterations = kmeans.n_iter_ 180 | print("iterations = ", iterations) 181 | except: 182 | print("error") 183 | print("Algorithm button pressed") 184 | 185 | # Draw cluster 186 | for i in range(len(clusters)): 187 | pygame.draw.circle(screen,COLORS[i], (int(clusters[i][0]) + 50, int(clusters[i][1]) + 50), 10) 188 | 189 | # Draw point 190 | for i in range(len(points)): 191 | pygame.draw.circle(screen,BLACK, (points[i][0] + 50, points[i][1] + 50), 6) 192 | 193 | if labels == []: 194 | pygame.draw.circle(screen,WHITE, (points[i][0] + 50, points[i][1] + 50), 5) 195 | else: 196 | pygame.draw.circle(screen,COLORS[labels[i]], (points[i][0] + 50, points[i][1] + 50), 5) 197 | 198 | # Calculate and draw error 199 | error = 0 200 | if clusters != [] and labels != []: 201 | for i in range(len(points)): 202 | error += distance(points[i], clusters[labels[i]]) 203 | 204 | text_error = font.render("Error = " + str(int(error)), True, BLACK) 205 | screen.blit(text_error, (850,350)) 206 | 207 | pygame.display.flip() 208 | 209 | pygame.quit() 210 | -------------------------------------------------------------------------------- /KMean/test.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def nothing(x): 5 | pass 6 | # Create a black image, a window 7 | img = np.zeros((300,512,3), np.uint8) 8 | cv2.namedWindow('image') 9 | # create trackbars for color change 10 | cv2.createTrackbar('R','image',0,255,nothing) 11 | cv2.createTrackbar('G','image',0,255,nothing) 12 | cv2.createTrackbar('B','image',0,255,nothing) 13 | # create switch for ON/OFF functionality 14 | switch = '0 : OFF \n1 : ON' 15 | cv2.createTrackbar(switch, 'image',0,1,nothing) 16 | while(1): 17 | cv2.imshow('image',img) 18 | k = cv2.waitKey(1) & 0xFF 19 | if k == 27: 20 | break 21 | # get current positions of four trackbars 22 | r = cv2.getTrackbarPos('R','image') 23 | g = cv2.getTrackbarPos('G','image') 24 | b = cv2.getTrackbarPos('B','image') 25 | s = cv2.getTrackbarPos(switch,'image') 26 | if s == 0: 27 | img[:] = 0 28 | else: 29 | img[:] = [b,g,r] 30 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algo 2 | Try to code some algorithms 3 | # 1. Bisection method (áp dụng trong trường hợp pt chỉ có 1 nghiệm nằm trong khoảng phân ly cho trước) 4 | Áp dụng đối với f(x) = $x^x-10$ 5 | - Lưu đồ: 6 | ![Diagram](https://github.com/huyvu15/Algo/blob/main/Bisection_Dia.png) 7 | Link Diagram: https://cacoo.com/diagrams/vp0zryKkYwpJ7JwO/B89E2 8 | - Result: 9 | ![result_1](https://github.com/huyvu15/Algo/blob/main/Bisection.png) 10 | 11 | # 2. Iteration_method: 12 | - Nhược điểm của phương pháp này là mình phải tìm hàm $\varphi$ thỏa mãn yêu cầu bài toán 13 | - Sai số $10^{-7}$ 14 | Ta có: 15 | $x^3+x-1000=0$ 16 | $\Leftrightarrow$ $x = \sqrt[3]{1000-x}$ 17 | - Đặt $\varphi (x)$ = $\sqrt[3]{1000-x}$ 18 | 19 | $| \varphi ' (x) |$ = $\displaystyle|-\dfrac{1}{3}$ $\dfrac{1}{(\sqrt{1000-x})^3}|$ < $\dfrac{1}{3.990^{\frac{2}{3}}}$ < 1 20 | $\Rightarrow$ q = $\dfrac{1}{3.990^{\dfrac{2}{3}}}$ 21 | $\Rightarrow$ Khi đó phương pháp lặp đơn hội tụ với pt đã cho $X_0 = 10 $, Xây dựng được dãy gần đúng theo công thức: 22 | 23 | $X_{n+1}$ = $\sqrt[3]{1000-x}$ 24 | - Kết quả chạy thuật toán: 25 | ![result_Iteration_method](https://github.com/huyvu15/Algo/blob/main/result_Iteration_method.png) 26 | Link Diagram: https://cacoo.com/diagrams/vp0zryKkYwpJ7JwO-16A4E.png 27 | 28 | # 3. KMean 29 | 30 | # 4. Bipartiteness Graph 31 | -------------------------------------------------------------------------------- /input_xuongthang.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 1 6 6 10 -3 3 | -9 1 -10 5 5 4 | 1 3 65 8 2 5 | -10 6 -10 1 2 6 | 17 -8 4 3 -6 7 | -------------------------------------------------------------------------------- /result_Iteration_method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyvu15/Algo/d183dbe7255651eb111f98cb96618596fcf35e3f/result_Iteration_method.png --------------------------------------------------------------------------------