Numerical methods เนื้อหามิดเทอม 2023/1

August 24, 2023

ไม่อยากอ่านแล้ว! เอาสูตรมาเลยน้อง

Table of Contents

Type of error

ϵ\epsilon: Error tolerance ex. 0.0000010.000001 or 0.00001%0.00001\%

  1. f(xm)<ϵ|f(x_m)| < \epsilon
    • สำหรับ Root of equation เพื่อ Check ว่า f(x)f(x) ใกล้ 00
  2. xmnewxmoldxmold×100<ϵ|\dfrac{x_{m_{new}}-x_{m_{old}}}{x_{m_{old}}}| \times 100 < \epsilon
    • ยิ่งค่าเข้าใกล้ xx ของจริง มันจะเปลี่ยนแปลงน้อยลงเรื่อยๆ

Root of equation

การหาว่า xx จะมีค่าเท่าไหร่ที่จะทำให้สมการเป็น 00

Graphical method (Sequential search)

Plot graph xx กับ f(x)f(x) ไปเรื่อยๆ แล้วมองด้วยตาว่า f(x)=0f(x)=0 ที่ xx เท่ากันเท่าไหร่

  • ex. หาตั้งแต่ [0,7][0,7]
xxcountdetail
x += 18เร็วสุด, ไม่เจอคำตอบ
x += 0.01800ไม่เจอคำตอบ
x += 0.0000018,000,000ช้ามากๆ, เจอคำตอบ

Solution ก็คือหาจนกว่าจะเจอการกลับเครื่องหมายจึงค่อย search ละเอียด

Bisection search (Binary search)

ปัญหาของ Graphical methods คือการ Search ที่เปลือง จึงใช้วิธีหาทีละครึ่งและดูว่าเครื่องหมายเปลี่ยนที่ด้านซ้ายหรือด้านขวา

สูตร: xM=xL+xR2x_M = \dfrac{x_L + x_R}{2}

Bisection

False-position method

คล้ายๆ กับ Bisection Method เพิ่ม การให้น้ำหนักของ xLx_L และ xRx_R

False-position

จากสูตรสามเหลี่ยมคล้าย

f(xR)xRx1=f(xL)xLx1\dfrac{f(x_R)}{x_R-x_1}=\dfrac{f(x_L)}{x_L-x_1}
xLf(xR)x1f(xR)=xRf(xL)x1f(xL)x_Lf(x_R)-x_1f(x_R)=x_Rf(x_L)-x_1f(x_L)
x1=xLf(xR)xRf(xL)f(xR)f(xL)x_1=\dfrac{x_Lf(x_R)-x_Rf(x_L)}{f(x_R)-f(x_L)}

One-Point Iteration method

  • Open method (only one initial value needed)
  • Simple but may not converge
One-point 1
One-point 2
  • Step: วิธีทำ

    ex. 0=f(x)=sinh(4x)+7x2x+30=f(x)=sinh(4x)+7x^2-x+3

    1. ย้ายข้างสมการให้เป็น x=...x=...

    2. สร้าง xi+1=sinh(4xi)+7xi2+3x_{i+1}=sinh(4x_i)+7x_i^2+3

    3. กำหนด x0x_0 หรือ Initial Value

      เช่น.

      x0=1x_0 = 1
      x1=sinh(4x0)+7x02+3x_{1} = sinh(4x_0) + 7x_0^2 + 3
      \vdots
      x10=sinh(4x9)+7x92+3x_{10} = sinh(4x_9) + 7x_9^2 + 3
    4. หยุดเมื่อ xoldxnew=0x_{old} - x_{new} = 0

Bonus: Taylor Series

f(x)=f(x0)+(xx0)f(x0)+(xx02)2!f(x0)+...+(xx0)nn!f(n)(x0)f(x)=f(x_0)+(x-x_0)f'(x_0)+\dfrac{(x-x_0^2)}{2!}f''(x_0)+...+\dfrac{(x-x_0)^n}{n!}f^{(n)}(x_0)
  • ฟังค์ชั่นใดๆ ในโลกนี้สามารถหาด้วย Taylor series ได้
  • ต้องมี x0x_0 และ fn(x)f^n(x) (ทุก dydx\dfrac{dy}{dx} ของ f(x)f(x))
Taylor series

As the degree of the Taylor polynomial rises, it approaches the correct function. This image shows sin x and its Taylor approximations by polynomials of degree 1, 3, 5, 7, 9, 11, and 13 at x = 0.

Newton-Raphson method

Newton-Raphson

วิธีนี้จะใช้การประมาณค่า xx โดยใช้ค่า xnx_n ปัจจุบันและค่าของฟังก์ชันแล้วดำเนินการปรับปรุงค่า xn+1x_{n+1} เพื่อให้ f(x)f(x) ใกล้เคียงศูนย์มากขึ้น กระบวนการนี้จะทำซ้ำจนกว่าค่า xx ที่ได้จะมีความเป็นศูนย์หรือใกล้เคียงศูนย์ตามที่ต้องการ

  • สูตร xn+1=xnf(xn)f(xn)x_{n+1}=x_n-\dfrac{f(x_n)}{f'(x_n)}
  • มาจาก Taylor Series ที่ n=1n = 1
0=f(x0)+(xx0)f(x0)0 = f(x_0) + (x - x_0)f'(x_0)
(xx0)f(x0)=f(x0)(x - x_0)f'(x_0) = -f(x_0)
xx0=f(x0)f(x0)x - x_0 = - \dfrac{f(x_0)}{f'(x_0)}
x=x0f(x0)f(x0)x = x_0 - \dfrac{f(x_0)}{f'(x_0)}

Secant method

Secant

ปัญหาของ Newton-Raphson methon คือการที่จำเป็นต้องรู้ x0,f(x0),f(x0),fn(x0)x_0, f(x_0), f'(x_0), f^{n}(x_0) ซึ่งการ dydx\dfrac{dy}{dx} ในคอมพิวเตอร์นั้นทำได้ยาก

  • ใช้เส้นตรงที่ผ่านทางสองจุดบนกราฟของฟังก์ชันเพื่อประมาณค่าของ xx
  • วิธีนี้เหมาะสำหรับฟังก์ชันที่ไม่สามารถหาอนุพันธ์ของฟังก์ชันได้
  • สูตร xn=xn2f(xn2)(xn1xn2)f(xn1)f(xn2)x_{n} = x_{n-2} - \dfrac{f(x_{n-2})(x_{n-1} - x_{n-2})}{f(x_{n-1}) - f(x_{n-2})} หรือ xn+1=xnf(xn)(xnxn1)fx(xn)fx(xn1)x_{n+1}=x_n - \dfrac{f(x_n)(x_n - x_{n-1})}{fx(x_n) - fx(x_{n-1})}

จาก Newton-Raphson

x2=x0f(x0)f(x0)    ;    f(x0)slope=f(x0)f(x1)x0x1=f(x1)f(x0)x1x0x_2 = x_0 - \dfrac{f(x_0)}{\color{red}f'(x_0)} \space\space\space\space ; \space\space\space\space f'(x_0) \approx slope = {\color{red}\dfrac{f(x_0) - f(x_1)}{x_0 - x_1} = \dfrac{f(x_1) - f(x_0)}{x_1 - x_0}}
x2=x0f(x0)f(x0)f(x1)x0x1=x0f(x0)f(x1)f(x0)x1x0x_2 = x_0 - \dfrac{f(x_0)}{\color{red}\dfrac{f(x_0) - f(x_1)}{x_0 - x_1}} = x_0 - \dfrac{f(x_0)}{\color{red}\dfrac{f(x_1) - f(x_0)}{x_1 - x_0}}
x2=x0f(x0)(x0x1)f(x0)f(x1)=x0f(x0)(x1x0)f(x1)f(x0)x_2 = x_0 - \dfrac{f(x_0)(x_0 - x_1)}{f(x_0) - f(x_1)} = x_0 - \dfrac{f(x_0)(x_1 - x_0)}{f(x_1) - f(x_0)}
xn=xn2f(xn2)(xn2xn1)f(xn2)f(xn1)=xn2f(xn2)(xn1xn2)f(xn1)f(xn2)x_n = x_{n-2} - \dfrac{f(x_{n-2})(x_{n-2} - x_{n-1})}{f(x_{n-2}) - f(x_{n-1})} = x_{n-2} - \dfrac{f(x_{n-2})(x_{n-1} - x_{n-2})}{f(x_{n-1}) - f(x_{n-2})}
Secant code

Linear algebra equation

หรือพีชคณิตเชิงเส้น เป็นการแก้และแทนระบบสมการเชิงเส้นด้วย Matrix

a11x1+a12x2+a13x3=b1a21x1+a22x2+a23x3=b2a31x1+a32x2+a33x3=b3Ax=B[a11a12a13a21a22a23a31a32a33]{x1x2x3}={b1b2b3}a_{11}x_1 + a_{12}x_2 + a_{13}x_3 = b_1 \\ a_{21}x_1 + a_{22}x_2 + a_{23}x_3 = b_2 \\ a_{31}x_1 + a_{32}x_2 + a_{33}x_3 = b_3 \\ \vdots \\ Ax=B \\ \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ \end{bmatrix} \begin{Bmatrix} x_1 \\ x_2 \\ x_3 \\ \end{Bmatrix}= \begin{Bmatrix} b_1 \\ b_2 \\ b_3 \\ \end{Bmatrix}

Cramer's rule

  • สูตร:
xi=det(Ai)det(A) ; A1=[b1a12a13b2a22a23b3a32a33],A2=[a11b1a13a21b2a23a31b3a33],A3=[a11a12b1a21a22b2a31a32b3]x_i = \dfrac{det(A_i)}{det(A)} \space ; \space A_{\color{red}1} = \begin{bmatrix} {\color{red}b_1} & a_{12} & a_{13} \\ {\color{red}b_2} & a_{22} & a_{23} \\ {\color{red}b_3} & a_{32} & a_{33} \\ \end{bmatrix},A_{\color{red}2} = \begin{bmatrix} a_{11} & {\color{red}b_1} & a_{13} \\ a_{21} & {\color{red}b_2} & a_{23} \\ a_{31} & {\color{red}b_3} & a_{33} \\ \end{bmatrix},A_{\color{red}3} = \begin{bmatrix} a_{11} & a_{12} & {\color{red}b_1} \\ a_{21} & a_{22} & {\color{red}b_2} \\ a_{31} & a_{32} & {\color{red}b_3} \\ \end{bmatrix}
  • AiA_i จะเป็นการเอา BB หรือคำตอบมาแทน AA ที่ Column ที่ ii

Bonus: Row Operation

5x1+7x2=10    2x1+5x2=5    {\begin{align*} \begin{equation} 5x_1 + 7x_2 = 10 \space\space\space\space \end{equation} \\ \begin{equation} 2x_1 + 5x_2 = 5 \space\space\space\space \end{equation} \end{align*}}
  • Multiplication
    • เช่น 5×(2)10x1+25x2=255 \times (2) \rArr 10x_1+25x_2 = 25
  • Add / Subtract
    • เช่น (2)+(1)7x1+12x2=15(2) + (1) \rArr 7x_1 + 12x_2 = 15

Guass elimination

  • Step: วิธีการทำ

    1. Forward elimination

      [a11a12a13b1a21a22a23b2a31a32a33b3]row operation[a11a12a13b10a22a23b200a33b3]\begin{bmatrix} a_{11} & a_{12} & a_{13} & | & b_{1} \\ a_{21} & a_{22} & a_{23} & | & b_{2} \\ a_{31} & a_{32} & a_{33} & | & b_{3} \\ \end{bmatrix} \xRightarrow[]{row\space operation} \begin{bmatrix} a_{11} & a_{12} & a_{13} & | & b_{1} \\ 0 & a'_{22} & a'_{23} & | & b'_{2} \\ 0 & 0 & a''_{33} & | & b''_{3} \\ \end{bmatrix}
    2. Back substitution

    x3=b3/a33x2=(b2a23x3)/a22x1=(b3a12x2a13x3)/a11orxi=bij=i+1naijxjaii\begin{align*} & x_3 = b''_3 / a_{33} \\ & x_2 = (b''_2 - a'_{23}x_3) / a_{22} \\ & x_1 = (b''_3 - a_{12}x_2 - a_{13}x_3) / a_{11} \end{align*} \\ \bold{or} \\ x_i = \dfrac{b_{i}- \sum\limits_{j=i + 1}^{n} {a_{ij}x_j} }{a_{ii}}

Guass Jordan elimination

  • คล้ายๆ กับ Guass elimination
  • Step: วิธีทำ
    1. Forward elimination

    2. Back elimination

      From Guass elimination;[a11a12a13b10a22a23b200a33b3]row operation[a1100b10a220b200a33b3]From\space Guass\space elimination; \\ \begin{bmatrix} a_{11} & a_{12} & a_{13} & | & b_{1} \\ 0 & a'_{22} & a'_{23} & | & b'_{2} \\ 0 & 0 & a''_{33} & | & b''_{3} \\ \end{bmatrix} \xRightarrow[]{row\space operation} \begin{bmatrix} a''_{11} & 0 & 0 & | & b''_{1} \\ 0 & a''_{22} & 0 & | & b''_{2} \\ 0 & 0 & a''_{33} & | & b''_{3} \\ \end{bmatrix}
    3. (rn)=(rn)/(rn)(r_n) = (r_n) / (r_n)

      [a1100b10a220b200a33b3]row operation[100b1010b2001b3]\begin{bmatrix} a''_{11} & 0 & 0 & | & b''_{1} \\ 0 & a''_{22} & 0 & | & b''_{2} \\ 0 & 0 & a''_{33} & | & b''_{3} \\ \end{bmatrix} \xRightarrow[]{row\space operation} \begin{bmatrix} 1 & 0 & 0 & | & b^*_{1} \\ 0 & 1 & 0 & | & b^*_{2} \\ 0 & 0 & 1 & | & b^*_{3} \\ \end{bmatrix}

Matrix Inversion

  • สร้าง Matrix [AI][A|I] และทำ Guass Jordan จะได้ [IA1][I|A^{-1}]

  • พิสูจน์สไตล์จารย์ 😎

    A=[2758]E1A=[2708]E2E1A=[2008]E3E2E1A=[1008]E4E3E2E1A=[1001]EnE3E2E1A=IA1=EnE3E2E1\begin{align*} A = \begin{bmatrix} 2 & 7 \\ 5 & 8 \end{bmatrix} \\ E_1A = \begin{bmatrix} 2 & 7 \\ 0 & 8 \end{bmatrix} \\ E_2E_1A = \begin{bmatrix} 2 & 0 \\ 0 & 8 \end{bmatrix} \\ E_3E_2E_1A = \begin{bmatrix} 1 & 0 \\ 0 & 8 \end{bmatrix} \\ E_4E_3E_2E_1A = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \end{align*} \\ \therefore {\color{red}E_n \dots E_3E_2E_1}A = I\\ A^{-1} = {\color{red}E_n \dots E_3E_2E_1}
  • วิธีทำแบบ Computer ก็คือการทำ Guass Jordan elimination

    [AI]En[IA1]\begin{bmatrix} A | I \end{bmatrix} \xRightarrow{E_n} \begin{bmatrix} I | A^{-1} \end{bmatrix}
  • จากนั้นคูณ Matrix [A]{x}={B}[A1]{B}={X}[A]\{x\}=\{B\} \rightarrow [A^{-1}]\{B\}=\{X\}

LU Decomposition method

  • ระเบิด Matrix ออกเป็นสองส่วน บู้มๆๆ 🔥
A=LU[a11a12a13a21a22a23a31a32a33]=[l1100l21l220l31l32l33][1u12u1301u23001]A = LU \\ \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ \end{bmatrix} = \begin{bmatrix} l_{11} & 0 & 0 \\ l_{21} & l_{22} & 0 \\ l_{31} & l_{32} & l_{33} \\ \end{bmatrix} \begin{bmatrix} 1 & u_{12} & u_{13} \\ 0 & 1 & u_{23} \\ 0 & 0 & 1 \\ \end{bmatrix}
  • Step วิธีทำ
    1. สร้าง Ax=B{\color{red}A}x=B
    2. LUx=B{\color{red}LU}x=B และกำหนด Y=UxY=Ux (Decomposition)
    3. LY=BLY=B
    4. หา YY จาก LY=BLY=B (Forward substitution)
    5. หา xx จาก Ux=YUx=Y (Back substitution)
LU Decomposition

สูตร

l11=a11u12=a12l11u13=a13l11      l21=a21l22=a22l21(u12)u23=a23l21(u13)l22      l31=a31l32=a32l31(u12)l33=a33l31(u13)l32(u23)\begin{align*} & l_{11} = a_{11} \\ & u_{12} = \dfrac{a_{12}}{l_{11}} \\ & u_{13} = \dfrac{a_{13}}{l_{11}} \end{align*}\space\space\space\space\space\space \begin{align*} & l_{21} = a_{21} \\ & l_{22} = a_{22} - l_{21}(u_{12}) \\ & u_{23} = \dfrac{a_{23} - l_{21}(u_{13})}{l_{22}} \end{align*}\space\space\space\space\space\space \begin{align*} & l_{31} = a_{31} \\ & l_{32} = a_{32} - l_{31}(u_{12}) \\ & l_{33} = a_{33} - l_{31}(u_{13}) - l_{32}(u_{23}) \end{align*}

Cholesky decomposition method

เงื่อนไขคือ [A][A] ต้องเป็น Symmetric Matrix\color{red}\textrm{Symmetric Matrix} เท่านั้น!

[A]=[L][L]T[a11a12a13a12a22a23a13a23a33]=[l1100l21l210l31l32l33][l1100l21l210l31l32l33][A]=[L] [L]^T \\ \begin{bmatrix} a_{11} & {\color{red}a_{12}} & {\color{blue}a_{13}} \\ {\color{red}a_{12}} & a_{22} & {\color{pink}a_{23}} \\ {\color{blue}a_{13}} & {\color{pink}a_{23}} & a_{33} \end{bmatrix}=\begin{bmatrix} l_{11} & 0 & 0 \\ l_{21} & l_{21} & 0 \\ l_{31} & l_{32} & l_{33} \\ \end{bmatrix}\cdot\begin{bmatrix} l_{11} & 0 & 0 \\ l_{21} & l_{21} & 0 \\ l_{31} & l_{32} & l_{33} \\ \end{bmatrix}
a11=l11l11+0l21+0l31l11=a11...\begin{align*} & a_{11} = l_{11}l_{11} + 0l_{21} + 0l_{31} \\ & l_{11} = \sqrt{a_{11}} \\ & ... \end{align*}

รวมสูตร

ชื่อสูตรวิธีทำ
Graphical method--
Bisection searchxM=xL+xR2x_M = \dfrac{x_L + x_R}{2}-
False-position methodx1=xLf(xR)xRf(xL)f(xR)f(xL)x_1=\dfrac{x_Lf(x_R)-x_Rf(x_L)}{f(x_R)-f(x_L)}-
One-Point Iteration methodxn+1=...xnx_{n+1} = ... x_{n}1. ย้ายข้างสมการให้ได้ x=...x=...
2. สร้าง xi+1=...xix_{i+1}=...x_i
3. กำหนด x0x_0
Taylor Seriesf(x)=f(x0)+(xx0)f(x0)+(xx02)2!f(x0)+...+(xx0)nn!f(n)(x0)f(x)=f(x_0)+(x-x_0)f'(x_0)+\dfrac{(x-x_0^2)}{2!}f''(x_0)+...+\dfrac{(x-x_0)^n}{n!}f^{(n)}(x_0)-
Newton-Raphson methodxn+1=xnf(xn)f(xn)x_{n+1}=x_n-\dfrac{f(x_n)}{f'(x_n)}-
Secant methodxn=xn2f(xn2)(xn1xn2)f(xn1)f(xn2)x_{n} = x_{n-2} - \dfrac{f(x_{n-2})(x_{n-1} - x_{n-2})}{f(x_{n-1}) - f(x_{n-2})}-
Cramer's rulexi=det(Ai)det(A) ; A1=[b1a12b2a22],A2=[a11b1a21b2]x_i = \dfrac{det(A_i)}{det(A)}\space;\space A_{\color{red}1}=\begin{bmatrix}{\color{red}b_1}&a_{12}\\ {\color{red}b_2}&a_{22}\end{bmatrix},A_{\color{red}2}=\begin{bmatrix}a_{11}&{\color{red}b_1}\\ a_{21}&{\color{red}b_2}\end{bmatrix}
Guass elimination-1. Forward elimination
2. Back substiution
Guass Jordan elimination-1. Forward elimination
2. Back elimination
Matrix Inversion-Guass Jordan elimination
LU Decomposition method-1. Ax=BLUx=BAx=B \rightarrow LUx=B
2. หา Y จาก LY=BLY=B
3. หา x จาก UX=YUX=Y
Cholesky decomposition method

Back to cscourse