Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
numer-60-2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kittisak Maneewong
numer-60-2
Commits
5290c59f
Commit
5290c59f
authored
Apr 05, 2018
by
Kittisak Maneewong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add sheet
parent
7447800f
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
2885 additions
and
0 deletions
+2885
-0
.gitignore
sheet/numerical-method/.gitignore
+104
-0
Chapter 01 Introduction to Python.ipynb
...ical-method/01-02/Chapter 01 Introduction to Python.ipynb
+601
-0
Chapter 02 Numpy and Matplotlib.ipynb
...erical-method/01-02/Chapter 02 Numpy and Matplotlib.ipynb
+0
-0
Chapter 03 Numerics and Error Analysis.ipynb
...method/01-02/Chapter 03 Numerics and Error Analysis.ipynb
+546
-0
03-04 System of Linear Equation.ipynb
...erical-method/03-04/03-04 System of Linear Equation.ipynb
+653
-0
readme.md
sheet/numerical-method/03-04/readme.md
+0
-0
Numerical-Differentiation-1.ipynb
.../numerical-method/09-10/Numerical-Differentiation-1.ipynb
+575
-0
Numerical-Differentiation-2.ipynb
.../numerical-method/09-10/Numerical-Differentiation-2.ipynb
+397
-0
README.md
sheet/numerical-method/README.md
+9
-0
No files found.
sheet/numerical-method/.gitignore
0 → 100644
View file @
5290c59f
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
sheet/numerical-method/01-02/Chapter 01 Introduction to Python.ipynb
0 → 100644
View file @
5290c59f
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Python\n",
"### สำหรับหลักการเชิงตัวเลข ###\n",
"\n",
"* Open-Source Object-Oriented interpret since 1980s\n",
"* รองรับหลายระบบปฏิบัติการ Linux, Windows, OSX, FreeBSD, etc. \n",
"* เขียนง่าย อ่านง่าย\n",
"* มี package เสริมมากมาย สำหรับหลากหลายด้าน\n",
"* Apps, games, graphics, data analysis, Big data, machine learning, etc. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Downloads\n",
"\n",
"* [Anaconda](https://www.continuum.io/downloads) python3 + numpy + matplotlib + jupyter + ...\n",
"\n",
" * https://repo.continuum.io/archive/Anaconda3-4.2.0-MacOSX-x86_64.pkg\n",
" * https://repo.continuum.io/archive/Anaconda3-4.2.0-Windows-x86_64.exe\n",
" * https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh\n",
"\n",
"* Linux\n",
" * installation\n",
" ```sh\n",
" bash Anaconda3-4.2.0-Linux-x86_64.sh\n",
" ``` \n",
" * start\n",
" ```sh\n",
" mkdir NumericalMethod/\n",
" cd NumericalMethod/\n",
" source ~/anaconda3/bin/activate ~/anaconda3\n",
" ```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Getting Start\n",
"```sh\n",
"$ ipython3\n",
"```\n",
"\n",
"```python\n",
"# comment\n",
"name = input(\"What's your name? \")\n",
"print(\"Hello, \"+name)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"name? วิชิต สมบัติ\n"
]
}
],
"source": [
"name = input(\"name? \")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5922444"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"name = \"5922444\"\n",
"int(name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ประเภทของข้อมูล\n",
"\n",
"* ข้อความ - `str`\n",
"```python\n",
"'Conor McGregor'\n",
"\"Cody Garbrandt\"\n",
"\"\"\" หลายๆ บรรทัด \n",
"บรรทัดต่อมา\n",
"บรรทัดสุดท้าย \"\"\"\n",
"```\n",
"* ตัวเลข - `int`, `float`, `complex`\n",
"```python\n",
"20\n",
"int('12')\n",
"30.0\n",
"float('3.54')\n",
"complex(10, 1)\n",
"```\n",
"* List - `list`, `tuple`\n",
"```python\n",
"[1, 3, 4, 5, 7]\n",
"(2, 1, 4)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# ประเภทของข้อมูล\n",
"\n",
"* ประพจน์ - bool\n",
"```python\n",
"True\n",
"False\n",
"```\n",
"* Dictionary - `dict`\n",
"```python\n",
"{\n",
" 'key1': value,\n",
" 'name': 'Jack',\n",
" 'age': 25\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ตัวแปร\n",
"** identifier / name **\n",
"> สายอักขระที่ประกอบด้วย a-z, A-Z, 0-9, _, แต่ตัวแรกต้องไม่เป็น 0-9 \n",
"```python\n",
"i = 20\n",
"x = 3.0\n",
"name = 'Conor A. McGregor'\n",
"u = [ 2.0, 3.5, 4.0, 5.0 ]\n",
"t = ( 1.0, 2.0, 3.0 )\n",
"kv = { 1: 1.0, 2: 4.0, 3: 27.0, 4: 256.0 }\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"i = 20\n",
"x = 3.0\n",
"name = 'Conor A. McGregor'\n",
"u = [ 2.0, 3.5, 4.0, 5.0 ]\n",
"t = ( 1.0, 2.0, 3.0 )\n",
"kv = { 1: 1.0, 2: 4.0, 3: 27.0, 4: 256.0 }"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i.real"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ตัวดำเนินการทางคณิตศาสตร์\n",
"\n",
"```python\n",
"x = 5 + 9 # บวก\n",
"y = 9 - 4 # ลบ\n",
"z = x * y # คูณ\n",
"z = 9 / 2 # หาร\n",
"z = x ** 2.5 # ยกกำลัง\n",
"z = 100 % y # mod (เศษที่ได้จากการหาร)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10//4"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Augmented assignment operators\n",
"\n",
"```python\n",
"x += 5 # x = x + 5\n",
"y -= 9 # y = y - 9\n",
"z *= x # z = z * x\n",
"z /= 9 # z = z / 9\n",
"z **= x # z = z ** x \n",
"z %= y # z = z % y\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# เครื่องหมายเปรียบเทียบ\n",
"| เครื่องหมาย | ความหมาย |\n",
"|:---------:|-------------------|\n",
"| < | น้อยกว่า |\n",
"| > | มากกว่า |\n",
"| <= | น้อยกว่าหรือเท่ากับ |\n",
"| >= | มากกว่าหรือเท่ากับ |\n",
"| == | เท่ากับ |\n",
"| != | ไม่เท่ากับ |\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# เครื่องหมายเปรียบเทียบ\n",
"* Logical\n",
"```python\n",
"2 < x and x < 10 # &&\n",
"2 < x or x > 10 # ||\n",
"2 < x < 10\n",
"```\n",
"\n",
"* เพิ่มเติม\n",
"```python\n",
"a = [2,4,6,8]\n",
"```\n",
"\n",
"```python\n",
"2 in a\n",
"1 not in a\n",
"len(a) is 4\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [2,4,6,8]\n",
"len(a) is 6"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Control Structure\n",
"## `if`\n",
"```python\n",
"if len(a) > 0:\n",
" a[0] = 9\n",
" a[1] *= a[0]\n",
"```\n",
"## `if-elif`\n",
"```python\n",
"if 1 in a:\n",
" a[0] = 9\n",
"elif 2 in a:\n",
" a[1] = 2\n",
"else:\n",
" a[2] = 3 \n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# `If-else`\n",
"* regular `if-else`\n",
"```python\n",
"if 1 in a:\n",
" print('a contains 1.')\n",
"else:\n",
" print(\"a doesn't contains 1\")\n",
"```\n",
"* one-liner\n",
"```python\n",
"print('a contains 1.' if 1 in a else \"a doesn't contains 1.\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# Note on block\n",
"python บังคับให้จัด block ให้เป็นระเบียบ โดยใช้ tab\n",
"\n",
"* java\n",
"```java\n",
"if (objList.contains(obj)) {\n",
" objList.remove(obj);\n",
" obj.consume();\n",
"}\n",
"```\n",
"* python\n",
"```python\n",
"if obj in objList: \n",
"```\n",
"> objList.remove(obj)\n",
"> \n",
"> obj.consume()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Control Structure\n",
"## `while`\n",
"```python\n",
"while เงื่อนไข:\n",
" statements\n",
"```\n",
"* ตัวอย่าง\n",
"```python\n",
"x = 1\n",
"y = 10\n",
"while x != y:\n",
" x += 1\n",
" y -= 1\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Control Structure\n",
"## `for`\n",
"```python\n",
"a = [1, 2, 3, 4, 5, 6]\n",
"for x in a:\n",
" print(x**2)\n",
"\n",
"for i in range(10):\n",
" print(i)\n",
" \n",
"for i in range(1,10,2):\n",
" print(i)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Utility Functions\n",
"```python\n",
"abs(-10.0) # ค่าสัมบูรณ์\n",
"max( [1,3,2,4,5] ) # ค่าสูงสุดใน list\n",
"min( [2,3,9,7,10,12] ) # ค่าต่ำสุดใน list\n",
"round(12345.12345, 3) # ปรับให้เหลือ 3 ตำแหน่ง\n",
"cmp(a, b) # a<b = -1, a==b = 0, a>b == 1\n",
"eval('20.932') # แปลงข้อความเป็นตัวเลข\n",
"type(a) # ดูชนิดข้อมูลของตัวแปร a\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# IO Functions\n",
"* เปิดไฟล์เพื่ออ่านข้อความในไฟล์\n",
"```python\n",
"with open('file.txt', 'r') as f:\n",
" lines = f.readlines()\n",
" print(lines)\n",
"```\n",
"* เปิดไฟล์เพื่อเขียนข้อความ\n",
"```python\n",
"with open('file.txt', 'w') as f:\n",
" f.write('text in line one\\n')\n",
" f.writelines(['text in line two\\n', 'line 3\\n', 'line 4\\n'])\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Function\n",
"\n",
"* defining function\n",
"```python\n",
"def timestable(x):\n",
" for i in range(1,13):\n",
" print('{}x{}={}'.format(i,x,i*x))\n",
"```\n",
"\n",
"* calling function\n",
"```python\n",
"timestable(9)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Modules\n",
"* regular import\n",
"```python\n",
"import math\n",
"x = math.pi\n",
"math.cos(x)\n",
"```\n",
"* function import\n",
"```python\n",
"from math import pi, cos\n",
"cos(pi)\n",
"```\n",
"* rename import\n",
"```python\n",
"import math as m\n",
"m.cos(m.pi)\n",
"```"
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
sheet/numerical-method/01-02/Chapter 02 Numpy and Matplotlib.ipynb
0 → 100644
View file @
5290c59f
This source diff could not be displayed because it is too large. You can
view the blob
instead.
sheet/numerical-method/01-02/Chapter 03 Numerics and Error Analysis.ipynb
0 → 100644
View file @
5290c59f
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# บทที่ 1 ตัวเลขและการวิเคราะห์ความคลาดเคลื่อน\n",
"\n",
"> Mathematically correct programming bug is **hard** to spot.\n",
"\n",
"> สมการถูกแต่...โปรแกรมผิด\n",
"\n",
">> Paulgramming - 2017\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ตัวเลขและความคลาดเคลื่อนในโปรแกรม\n",
"## C++\n",
"\n",
"```c++\n",
"#include <iostream>\n",
"using namespace std;\n",
"main() {\n",
" float x = 1.0; \n",
" float y = x / 3.0;\n",
" cout << ((x == y * 3.0)?\"\": \"Not\") << \" Equal\" << endl;\n",
"}\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# ตัวเลขและความคลาดเคลื่อนในโปรแกรม\n",
"## Java\n",
"\n",
"```java\n",
"public class Test {\n",
" public static void main(String[] a) {\n",
" float x = 1.0;\n",
" float y = x / 3.0;\n",
" System.out.println( ((x == y * 3.0)?\"\": \"Not\") + \" Equal\" );\n",
" }\n",
"}\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# ตัวเลขและความคลาดเคลื่อนในโปรแกรม\n",
"## Python\n",
"\n",
"```python\n",
"x = 1.0\n",
"y = x / 3.0\n",
"print(\"Equal\" if x == y*3.0 else \"Not Equal\")\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# มีหลายตัวเลขที่ไม่\n",
"x = 1.0\n",
"y = x / 3.0\n",
"#print(\"Equal\" if x == y*3.0 else \"Not Equal\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# มีหลายตัวเลขที่คอมเก็บค่าไ่ม่ได้\n",
"\n",
"* ตัวเลขที่คอมพิวเตอร์ต้องประมาณการ (Inexact numbers)\n",
" * ค่าอตรรกยะ (irrational numbers) $\\pi, \\e, ...$\n",
" * ค่าตรรกยะ (rational numbers) ที่ไม่สามารถแปลงเป็นเลขฐานสองได้\n",
"```python\n",
"0.1 + 0.2 == 0.3\n",
"```\n",
"\n",
"* ตัวเลขที่คอมพิวเตอร์สามารถเก็บค่าได้ตรง (Exact numbers)\n",
"> ตัวเลขที่สามารถแปลงให้เป็นเลขฐานสองได้"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Floating-Point Binary\n",
"\n",
"> ตัวเลข(number) เขียนแทนด้วยสายของเลขเดี่ยว(digit) โดยจำนวนของเลขเดี่ยว ขึ้นอยู่กับ เลขฐาน(base)\n",
"\n",
"ในระบบจำนวนนับของคนเราจะเป็นเลขฐาน 10 โดยมี digit ที่ใช้ได้ได้แก่ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n",
"ยกตัวอย่างตัวเลขในเลขฐาน 10 เช่น 128 77 12100 เป็นต้น\n",
"\n",
"ตัวเลขที่ใช้ในคอมพิวเตอร์จะเป็นเลขฐาน 2 โดยมี digit ที่ใช้ได้ ได้แก่ 0 และ 1 \n",
"ยกตัวอย่างตัวเลขในเลขฐาน 2 เช่น 1, 101, 1101 เป็นต้น"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Terminology\n",
"** Bit **\n",
": 0 or 1\n",
"\n",
"** Byte ** \n",
": 8 bits\n",
"\n",
"** Real **\n",
": 4 bytes **single precision**\n",
": 8 bytes **double precision**\n",
"\n",
"** Integer **\n",
": 1, 2, 4, or 8 byte signed\n",
": 1, 2, 4, or 8 byte unsigned\n",
"\n",
"> ขึ้นอยู่กับภาษาโปรแกรมว่าจะใช้สายที่มีความยาวเท่าไหร่ 8, 16, 32 หรือ 64 เป็นต้น\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ข้อตกลง\n",
"\n",
"* เพื่อง่ายต่อการคำนวณเราจะใช้สายที่มีความยาว 8 และเขียนแยกเป็นกลุ่มละ 4 เพื่อง่ายต่อการคำนวน \n",
"\n",
"> เช่น 1 = 0000 0001\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Binary $\\rightarrow$ Integer\n",
"\n",
"| | | | | | | | |\n",
"|------|------|------|------|------|------|------|------|\n",
"| 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 |\n",
"|$2^7$ |$2^6$ |$2^5$ |$2^4$ |$2^3$ |$2^2$ |$2^1$ |$2^0$ |\n",
"\n",
"$1\\times2^7 + 1\\times2^6 + 1\\times2^5 + 0\\times2^4 + 0\\times2^3 + 1\\times2^2 + 1\\times2^1 + 1\\times2^0$\n",
"\n",
"$1\\times 128+ 1\\times 64 + 1\\times 32 + 0\\times 16 + 0\\times 8 + 1\\times 4 + 1\\times 2 + 1 \\times 1$\n",
"\n",
"\n",
"$128 + 64 + 32 + 0 + 0 + 4 + 2 + 1$\n",
"\n",
"**Answer**\n",
"> $231$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# เขียนโปรแกรมทดสอบ\n",
"\n",
"* คำนวณหาผลลัพธ์\n",
"```python\n",
"1*2**7 + 1*2**6 + 1*2**5 + 0*2**4 + 0*2**3 + 1*2**2 + 1*2**1 + 1*2**0\n",
"```\n",
"\n",
"* คำสั่งแปลงเลขฐานต่างๆ\n",
"```python\n",
"int('11100111', 2) # แปลง str '11100111' ฐาน 2 เป็น int\n",
"oct(35) # แปลง 35 ฐานสิบ ให้เป็นฐาน 8\n",
"hex(35) # แปลง 35 ฐานสิบ ให้เป็นฐาน 16\n",
"bin(35) # แปลง 35 ฐานสิบ ให้เป็นฐาน 2\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"#int('11100111', 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Exercise\n",
"จงแสดงการแปลงตัวเลขฐานสองต่อไปนี้เป็นจำนวนเต็ม\n",
"* 10011001\n",
"* 00010011\n",
"* 00011111"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Binary $\\rightarrow$ Float\n",
"กำหนดตำแหน่งที่เลขสองกำลังเป็นศูนย์ (fix-point representation)\n",
"\n",
"| | | | | | | | |\n",
"|------|------|------|------|------|------|------|------|\n",
"| 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 |\n",
"|$2^5$ |$2^4$ |$2^3$ |$2^2$ |$2^1$ |$2^0$ |$2^-1$ |$2^-2$ |\n",
"\n",
"$1\\times 2^5+1\\times 2^4+1\\times 2^3+0\\times 2^2+9\\times 2^1+1\\times 2^0+1\\times 2^{-1}+1\\times 2^{-2}$\n",
"\n",
"$1\\times2^5 + 1\\times2^4 + 1\\times2^3 +0\\times 2^2+0\\times2^1+1\\times 1+\\frac{1}{2}+\\frac{1}{4}$\n",
"\n",
"$32 + 16 + 8 + 0 + 0 + 1 + 0.5 + 0.25$\n",
"\n",
"**Answer**\n",
"> 57.75"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"#1*2**5 + 1*2**4 + 1*2**3 + 0*2**2 + 0*2**1 + 1*2**0 + 1*2**(-1) + 1*2**(-2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Binary Fixed-Point Arithmetic\n",
"| | | | | | | | | |\n",
"|------|------|------|------|------|------|------|------|------|\n",
"| 1 | 1 | 1 | ... | 0 | 0 | ... | 1 | 1 |\n",
"| sign ||$2^{m-1}$ |$2^{m-2}$ | ... |$2^0$ |$2^{-1}$ | ... |$2^{-n+1}$ |$2^{-n}$ |\n",
"\n",
"* Parameters: $m, n \\in Z$\n",
"* $m$ ความยาวของสาย(จำนวน bit) ที่ใช้เป็นจำนวนเต็ม(integer portion)\n",
"* $n$ ความยาวของสาย(จำนวน bit) ที่ใช้เป็นตัวหาร(fractional portion)\n",
"* ความยาวทั้งหมดของสายเป็น $m+n+1$ รวมกับ bit ที่ใช้บอกเครื่องหมาย (+/-)\n",
"* ความรู้เพิ่มเติม: [Fixed-Point Numbers](https://en.wikibooks.org/wiki/Floating_Point/Fixed-Point_Numbers)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Error in Binary Fixed-Point Arithmetic\n",
"\n",
"** สมมติว่ามีแค่ 2 bit ให้ใช้ ** \n",
"\n",
"> $m = 1, n=1$ โดยที่ไม่คิด sign bit\n",
"\n",
"$0.1 \\times 0.1 = 0.01 \\approx 0.0$ \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Binary Floating-Point Arithmetic\n",
"1. IEEE single precision format\n",
"\n",
"| 0 | 1-8 | 9-31 |\n",
"|:----:|:-------------:|:-----------------:|\n",
"| $s$ | $e$ | $f$ |\n",
"| $0$ | $00001001$ | $0011011...01$ |\n",
"\n",
"$ = (-1)^s \\times 2^{e-127} \\times 1.f $\n",
"\n",
"โดยที่\n",
" * **sign** $s \\in {0, 1}$\n",
" * **biased exponent** $0 \\le e \\le 255$\n",
" * **exponent** $p=e-127$ so $-126 \\le p \\le 127$\n",
" * **significand** $1.f$ "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"4.70197740328915e-38"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"8388607"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#int('20', base=10)\n",
"#int('20', base=8)\n",
"#int('101010101', base=2)\n",
"v = '00000001100000000000000000000011'\n",
"#len(v)\n",
"s = v[0]\n",
"e = v[1:9]\n",
"f = v[9:32]\n",
"#s,len(s),e,len(e),f,len(f)\n",
"#(-1)**int(s)*2**(int(e,base=2)-127)*1.int(f,base=2)\n",
"(-1)**int(s)\n",
"2**(int(e,base=2)-127)\n",
"#1.int(f,base=2)\n",
"int('1'*23, base=2)\n",
"int('111', base=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Binary Floating-Point Arithmetic \n",
"2. IEEE double precision format\n",
"\n",
"| 0 | 1-11 | 12-63 |\n",
"|:----:|:-------------:|:-----------------:|\n",
"| $s$ | $e$ | $f$ |\n",
"| $0$ | $00001000111$ | $0011011...01$ |\n",
"\n",
"$ = (-1)^s \\times 2^{e-1023} \\times 1.f $\n",
"\n",
"โดยที่\n",
" * **sign** $s \\in {0, 1}$\n",
" * **biased exponent** $0 \\le e \\le 2047$\n",
" * **exponent** $p=e-1023$ so $-1022 \\le p \\le 1024$\n",
" * **significand** $x = 1.f$ "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Example \n",
"\n",
"จงเขียนคำสั่งเพื่อหาค่าของตัวเลขตามข้อต่อกำหนดต่อไปนี้ โดยใช้ IEEE single precision format\n",
"> 0 1111 0000 0000 1010 0000 0000 0000 000\n",
" \n",
"**Answer** \n",
"หาค่า $s, e, f$ จากโจทย์\n",
"> $s = 0$\n",
"\n",
"> $e$ = 11110000\n",
"\n",
"> $f$ = 0000101 \n",
"\n",
"```python\n",
"s = 0\n",
"e = int('11110000', 2)\n",
"x = 1 + 0*2**(-1) + 0*2**(-2) + 0*2**(-3) + 0*2**(-4) + 1*2**(-5) + 0*2**(-6) + 1*2**(-7)\n",
"print( (-1)**s * 2**(e-127) * x )\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Exercise \n",
"1. จงเขียนคำสั่งเพื่อหาค่าของตัวเลขตามข้อต่อกำหนดต่อไปนี้ โดยใช้ IEEE single precision format\n",
" * $s = 0$\n",
" * $e$ = 1111 0000 0000\n",
" * $f$ = 0000 1010 \n",
" \n",
"2. จงเขียนคำสั่งเพื่อหาค่าของตัวเลขตามข้อต่อกำหนดต่อไปนี้ โดยใช้ IEEE double precision format\n",
" * $s = 1$\n",
" * $e$ = 0000 0011 0000\n",
" * $f$ = 0110 1010 \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Error Calculation\n",
"\n",
"* **Absolute Error** \n",
" $\\rightarrow E_{abs} = \\| x_0 - x \\|$\n",
"* **Relative Error**\n",
" $\\rightarrow E_{rel} = \\frac { E_{abs} }{x} $\n",
"* **Percentage Error**\n",
" $\\rightarrow E_{per} = E_{rel} \\times 100$"
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
sheet/numerical-method/03-04/03-04 System of Linear Equation.ipynb
0 → 100644
View file @
5290c59f
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ระบบสมการเชิงเส้นเชิงคำนวณ\n",
"# Systems of Linear Algebraic Equations\n",
"> wichit sombat\n",
"\n",
"Solve the equations $Ax = b$\n",
"\n",
"Find solution of $n$ linear, algebraic equations in $n$ variables.\n",
"\n",
"$$\n",
"A_{11} x_1 + A_{12} x_2 + ... + A_{1n}x_n = b_1 \\\\\n",
"A_{11} x_1 + A_{12} x_2 + ... + A_{1n}x_n = b_1 \\\\\n",
"\\vdots \\\\asdfasdfasd\n",
"A_{m1} x_1 + A_{m2} x_2 + ... + A_{mn}x_n = b_n\n",
"$$adfasdfadsfasdf\n",
"adsf\n",
"asdf\n",
"asdf\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Matrix Form\n",
"$$\n",
"{\n",
"\\begin{bmatrix}\n",
"A_{11} & A_{12} & \\cdots & A_{1n} \\\\\n",
"A_{21} & A_{22} & \\cdots & A_{2n} \\\\\n",
"\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
"A_{n1} & A_{n2} & \\cdots & A_{mn}\n",
"\\end{bmatrix}\n",
"}{\n",
"\\begin{bmatrix}\n",
"x_1 \\\\\n",
"x_2 \\\\\n",
"\\vdots \\\\\n",
"x_n\n",
"\\end{bmatrix}\n",
"} = {\n",
"\\begin{bmatrix}\n",
"b_1 \\\\\n",
"b_2 \\\\\n",
"\\vdots \\\\\n",
"b_n\n",
"\\end{bmatrix}\n",
"}\n",
"$$\n",
"Or\n",
"$$\n",
" Ax = b \n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Augmented Coefficient Matrix\n",
"$$\n",
"[A|b] = {\n",
"\\begin{bmatrix}\n",
"A_{11} & A_{12} & \\cdots & A_{1n} & b_1 \\\\\n",
"A_{21} & A_{22} & \\cdots & A_{2n} & b_2 \\\\\n",
"\\vdots & \\vdots & \\ddots & \\vdots & \\vdots \\\\\n",
"A_{n1} & A_{n2} & \\cdots & A_{mn} & b_n\n",
"\\end{bmatrix}\n",
"}\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution\n",
"* if $|A| \\neq 0$, there is a unique solution.\n",
" 1. the coefficient matrix is **nonsingular** \n",
" \n",
" 2. rows & columns are **linearly independent**\n",
"\n",
"asdfasdf\n",
"* if $|A| = 0$, there are eitheradfasdf\n",
" 1. there are infinite numbers of solution. or\n",
" \n",
" 2. there is no solutions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python\n",
"```python\n",
"import numpy.linalg as linalg\n",
"A = np.array( [\n",
" [ 4.0, -2.0, 1.0], \n",
" [-2.0, 4.0, -2.0], \n",
" [ 1.0, -2.0, 3.0] ] )\n",
"b = np.array( [1.0, 4.0, 2.0] )\n",
"linalg.inv(A)\n",
"linalg.det(A)\n",
"linalg.solve(A, b)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear Dependent Equation\n",
"\n",
"* $2x + y = 3$ and $4x + 2y = 6$\n",
"> linear dependent\n",
"\n",
"* $2x + y = 3$ and $4x + 2y = 0$\n",
"> no solution\n",
"> contradiction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"\n",
"1. จงหาเขียน matrix $A$ ของ linear equation system ต่อไปนี้\n",
" * $y = 2x + 1$\n",
" * $5x = 6 + 3y$ \t\n",
" * $y/2 = 3 − x$ \n",
"2. จงเขียนคำตอบของ linear equation system\n",
" * $y = 2x + 1$\n",
" * $2y = 4x + 2$\n",
"3. จงเขียนคำตอบของ linear equation system\n",
" * $x + y = 6$\n",
" * $-3x + y = 2$\n",
"4. จงเขียนคำตอบของ linear equation system\n",
" * $y = 3x - 2$\n",
" * $y = -x - 6$\n",
"5. จงเขียนคำตอบของ linear equation system\n",
" * $y = 2x + 5$\n",
" * $y = -x - 6$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ill Conditioning\n",
"$$\n",
"|A| \\to 0 \n",
"$$\n",
"Or\n",
"$$\n",
"|A| << ||A||\n",
"$$\n",
"where $||A||$ is the **norm** of the matrix.\n",
"\n",
"* Euclidean norm\n",
"$$\n",
"||A||_e = \\sqrt{\\sum_{i=1}^{n}{\\sum_{j=1}^{n}A_{ij}^2}}\n",
"$$\n",
"* Row-sum norm\n",
"$$\n",
"||A||_{\\infty} = \\max_{i \\leq i \\leq n}{\\sum_{j=1}^{n}{|A_{ij}|}}\n",
"$$\n",
"* Matrix Condition Number\n",
"$$\n",
"cond(A) = ||A|| \\times ||A^{-1}||\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example $|A| \\to 0$\n",
"$$\n",
"2x + y =3 \\\\\n",
"2x + 1.001y = 0\n",
"$$\n",
"\n",
"* $|A| = 0.002$\n",
"* solution: ?\n",
"* Change 2nd equation to $2x + 1.002y = 0$\n",
"* solution: ?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Methods of Solution for $Ax = b$\n",
"* Direct method\n",
" * elementary operations\n",
"* Indirect method - numerical method, iterative method\n",
" * start with a guess\n",
" * iteration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Direct Methods\n",
"\n",
"| Method | Initial form | Final form |\n",
"|--------------------------|-----------------------|-----------------------|\n",
"| Gauss elimination | $ Ax = b $ | $ Ux = c $ |\n",
"| LU decomposition | $ Ax = b $ | $ LUx = c $ |\n",
"| Gauss-Jordan elimination | $ Ax = b $ | $ Ix = c $ |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gauss Elimination\n",
"$$\n",
"Ax = b\n",
"$$\n",
"1. Elimination Phase\n",
" * pick pivot equation $(j)$\n",
" * $(i) = (i) - \\lambda (j)$\n",
" * repeat to get $Ux = c$\n",
"$$\n",
"U = \\begin{bmatrix}\n",
"U_{11} & U_{12} & \\cdots & U_{1n} \\\\\n",
"0 & U_{22} & \\cdots & U_{2n} \\\\\n",
"\\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
"0 & 0 & \\cdots & U_{nn}\n",
"\\end{bmatrix}\n",
"$$\n",
"2. Back substitution\n",
"$$\n",
"x_k = (c_k - \\sum_{j=k+1}^{n}U_{kj}x_{j})\\frac{1}{U_{kk}} \\\\\n",
"k = n-1, n-2, ..., 1\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_1 = 1.0\n",
"x_2 = -2.0\n",
"x_3 = 3.0\n"
]
}
],
"source": [
"import numpy.linalg as la\n",
"A = [\n",
" [4, -2, 1],\n",
" [-2, 4, -2],\n",
" [1, -2, 4],\n",
"]\n",
"b = [\n",
" 11,\n",
" -16,\n",
" 17\n",
"]\n",
"result = la.solve(A, b)\n",
"for i,x in zip(range(1,len(result)+1), result):\n",
" print('x_{} = {}'.format(i,x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Elimination\n",
"$$\n",
"4x_1 - 2x_2 + x_3 = 11 \\to (a)\\\\\n",
"-2x_1 + 4x_2 - 2x_3 = -16 \\to (b)\\\\\n",
"x_1 - 2x_2 + 4x_3 = 17 \\to (c)\n",
"$$\n",
"1.1 pivot equation (a)\n",
"\n",
"$\n",
"\\lambda = \\frac{-2}{4} \\\\\n",
"(b) = (b) - \\lambda*(a) \\\\\n",
"(c) = (c) - 0.25(a) \\\\\n",
"$\n",
"\n",
"$$\n",
"4x_1 - 2x_2 + x_3 = 11 \\to (a)\\\\\n",
"3x_2 - 1.5x_3 = -10.5 \\to (b)\\\\\n",
"-1.5x_2 + 3.75x_3 = 14.25 \\to (c)\n",
"$$\n",
"\n",
"1.2 pivot equation (b)\n",
"\n",
"$\n",
"(c) = (c) - (-0.5)(b)\n",
"$\n",
"\n",
"$$\n",
"4x_1 - 2x_2 + x_3 = 11 \\to (a) \\\\\n",
"3x_2 - 1.5x_3 = -10.5 \\to (b) \\\\\n",
"3x_3 = 9 \\to (c)\n",
"$$\n",
"\n",
"1.3 $Ux = c$\n",
"$$\n",
"U = \\begin{bmatrix}\n",
"4 & -2 & 1 \\\\\n",
"0 & 3 & -1.5 \\\\\n",
"0 & 0 & 3\n",
"\\end{bmatrix} \\\\\n",
"C = \\begin{bmatrix}\n",
"11.0 \\\\\n",
"-10.5 \\\\\n",
"9.0\n",
"\\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Back Substitution\n",
"* from $ 3x_3 = 9 \\to (c) $\n",
"$$\n",
"x_3 = \\frac{9}{3} = 3\n",
"$$\n",
"* from $ 3x_2 - 1.5x_3 = -10.5 \\to (b) $\n",
"$$\n",
"x_2 = \\frac{-10.5 + 1.5x_3}{3} = \\frac{-10.5 + 1.5(3)}{3} = -2 \n",
"$$\n",
"* from $4x_1 - 2x_2 + x_3 = 11 \\to (a) $\n",
"$$\n",
"x_1 = \\frac{11 + 2x_2 - x_3}{4} = \\frac{11+2(-2)-3}{4} = 1\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Elimination Phase in Python\n",
"$$\n",
"Ax = b\n",
"$$\n",
"\n",
"$ A_{ij} = A_{ij} - \\lambda A_{kj}, j =k, k+1, ..., n \\\\\n",
" b_i = b_i - \\lambda b_k $\n",
"\n",
"```python\n",
"for k in range(0, n-1):\n",
" for i in range(k+1, n):\n",
" if a[i,k] != 0.0:\n",
" lam = a[i,k]/a[k,k]\n",
" a[i,k+1:n] = a[i, k+1:n] - lam*a[k,k+1:n]\n",
" b[i] = b[i] - lam*b[k]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"k = 0\n",
"[ 4. -2. 1.] 11.0\n",
"[ 0. 3. -1.5] -10.5\n",
"[ 0. -1.5 3.75] 14.25\n",
"k = 1\n",
"[ 0. 3. -1.5] -10.5\n",
"[0. 0. 3.] 9.0\n"
]
}
],
"source": [
"import numpy as np\n",
"A = np.array([\n",
" [4, -2, 1],\n",
" [-2, 4, -2],\n",
" [1, -2, 4]\n",
"], float)\n",
"b = np.array([ 11, -16, 17 ],float)\n",
"n = len(b)\n",
"for k in range(0,n-1):\n",
" print('k = ',k)\n",
" print(A[k,:],b[k])\n",
" for i in range(k+1, n):\n",
" lam = A[i,k]/A[k,k]\n",
" A[i,k] = 0\n",
" A[i, k+1:n] = A[i,k+1:n] - lam*A[k,k+1:n]\n",
" b[i] -= lam*b[k]\n",
" print(A[i,:],b[i])\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def ge(A, b):\n",
" # 1. elimination\n",
" n = len(b)\n",
" for j in range(0, n-1): # pivot equation\n",
" #print('------------\\nj = %d'%(j))\n",
" for i in range(j+1, n):\n",
" lam = A[i,j]/A[j,j]\n",
" A[i,j:n] = A[i, j:n] - lam*A[j, j:n]\n",
" b[i] = b[i] - lam*b[j]\n",
" #print(A)\n",
" #print(b)\n",
" #print()\n",
" # 2. back substitution\n",
" x = b.copy()\n",
" for k in range(n-1, -1, -1):\n",
" x[k] = (b[k] - np.dot(A[k,k+1:n], x[k+1:n]))/A[k,k]\n",
" \n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1. -2. 3.]\n"
]
}
],
"source": [
"import numpy as np\n",
"A = np.array([\n",
" [4, -2, 1],\n",
" [-2, 4, -2],\n",
" [1, -2, 4]\n",
" ], float)\n",
"b = np.array([11,-16,17], float)\n",
"ge(A, b)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0. 3. -1.5]\n",
"[0. 0. 3.]\n"
]
}
],
"source": [
"j=0\n",
"i=1\n",
"R = A[i,j:3]-(A[i,j]/A[j,j])*A[j,j:3]\n",
"print(R)\n",
"i=2\n",
"R = A[i,j:3]-(A[i,j]/A[j,j])*A[j,j:3]\n",
"print(R)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Back Substitution in Python\n",
"$$ \n",
"Ux = c \n",
"$$\n",
"\n",
"หลังจาก elimination phase ไปแล้วจะได้ $U = A, c = b$\n",
"$$\n",
"x_k = (c_k - \\sum_{j=k+1}^{n}U_{kj}x_{j})\\frac{1}{U_{kk}} \\\\\n",
"k = n-1, n-2, ..., 1\n",
"$$\n",
"\n",
"จึงเขียนใหม่ได้เป็น\n",
"\n",
"$$\n",
"x_k = (b_k - \\sum_{j=k+1}^{n}A_{kj}x_{j})\\frac{1}{A_{kk}} \\\\\n",
"$$\n",
"\n",
"```python\n",
"for k in range(n-1, -1, -1):\n",
" x[k] = (b[k] - dot(a[k,k+1:n], x[k+1:n]))/a[k,k]\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"จงเขียนโปรแกรมเพื่อหาคำตอบของ $Ax = b$\n",
"\n",
"```$ guass.py A.txt b.txt```\n",
"\n",
"$$\n",
"A = \\begin{bmatrix}\n",
"6 & -4 & 1 \\\\\n",
"-4 & 6 & -4 \\\\\n",
"1 & -4 & 6\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$$\n",
"b_1 = \\begin{bmatrix}\n",
"-14 \\\\\n",
"36 \\\\\n",
"6\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$$\n",
"b_2 = \\begin{bmatrix}\n",
"-14 \\\\\n",
"36 \\\\\n",
"6\n",
"\\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LU Decomposition\n",
"solve $Ax = b \\to LUx = c$\n",
"1. Doolittle's Decomposition $L_{ii} = 1$\n",
"2. Crout's Decomposition $U_{ii} = 1$\n",
"3. Choleski's Decomposition $L = U^T$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Choleski's Decomposition\n",
"\n",
"$$\n",
"A = \\begin{bmatrix}\n",
"4 & -2 & 2 \\\\\n",
"-2 & 2 & -4 \\\\\n",
"2 & -4 & 11 \n",
"\\end{bmatrix} \\\\\n",
"$$\n",
"\n",
"\n",
"```python\n",
"A = np.array([ \n",
"[4, -2, 2],\n",
"[-2, 2, -4],\n",
"[2, -4, 11]\n",
"])\n",
"L = np.linalg.cholesky(A)\n",
"print(L)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"$$\n",
"A = \\begin{bmatrix}\n",
"3 & -1 & 4 \\\\\n",
"-2 & 0 & 5 \\\\\n",
"7 & 2 & -2\n",
"\\end{bmatrix} \\\\\n",
"b_1 = \\begin{bmatrix}\n",
"6 \\\\ 3 \\\\ 7\n",
"\\end{bmatrix} \\\\\n",
"b_2 = \\begin{bmatrix}\n",
"-4 \\\\ 2 \\\\ -5\n",
"\\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"$$\n",
"A = \\begin{bmatrix}\n",
"1.44 & -0.36 & 5.52 & 0.00 \\\\\n",
"-0.36 & 10.33 & -7.78 & 0.00 \\\\\n",
"5.52 & -7.78 & 28.40 & 9.00 \\\\\n",
"0.00 & 0.00 & 9.00 & 61.00\n",
"\\end{bmatrix} \\\\\n",
"b = \\begin{bmatrix}\n",
"0.04 \\\\ -2.15 \\\\ 0 \\\\ 0.88\n",
"\\end{bmatrix}\n",
"$$"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
sheet/numerical-method/03-04/readme.md
0 → 100644
View file @
5290c59f
sheet/numerical-method/09-10/Numerical-Differentiation-1.ipynb
0 → 100644
View file @
5290c59f
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numerical Differentiation\n",
"การหาผลต่าง \n",
"\n",
"> กำหนดฟังก์ชัน $f(x)$ จงคำนวณหา $\\frac{d^nf}{dx^n}$ เมื่อกำหนดค่า $x$ ให้\n",
"\n",
"โดยที่ $f(x)$ กำหนดได้สองแบบคือ\n",
"\n",
"* กำหนดเป็นฟังก์ชันสำหรับ การคำนวณหาโดยตรง $y = f(x)$ \n",
"* กำหนดความสัมพันธ์เป็นแบบชุดข้อมูลมาให้ $(x_i,y_i), i=0,1,..,n$\n",
"\n",
"## Taylor Series of $f(x)$ at $a$ is\n",
"$$\n",
"\\sum_{n=0}^{\\infty}\\frac{f^n(a)}{n!}(x-a)^n\n",
"$$\n",
"\n",
"ตัวอย่าง Taylor series สำหรับ $e^x$ ที่ $a = 0$ คือ\n",
"$$\n",
"\\frac{x^0}{0!} + \\frac{x^1}{1!} + \\frac{x^2}{2!} + \\frac{x^3}{3!} + ... \\\\\n",
"1 + x + \\frac{x^2}{2!} + \\frac{x^3}{3!} + ... \\\\\n",
"\\sum_{n=0}^{\\infty}\\frac{x^n}{n!}\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Backward/Forward Taylor Series on $f(x)$\n",
"$$\n",
"f(x+h) = f(x) + hf^1(x) + \\frac{h^2}{2!}f^2(x) + \\frac{h^3}{3!}f^3(x) + \\frac{h^4}{4!}f^4(x) + ... \\to (a) \\\\\n",
"f(x-h) = f(x) - hf^1(x) + \\frac{h^2}{2!}f^2(x) - \\frac{h^3}{3!}f^3(x) + \\frac{h^4}{4!}f^4(x) - ... \\to (b) \\\\\n",
"f(x+2h) = f(x) + 2hf^1(x) + \\frac{(2h)^2}{2!}f^2(x) + \\frac{(2h)^3}{3!}f^3(x) + \\frac{(2h)^4}{4!}f^4(x) + ... \\to (c) \\\\\n",
"f(x-2h) = f(x) - 2hf^1(x) + \\frac{(2h)^2}{2!}f^2(x) - \\frac{(2h)^3}{3!}f^3(x) + \\frac{(2h)^4}{4!}f^4(x) - ... \\to (d) \\\\\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ผลต่างของสมการ $(a), (b), (c), (d)$\n",
"$ (a) + (b) $\n",
"$$\n",
"f(x+h)+f(x-h) = 2f(x) + h^2f^2(x) + \\frac{h^4}{12}f^4(x) + ... \\to (e) \\\\\n",
"$$\n",
"$ (a) - (b) $\n",
"$$\n",
"f(x+h)-f(x-h) = 2hf^1(x) + \\frac{h^3}{3}f^3(x) + ... \\to (f) \\\\\n",
"$$\n",
"$ (c) + (d) $\n",
"$$\n",
"f(x+2h)+f(x-2h) = 2f(x) + 4h^2f^2(x) + \\frac{4h^4}{3}f^4(x) + ... \\to (g) \\\\\n",
"$$\n",
"$ (c) - (d) $\n",
"$$\n",
"f(x+2h)-f(x-2h) = 4hf^1(x) + \\frac{8h^3}{3}f^3(x) + ... \\to (h) \\\\\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finite Difference Approximations\n",
"\n",
"### Central Difference Approximations: CDA\n",
"\n",
"#### First CDA\n",
"$$\n",
"f^1(x) = \\frac{f(x+h) - f(x-h)}{2h} - \\frac{h^2}{3!}f^3(x) - \\frac{h^4}{5!}f^5(x) - \\frac{h^6}{7!}f^7(x) -...\n",
"$$\n",
"หรือ\n",
"$$\n",
"f'(x) = \\frac{f(x+h)-f(x-h)}{2h} + O(h^2)\n",
"$$\n",
"สูตร\n",
"$$\n",
"f'(x) \\approx \\frac{f(x+h)-f(x-h)}{2h} \\to (c)\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^1(x)$, (First CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า First CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## solution $f(x) = e^{-x}$\n",
"$$\n",
"f'(x) = \\frac{f(x+h) - f(x-h)}{2h} \\\\\n",
"h = 0.001 \\\\\n",
"f'(x) = \\frac{f(x+0.001) - f(x - 0.001)}{2\\times0.001} \\\\\n",
"f'(x) = \\frac{e^{-(x+0.001)} - e^{-(x-0.001)}}{0.002}\\\\\n",
"x = 1 \\\\\n",
"f'(1) = \\frac{e^{-(1+0.001)} - e^{-(1-0.001)}}{0.002} = \\frac{e^{-1.001} - e^{-0.999}}{0.002} \n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.3678795024846804"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from math import e\n",
"f1x = (e**(-1.001) - e**(-0.999))/0.002\n",
"f1x"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def f1x(x):\n",
" return (e**(-(x+0.001)) - e**(-(x-0.001)))/(2*0.001)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.3678795024846804"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f1x(1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.3678795024846804"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f1x(x, h):\n",
" return (e**(-(x+h)) - e**(-(x-h)))/(2*h)\n",
"f1x(1,0.001)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution ของ $f(x) = sin(x)e^{-cos(x)}$\n",
"$$\n",
"f'(x) \\approx \\frac{f(x+h)-f(x-h)}{2h}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"f'(x) \\approx \\frac{sin(x+h)e^{-cos(x+h)} - sin(x-h)e^{-cos(x-h)}}{2h}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"แทน $x = 1, h = 0.001$\n",
"$$\n",
"f'(x) \\approx \\frac{sin(1+0.001)e^{-cos(1+0.001)} - sin(1-0.001)e^{-cos(1-0.001)}}{2 \\times 0.001}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"f'(x) \\approx \\frac{sin(1.001)e^{-cos(1.001)} - sin(0.999)e^{-cos(0.999)}}{0.002}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7272689097917728"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from math import cos, sin, e\n",
"f1x = (sin(1.001)*e**(-cos(1.001)) - sin(0.999)*e**(-cos(0.999)))/0.002\n",
"f1x"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7272689097917728"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fsin():\n",
" return (sin(1.001)*e**(-cos(1.001)) - sin(0.999)*e**(-cos(0.999)))/0.002\n",
"def fsinxh(x,h):\n",
" return (sin(x+h)*e**(-cos(x+h)) - sin(x-h)*e**(-cos(x-h)))/(2*h)\n",
"fsinxh(1,0.001)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Second CDA\n",
"$$\n",
"f^2(x) = \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2} + O(h^2)\n",
"$$\n",
"สูตร\n",
"$$\n",
"f^2(x) \\approx \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2} \\to (d)\n",
"$$\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example จงหา $f2x(x,h)$ ของ $f(x) = e^{-x}$"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.367879471885324"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f2x(x, h):\n",
" return (e**(-(x+h)) - 2*e**(-x) + e**(-(x-h)))/(h**2)\n",
"f2x(1, 0.001)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise จงหา $f2x(x,h)$ ของ $f(x) = sin(x)e^{-cos(x)}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^2(x)$, (Second CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า Second CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# เสริม เรื่อง การลดรูปฟังก์ชันและการแสดงผล\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import sympy\n",
"sympy.init_printing()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"x,y,z,e = sympy.Symbol('x'), sympy.Symbol('y'), sympy.Symbol('z'), sympy.Symbol('e')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from sympy import sin, cos"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"y = sin(x)*e**(-cos(x))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAIQAAAAaBAMAAACEBumnAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEIl2mSJE3e9UMqtmzbsXyEShAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACWklEQVQ4EY1UPWgUQRT+ds/Z4/72Nl4qg7Ik0UrhvL1GjBiUFIrFgqAiQo40V3oSEKxMGiFocYiSJsVisLC6I8YmCFk7C4PbKGLjyWEnXgJqk8Y3uzNn9pK5zYPd9973vvfNm93ZBdTGwlJWTUiu2CElU0tmqhg5N6rMqAjJ+AtBGUumqhgbolAQ06h4cVwfn4FTsVJTE4Q3AL26uoGUHycNz9awZja1xhyuA4wkzuaC+8i0D2hiQjczMCLbAVpAd/bKCGB0AK9ATB6EdssXAbkTMnwjAuMX2XcY28B7YN3o/aVOvniRFskGgtWyRUDukwyPyyDy7E80xUssU2eHwBa8SCtORH5aAnpZRpFfR8lsssVnMAGtAbZ9BjbydpzEszRNF5m2IKPI65fLGJ90nzonKb8H7e1cxcKRmiDd9kQA3OxH+EyrTVT7kv8LFJ0TWYH71ZULrNfOfrnqvOZpla7S+RsV2sUkUHeZzdF9RpsJbYXuWgdpFNpIdzHLF/xJkJ2fTwfAFrQljHoE7jfDDzFtmpyxiBTMNswFFDn8gI6dpe8YJLeM3BPnHWEH2fMQTFncPey6oUSAYpnSRzQFzA6vtFBsc59oR7/9DiU6fQk6NFyMS9iJ7URgFj7W+EaEBG2Emj1+ryNtA6EaT5VmlJHz90jQ48xYF8E3uYVUgFFX2SoL9DhNl0aQU9CbvNvs4Q7VL9Er/uBLotobp6dOZTd3j23uvuo9JlodGHNK13jnV3XX0MqeA94YSlQX1Z+Zumew8kMCAx+7hA/h+7+cvtYhmuIU+ePLu/gHZIh7l4oKPuIAAAAASUVORK5CYII=\n",
"text/latex": [
"$$e^{- \\cos{\\left (x \\right )}} \\sin{\\left (x \\right )}$$"
],
"text/plain": [
" -cos(x) \n",
"e ⋅sin(x)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWsAAAAaBAMAAACDYWCbAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEIl2mSJE3e9UMqtmzbsXyEShAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFK0lEQVRYCa1XXWgcVRT+ZjY76+5mN0OIJBqqk2zik+ImG180hbES0VBk0AeLFLIoWvAnDUoKCmIEEYoVtw9axaIDRa0PNYulKGLoWKu+VFwEfxCDK8GHgLRBq1DzEM+5987s7swmu5v0wM49v9/5cvfcOxtgC3E4ltoi4aqHREe4O8LVRXWy2CGIft+dHVYE6bIj3ggc21HOyKKpDmtHsdRhRZCuOnbJTQ/cHSkpS6YPdlQFzGNxm239jlq5w5b16WlXWpkOSRzCTLEep33d74gf2q8RmcMjTqwwhMK5PJAA9IkTnyHmdQiCd832K0IdkZu+w8Vk+/WcmbbS1ir6DStOtB8ExtKVQ0iWm4HEPeFNOk2CzwjffhV5vUlG4Ap3jJlLiSLeDuL1yqaAMyZSa8jedvko7dcs4GY8wKjSLbhk1QOQfoOy1SkC9nl+hm6zplX5SZJu9pfJEBDumMKvFKLWUYkCGhdJfgcOAt0LSNuzG0XgCarsoZapCikD5RCOP367fP+i5WvnhRIcCY3rIzLLLc1oR/xDqaci6eTYHHDGEbt9u5OcAz6i1EW6+g1mnA3R7rbJyaLn5Vp7GpbhkiXJszvHD5JkJJV2O9wxtUbvN96xiEQB/ZS0reVXse86G1WgH/G1W2Ch26JwmHbCUTXaglKC5dRb75hkzAeOAaU1ox3qiBm9Qu+cc0FtnRIFDIIjBejDU8ncOLFKmNrnD4+b6CpSOEybzquSH9X6kKuU1zY2SIvPKZO+3aJUm9FGY0cMfp0bAu4OamtKCFDLTfg7V8thLVaSdoYXoq3lxoswPt5bqJI9QR9ZOULaieO745fKqZ+nC6fJEhKzKaFw5hGaEJ1UloB2b6H5m9fviAqn9w3fK3sSOpkhwANO3OKsqFSl6zgvRPsDBys4aCa8PNl/0kdWfisujQQyZSRWeFalZD2gF9/kn6dTLljUaBvzuF9lhZaqtJMerfHDOOlwT7pB6BWCRkDtMPpc8jaRJ4VPs3kh2k/TCXU/QXqO7WeJrKw8Rlfki4hxRnYBPR5HSRIlYAi3mo8SXVFRoz1Q0qZlUvgpO6KP/ZkK3nS55/uMHgZMHyl8Ea5WdtJhJWbyM1tOXaH70LpH0X6Z7mNZuUjRF1YcQbuCnjxnk1xTBExcZJVeBTQpy8s/vbq8XCb10y8nBSbHGkV2xBh7b7SpUvRk9DBgDwO1lmw5+S/Rrs64AyXOJtqqkmn3/nZZ0K420gb+5lxBm1Z/ttfZ2VIW81QhejK6pF0D7LFaAnCC2m37gdyoyKchUZUHaAxNfFfkIamjzUOC1H+cbMyJkoD2X9Js8Qx2e4rR5ZDUABMWQH9XKyFSL9GcFf1f8HQkVSUdSSOPtBeizSfoaGyNv/DwkaSB5WFtIZkKUOKejzG6PJI1wFgFfU4LBAoTqZMuvsdTj78nkunaU5V7xJHMOrTV9but2zCuZNfiHtBliZJgt3dDm5KerZ7xI9Ad7kkHPksM9UbAiQveVtUyljq77sT3jBSR2digHaDLjz6y8heiffPkTZRx/dn1Dy+9Igt4MrTTYxMXyMy40ufPdmwve1vK4F2j4J6MTslNAFtCBAmruPYYz1TC/4a0uSDWqNTexf0q4NNuzGvbigK2XZqs0qjSB902PViiP6Wkv8lPKc1Uoe0t54OyXKC1qWjP0ZiXOPkPVbFrs0qeRyFaZbOUzvw7AezNjcs3nP9vgk8/QkGrKlfQLpLSmeOqAPIVQdLt76mwGh77lfVVg3cHRh3g/621S3gdLheqAAAAAElFTkSuQmCC\n",
"text/latex": [
"$$e^{- \\cos{\\left (x \\right )}} \\log{\\left (e \\right )} \\sin^{2}{\\left (x \\right )} + e^{- \\cos{\\left (x \\right )}} \\cos{\\left (x \\right )}$$"
],
"text/plain": [
" -cos(x) 2 -cos(x) \n",
"e ⋅log(e)⋅sin (x) + e ⋅cos(x)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"z = y.diff(x)\n",
"z"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAAUBAMAAACOiQyaAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAImYQu82Z3XZU70SJMqtHo0JKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAExklEQVRIDdVXTWgcZRh+srO/2dnNelEoQqd6KaWYOZhGgrhbFRFBuzYnEXWU2kMTZMUfKEi6Ci05VDNtwZOQaPRQi7Ag0uAlS6khwiZZ6MFLcNceAi1K89O1pqld35+Z7Nj2MITm0Jfw5sk878/zzjfffBOgy8IDbqdY/5OA+dkco8zIcK2GfYdeDiBg9mQdxuhMRR2eOjnkJzADc3QOXbWLrtJaQmK4YtBCNhmez1HPvVR5X+0PXwghtd+WarVqR6eqyVIGXgVeQz+jSLvdvm00sVDpIPRV4kXEgSvq4CDraoIwxut4Fw8jtaq0JGqM13jzV7gmA1bXJGKu8T6Mb7DDViGMgNQk0EsSmwF1osYoknYHkQmYhBCnWZpxG90THYQXEC3gNNCnLmXBKGqCMFEHz2PMxQ2lJVFiNuV7IGSTq8AS9gC/IDuBrCP1BOGZ+TXgbSAb0Ol1mgUSOUSbSFEITPqpd5eQXemg9ArLuAj0qzMLMEqSoMw0jY6fK2h5NJeQGE4LWsgmLWABrwDjdk8R5rIIEUQ3nDRWgbmgTlYDDACPAD1NpNe15RRia8j+S394KDrBxPRzuKAufd02LUlQ5iWmacFXlSY4BYmRywEXrolxHRjL3aaObr6AJN/DKXiIxwDSZfaeOq9TrIITQN5B+h8mgQK7BMd7qOeNWdrS2fZeVx2urf6qCcq0Fk9UKLq/6tGSyDF3Wsgm39Fq/Pg3jVHuptXYoCoFeEjHiErhAnvSqZ1MB88CO6vI6BimxfR0lR4ND+UPIuFS6ZbtObM9qAnCGC0LR4FHP/ZpSeSYOy1kkwXgrV23gEbZXEZsVYR4SMe4wIV9ddBOkSKL2OkgQ5lkefEjAZRfRnwSqaFj59Thy/M3K5IgjNEGjtAISZ+WEhwjlQIuZJNoObVwilajUcWL+GpdJSmSMYxlrrmpUzulSjgXfKiOc0zcIeejnhIiG3gCyZu2uEgTY5N5fgqVuUEvQZb8vdKSKDF07X8mOXLFL33PJjj9+cIleaiQ2r1Im5SjFckYySLX8Et4nTLLPAbtvhTNTfYhO9otHZQoIrIKemM3XHHdNjLrkqDMRzSG9RBwQGlJlBguErSQTSjlmk1bfNwllKVNKpIEyRiJAhf1dXqdaAx6qOhg0OcOlI+UA5LlI3pnRzZ4JbOPs7N4OS9LgjCg93xvpW3jwBmhJVFiuFvQwjWxKOVNOonQaxOKNVWIItFId4PMV+d1Spd4i/snEzK0mlgEPu2gzBriE/iaZs2J684B+yVBmYaNI/gWOGYLLYkSw92CFrLJHjuzosdfZBKNnNRTpLe64VDRTZ1eJ9riM3T5KAbqBh08qVtA1ye190oB9CcGXPxkY7e69CAiZUmAMNmqcRaP8bkhMZKoMflicIqwTY7bfRa9G40fkBw0DqokRUjyOTBeJbepUzshXsV5uvz06F/ACI1wlpaPvlpKAWQOE5dZok9Dcfh9eMhLEAYz83WkDx/yaS6hMYkPCAUsXBNzhsobs1/UgdrhikpSFL/aegfY4VLJjk5Rg1idP0a2yc5sU927y/bTxnDuvnx/rlTvT5kQVWYpZn+IuK2EGOWtZG0lhz/U+d+mbbHItlS9V9Eo74sui9wDbbuA/wBYLHoYrgR5qQAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$\\left ( 0.727268880663878, \\quad 0.7272689097917728\\right )$$"
],
"text/plain": [
"(0.727268880663878, 0.7272689097917728)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z.evalf(subs={e: math.e, x:1.0}), 0.7272689097917728"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Third CDA\n",
"$$\n",
"f^3(x) = \\frac{f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)}{2h^3} + O(h^2) \n",
"$$\n",
"สูตร\n",
"$$\n",
"f^3(x) \\approx \\frac{f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)}{2h^3} \\to (e)\n",
"$$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^1(x)$, (Third CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า Third CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Fourth CDA\n",
"$$\n",
"f^4(x) = \\frac{f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)}{h^4} + O(h^2)\n",
"$$\n",
"สูตร\n",
"$$\n",
"f^4(x) \\approx \\frac{f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)}{h^4} \\to (f)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^1(x)$, (Fourth CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า Fourth CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example\n",
"\n",
"1. กำหนดฟังก์ชัน $f(x) = e^{-x}$ จงคำนวณหา $f^2(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ second CDA\n",
"> จากสูตร $(d)$ เราสามารถประมาณค่า $f^2(x)$ ได้ โดยใช้สมการ\n",
"$$\n",
"f^2(x) = \\frac{f(x-h) - 2f(x) + f(x+h)}{h^2}\n",
"$$\n",
"ดังนั้น คำตอบคือ\n",
"$$\n",
"f^2(1) = \\frac{f(1-0.01) - 2\\times f(1) + f(1+0.01)}{0.01^2} \\\\\n",
"f^2(1) = \\frac{f(0.98) - 2\\times f(1) + f(1.01)}{0.0001} \\\\\n",
"f^2(1) = \\frac{e^{-0.98} - 2\\times e^{-1} + e^{-1.01}}{0.0001} \\\\\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"\n",
"1. กำหนดฟังก์ชัน $f(x) = x^3e^x$ จงคำนวณหา $f^2(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ second CDA\n",
"\n",
"2. กำหนดฟังก์ชัน $f(x) = x^2e^{-x}$ จงคำนวณหา $f^3(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ third CDA\n",
"\n",
"3. กำหนดฟังก์ชัน $f(x) = \\frac{e^{-x}}{x^3}$ จงคำนวณหา $f^4(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ fourth CDA\n",
"\n",
"4. กำหนดฟังก์ชัน $f(x) = \\frac{e^{-x}}{x^3}$ จงเขียนโปรแกรมเพื่อคำนวณหา $f^4(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ fourth CDA"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
sheet/numerical-method/09-10/Numerical-Differentiation-2.ipynb
0 → 100644
View file @
5290c59f
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numerical Differentiation\n",
"การหาผลต่าง \n",
"\n",
"> กำหนดฟังก์ชัน $f(x)$ จงคำนวณหา $\\frac{d^nf}{dx^n}$ เมื่อกำหนดค่า $x$ ให้\n",
"\n",
"โดยที่ $f(x)$ กำหนดได้สองแบบคือ\n",
"\n",
"* กำหนดเป็นฟังก์ชันสำหรับ การคำนวณหาโดยตรง $y = f(x)$ \n",
"* กำหนดความสัมพันธ์เป็นแบบชุดข้อมูลมาให้ $(x_i,y_i), i=0,1,..,n$\n",
"\n",
"## Taylor Series of $f(x)$ at $a$ is\n",
"$$\n",
"\\sum_{n=0}^{\\infty}\\frac{f^n(a)}{n!}(x-a)^n\n",
"$$\n",
"\n",
"ตัวอย่าง Taylor series สำหรับ $e^x$ ที่ $a = 0$ คือ\n",
"$$\n",
"\\frac{x^0}{0!} + \\frac{x^1}{1!} + \\frac{x^2}{2!} + \\frac{x^3}{3!} + ... \\\\\n",
"1 + x + \\frac{x^2}{2!} + \\frac{x^3}{3!} + ... \\\\\n",
"\\sum_{n=0}^{\\infty}\\frac{x^n}{n!}\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Backward/Forward Taylor Series on $f(x)$\n",
"$$\n",
"f(x+h) = f(x) + hf^1(x) + \\frac{h^2}{2!}f^2(x) + \\frac{h^3}{3!}f^3(x) + \\frac{h^4}{4!}f^4(x) + ... \\to (a) \\\\\n",
"f(x-h) = f(x) - hf^1(x) + \\frac{h^2}{2!}f^2(x) - \\frac{h^3}{3!}f^3(x) + \\frac{h^4}{4!}f^4(x) - ... \\to (b) \\\\\n",
"f(x+2h) = f(x) + 2hf^1(x) + \\frac{(2h)^2}{2!}f^2(x) + \\frac{(2h)^3}{3!}f^3(x) + \\frac{(2h)^4}{4!}f^4(x) + ... \\to (c) \\\\\n",
"f(x-2h) = f(x) - 2hf^1(x) + \\frac{(2h)^2}{2!}f^2(x) - \\frac{(2h)^3}{3!}f^3(x) + \\frac{(2h)^4}{4!}f^4(x) - ... \\to (d) \\\\\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ผลต่างของสมการ $(a), (b), (c), (d)$\n",
"$ (a) + (b) $\n",
"$$\n",
"f(x+h)+f(x-h) = 2f(x) + h^2f^2(x) + \\frac{h^4}{12}f^4(x) + ... \\to (e) \\\\\n",
"$$\n",
"$ (a) - (b) $\n",
"$$\n",
"f(x+h)-f(x-h) = 2hf^1(x) + \\frac{h^3}{3}f^3(x) + ... \\to (f) \\\\\n",
"$$\n",
"$ (c) + (d) $\n",
"$$\n",
"f(x+2h)+f(x-2h) = 2f(x) + 4h^2f^2(x) + \\frac{4h^4}{3}f^4(x) + ... \\to (g) \\\\\n",
"$$\n",
"$ (c) - (d) $\n",
"$$\n",
"f(x+2h)-f(x-2h) = 4hf^1(x) + \\frac{8h^3}{3}f^3(x) + ... \\to (h) \\\\\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finite Difference Approximations\n",
"\n",
"### Central Difference Approximations: CDA\n",
"\n",
"#### First CDA\n",
"$$\n",
"f^1(x) = \\frac{f(x+h) - f(x-h)}{2h} - \\frac{h^2}{3!}f^3(x) - \\frac{h^4}{5!}f^5(x) - \\frac{h^6}{7!}f^7(x) -...\n",
"$$\n",
"หรือ\n",
"$$\n",
"f'(x) = \\frac{f(x+h)-f(x-h)}{2h} + O(h^2)\n",
"$$\n",
"สูตร\n",
"$$\n",
"f'(x) \\approx \\frac{f(x+h)-f(x-h)}{2h} \\to (c)\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^1(x)$, (First CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า First CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Second CDA\n",
"$$\n",
"f^2(x) = \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2} + O(h^2)\n",
"$$\n",
"สูตร\n",
"$$\n",
"f^2(x) \\approx \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2} \\to (d)\n",
"$$\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^2(x)$, (Second CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า Second CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Third CDA\n",
"$$\n",
"f^3(x) = \\frac{f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)}{2h^3} + O(h^2) \n",
"$$\n",
"สูตร\n",
"$$\n",
"f^3(x) \\approx \\frac{f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)}{2h^3} \\to (e)\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^1(x)$, (Third CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า Third CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Fourth CDA\n",
"$$\n",
"f^4(x) = \\frac{f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)}{h^4} + O(h^2)\n",
"$$\n",
"สูตร\n",
"$$\n",
"f^4(x) \\approx \\frac{f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)}{h^4} \\to (f)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"1. จงคำนวณหาค่าประมาณของ $f^1(x)$, (Fourth CDA) พร้อมค่าผิดพลาด $O(h^2)$ ของฟังก์ชันต่อไปนี้ เมื่อกำหนด $h = 0.001, x=1$\n",
" * $f(x) = e^{-x}$\n",
" * $f(x) = sin(x)e^{-cos(x)}$\n",
"\n",
"2. จงเขียนฟังก์ชันเพื่อแสดงตารางค่าความสัมพันธ์ระหว่าง $h$ กับ ค่า Fourth CDA ของฟังก์ชันต่างๆในข้อ 1. เมื่อกำหนด $x = 1$\n",
" และ $h = [0.64, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01, 0.005, 0.0025, 0.00125]$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example\n",
"\n",
"1. กำหนดฟังก์ชัน $f(x) = e^{-x}$ จงคำนวณหา $f^2(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ second CDA\n",
"> จากสูตร $(d)$ เราสามารถประมาณค่า $f^2(x)$ ได้ โดยใช้สมการ\n",
"$$\n",
"f^2(x) = \\frac{f(x-h) - 2f(x) + f(x+h)}{h^2}\n",
"$$\n",
"ดังนั้น คำตอบคือ\n",
"$$\n",
"f^2(1) = \\frac{f(1-0.01) - 2\\times f(1) + f(1+0.01)}{0.01^2} \\\\\n",
"f^2(1) = \\frac{f(0.98) - 2\\times f(1) + f(1.01)}{0.0001} \\\\\n",
"f^2(1) = \\frac{e^{-0.98} - 2\\times e^{-1} + e^{-1.01}}{0.0001} \\\\\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise\n",
"\n",
"1. กำหนดฟังก์ชัน $f(x) = x^3e^x$ จงคำนวณหา $f^2(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ second CDA\n",
"\n",
"2. กำหนดฟังก์ชัน $f(x) = x^2e^{-x}$ จงคำนวณหา $f^3(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ third CDA\n",
"\n",
"3. กำหนดฟังก์ชัน $f(x) = \\frac{e^{-x}}{x^3}$ จงคำนวณหา $f^4(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ fourth CDA\n",
"\n",
"4. กำหนดฟังก์ชัน $f(x) = \\frac{e^{-x}}{x^3}$ จงเขียนโปรแกรมเพื่อคำนวณหา $f^4(x)$ ที่ $x = 1, h = 0.01$ โดยใช้ fourth CDA"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# สรุป Central Difference Approximations\n",
"\n",
"$$\n",
"(1) \\to f'(x) = \\frac{f(x+h)-f(x-h)}{2h} + O(h^2) \\\\\n",
"(2) \\to f^2(x) = \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2} + O(h^2) \\\\\n",
"(3) \\to f^3(x) = \\frac{f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)}{2h^3} + O(h^2) \\\\\n",
"(4) \\to f^4(x) = \\frac{f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)}{h^4} + O(h^2) \\\\\n",
"$$\n",
"\n",
"หรือถ้าไม่คิด $O(h^2)$\n",
"\n",
"$(1) \\to f'(x) = \\frac{f(x+h)-f(x-h)}{2h}$\n",
"\n",
"$(2) \\to f^2(x) = \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2}$\n",
"\n",
"$(3) \\to f^3(x) = \\frac{f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)}{2h^3}$\n",
"\n",
"$(4) \\to f^4(x) = \\frac{f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)}{h^4}$\n",
"\n",
"ปรับใหม่ \n",
"\n",
"$(1) \\to {2h}f'(x) = f(x+h)-f(x-h)$\n",
"\n",
"$(2) \\to {h^2}f^2(x) = f(x+h) - 2f(x) + f(x-h)$\n",
"\n",
"$(3) \\to {2h^3}f^3(x) = f(x+2h) - 2f(x+h) + 2f(x-h) + -f(x-2h)$\n",
"\n",
"$(4) \\to {h^4}f^4(x) = f(x+2h) - 4f(x+h) + 6f(x) - 4f(x-h) + f(x-2h)$\n",
"\n",
"แล้วเขียนออกมาเป็น ตาราง coefficient ได้เป็น\n",
"\n",
"# ตารางค่าสัมประสิทธิของ CDA $O(h^2$\n",
"\n",
"| สมการด้านซ้ายมือ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ |\n",
"|---------------------|----------------|---------------|-----------------|----------------|-----------------|\n",
"| | $f(x-2h)$ | $f(x-h)$ | $f(x)$ | $f(x+h)$ | $f(x+2h)$ |\n",
"|$2hf^1(x)$ | | -1 | 0 | 1 | |\n",
"|$h^2f^2(x)$ | | 1 | -2 | 1 | |\n",
"|$2h^3f^3(x)$ | -1 | 2 | 0 | -2 | 1 |\n",
"|$h^4f^4(x)$ | 1 | -4 | 6 | -4 | 1 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ในกรณีที่ ให้มาเป็นจุด หรือไม่สามารถประเมินโดยใช้ค่า $+h, -h$ ได้ จะใช้\n",
"\n",
"\n",
"# ตารางค่าสัมประสิทธิของ Forward Finite Difference Approximation (FDA) $O(h^2$\n",
"\n",
"| สมการด้านซ้ายมือ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ |\n",
"|----------------|----------------|----------------|---------------|-----------------|----------------|-----------------|\n",
"| | $f(x)$ | $f(x+h)$ | $f(x+2h)$ | $f(x+3h)$ | $f(x+4h)$ | $f(x+5h)$ |\n",
"|$2hf^1(x)$ | -3 | 4 | -1 | | | |\n",
"|$h^2f^2(x)$ | 2 | -5 | 4 | -1 | | |\n",
"|$2h^3f^3(x)$ | -5 | 18 | -24 | 14 | -3 | |\n",
"|$h^4f^4(x)$ | 3 | -14 | 26 | -24 | 11 | -2 |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# ตารางค่าสัมประสิทธิของ Backward Finite Difference Approximation (FDA) $O(h^2$\n",
"\n",
"| สมการด้านซ้ายมือ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ | ค่าสัมประสิทธิ์ของ |\n",
"|----------------|----------------|----------------|---------------|-----------------|----------------|-----------------|\n",
"| | $f(x-5h)$ | $f(x-4h)$ | $f(x-3h)$ | $f(x-2h)$ | $f(x-h)$ | $f(x)$ |\n",
"|$2hf^1(x)$ | | | | 1 | -4 | 3 |\n",
"|$h^2f^2(x)$ | | | -1 | 4 | -5 | 2 |\n",
"|$2h^3f^3(x)$ | | 3 | -14 | 24 | 18 | 5 |\n",
"|$h^4f^4(x)$ | -2 | 11 | -24 | 26 | -14 | 3 |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Richardson Extrapolation\n",
"\n",
"พิจารณา \n",
"$$\n",
"(2) \\to f^2(x) = \\frac{f(x+h) - 2f(x) + f(x-h)}{h^2} + O(h^2)\n",
"$$\n",
"ของฟังก์ชัน\n",
"$f(x) = e^{-x}$ ที่ $x=1$ \n",
"\n",
"เมื่อเปรียบเทียบกับค่าจริง(6 หลัก) $f^2(1) = e^{-1} = 0.3678794$\n",
"\n",
"\n",
"| $h$ | second CDA | ค่าจริง | $O(h^2)$ |\n",
"|---------|------------|----------|-----------|\n",
"| 0.64 | 0.380610 | 0.367879 | 0.012731 |\n",
"| 0.32 | 0.371035 | 0.367879 | 0.003156 |\n",
"| 0.16 | 0.368711 | 0.367879 | 0.000832 |\n",
"| 0.08 | 0.368281 | 0.367879 | 0.000402 |\n",
"| 0.04 | 0.36875 | 0.367879 | 0.000871 |\n",
"| 0.02 | 0.37 | 0.367879 | 0.002121 |\n",
"| 0.01 | 0.38 | 0.367879 | 0.012121 |\n",
"| 0.005 | 0.40 | 0.367879 | 0.032121 |\n",
"| 0.0025 | 0.48 | 0.367879 | 0.112121 |\n",
"| 0.00125 | 1.28 | 0.367879 | 0.912121 |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"เทคนิค Richardson Extrapolation ช่วยลดค่าความผิดพลาดได้ โดยมีเงื่อนไขคือ\n",
"$$\n",
"G = g(h) + E(h)\n",
"$$\n",
"และ $E(h) = ch^p$ เมื่อ $c,p$ เป็นค่าคงที่\n",
"\n",
"แทนค่าในสมการที่ $h_1, h_2$ \n",
"$$\n",
"G = g(h_1) + ch_{1}^p \\to (1) \\\\\n",
"G = g(h_2) + ch_{2}^p \\to (2)\n",
"$$\n",
"แล้วกำจัด $c$ จะได้\n",
"$$\n",
"G = \\frac{(h_1/h_2)^p g(h_2) - g(h_1)}{(h_1/h_2)^p -1}\n",
"$$\n",
"ซึ่งโดยปกติแล้วจะใช้ $h_2 = \\frac{h_1}{2}$ \n",
"จะทำให้\n",
"$$\n",
"G = \\frac{2^p g(h_1/2) - g(h_1)}{2^p - 1}\n",
"$$\n",
"\n",
"\n",
"## ตัวอย่าง\n",
"จากตาราง \n",
"$g(0.64) = 0.380610$ \n",
"$g(0.32) = 0.371035$\n",
"จะได้ $h_1 = 0.64, p=2$ \n",
"$$\n",
"G = \\frac{2^p g(h_1/2) - g(h_1)}{2^p - 1} \\\\\n",
"G = \\frac{2^2 g(0.64/2) - g(0.64)}{2^2 - 1} \\\\\n",
"G = \\frac{2^2 g(0.32) - g(0.64)}{2^2 - 1} \\\\\n",
"G = 0.367843\n",
"$$\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
sheet/numerical-method/README.md
0 → 100644
View file @
5290c59f
# Numerical Method
## Prerequisite
1.
Python3
2.
Jupyter
3.
Numpy
4.
Matplotlib
## Howtos
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment