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
Littichai Buddaken
numer-60-2
Commits
6b8215e5
Commit
6b8215e5
authored
Mar 29, 2018
by
Littichai Buddaken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add week09
parent
9fc37dc2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
355 additions
and
0 deletions
+355
-0
Numerical-Differentiation-1.ipynb
week09/Numerical-Differentiation-1.ipynb
+355
-0
No files found.
week09/Numerical-Differentiation-1.ipynb
0 → 100644
View file @
6b8215e5
{
"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": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.3678795024846804"
]
},
"execution_count": 34,
"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": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f1x(x):\n",
" return (e**-(x+0.001) - e**-(x-0.001))/(2*0.001)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f1x(x, h):\n",
" return (e**-(x+h) - e**-(x-h))/(2*h)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution ของ $f(x) = sin(x)e^{-cos(x)}$\n",
"$$\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.3806090967261677,\n",
" 0.37102941395073613,\n",
" 0.36866492065626505,\n",
" 0.3680756854013579,\n",
" 0.3679284943797212,\n",
" 0.36789170398301985,\n",
" 0.3678825068437197,\n",
" 0.36788020758793394,\n",
" 0.36787963277440383,\n",
" 0.3678794890760173]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from math import sin,cos,e\n",
"x= (sin(1+0.001)*e**(-cos(1+0.001))-sin(1-0.001)*e**(-cos(1-0.001)))/(2*0.001)\n",
"x\n",
"def fsin():\n",
" return (sin(1+0.001)*e**(-cos(1+0.001))-sin(1-0.001)*e**(-cos(1-0.001)))/(2*0.001)\n",
"def fsinxh(x,h):\n",
" return (sin(x+h)*e**(-cos(x+h))-sin(x-h)*e**(-cos(x-h)))/(2*h)\n",
"\n",
"def sfsin():\n",
" return (sin(1+0.001)*e**(-cos(1+0.001))- 2*(sin(1)*e**(-cos(1))) +sin(1-0.001)*e**(-cos(1-0.001)))/(0.001**2)\n",
"def f2x(x,h):\n",
" ##f(x)=sin(x)e−cos(x)f(x)=sin(x)e−cos(x)\n",
" return (e**-(x+h) - 2*(e**(-x)) +e**-(x-h))/(h**2) ##new - 2*(e**(-x)) + ที่เพิ่มมาจากเดิม\n",
"f2x(1,0.001)\n",
"def f2xh(x,h):\n",
" return [(e**-(x+h[i]) - 2*(e**(-x)) +e**-(x-h[i]))/(h[i]**2) for i in range(0,10)]\n",
"h=[0.64,0.32,0.16,0.08,0.04,0.02,0.01,0.005,0.0025,0.00125]\n",
"f2xh(1,h)"
]
},
{
"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"
]
}
],
"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
}
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