22
Lecture 4 コンピュータその4 1 求根アルゴリズム

Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

Lecture 4

コンピュータその41

求根アルゴリズム

Page 2: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

Gnuplot

xの範囲

Gnuplotの使い方

2

1. 端末を開き,gnuplotを打つ. 2. set term x11 を打つ. 3. plot [-5:5] sin(x) を打つ. 4. plot [-5:5] sin(x) linecolor 2 グラフの線の色を変える

linuxのみ

Page 3: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

1� cos(x)

x

2

Catastrophic cancellation

x の範囲

原点の付近ではどうなっているだろうか?

0.

3

1. 端末を開き,gnuplotを打つ.

2. plot [-5:5](1-cos(x))/(x*x) 3. x の範囲を小さくして繰り返す

Page 4: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

これをどう直せるのか?

a-b:a=x.xxxxxxxxxxx1 yyyy...

b=x.xxxxxxxxxxx0 zzzz... =)

=0.00000000001 ????

x.xxxxxxxxxxx1x.xxxxxxxxxxx0-

4

Catastrophic cancellation

Page 5: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

5

1. emacs compute_file.c2. 3. cc compute_file.c4. ./a.exe > data.dat5. gnuplot6. set term x117. plot ‘data.dat’ with lines

1.データファイルをプロットしてみましょう

Page 6: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

f(x)f(x⇤) = 0

Idea

Root-finding algorithms (求根アルゴリズム)

与えられた一次元の関数 に対しての  

f(x)

6

となる点を求める.

Page 7: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

f(x) = x

2 � x� 1

f(x) = tanh(x)� 3

注意事項(Observations)二つの根の場合

根無しの場合

実験からのデータはどうするのか?

7

Page 8: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

アルゴリズム的なアプローチ

•初期値:

•      の関係

•終了基準(stopping criteria)

x0

{x0, x1, ...}

何が必要か?

xn+1, xn, ...

8

1. |xn+1 � xn| < tol?

2. |f(xn+1)� f(xn)| < tol?

3. Both?

✏✏

Page 9: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

xn+1xn

f(xn)

f(xn+1)

アルゴリズム的なアプローチアルゴリズム的なアプローチ1. |xn+1 � xn| < tol?✏ 2. |f(xn+1)� f(xn)| < tol?✏

xn+1 xn

f(xn)f(xn+1)

|f(xn+1)� f(xn)| < ✏

|f(xn+1)� f(xn)| � ✏

|xn+1 � xn| � ✏

|xn+1 � xn| < ✏

Page 10: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

基本的な手法 (standard methods)

• Bisection (二分法)

–単純で使いやすい反復法の一つ

• Newton’s Method (ニュートン法)

–関数の微分が必要

–2次収束する (非常に早く解に収束)

• Secant Method (セカント法)

–微分の代用としてセカント(secant)の線を使う

–超線形収束する (Converges super-linearly)

10

Page 11: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

(二分法) Bisection Method

•中間値の定理 (intermediate value theorem)

f(x)

9x⇤ s.t. f(x⇤) = 0

11

Page 12: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

f(x)Step 1

+

-

(二分法) Bisection Method

12

Page 13: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

左辺は他の解があるが,それらを必要としない.

中間値の定理により,右辺の間にはf(x)=0となる点があるので,この区間に着目する.

+

-

-

Step 2

(二分法) Bisection Method

13

Page 14: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

+

-

-

 ここ!

Step 3

繰り返す

Step 4

+-

(二分法) Bisection Method

14

Page 15: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

p2

|ak � bk| =�12

�k |a0 � b0|

|ak � bk||f(ak)� f(bk)|

二分法を用いて  の値を近似せよ.2.

また,各反復回数ごとに以下の誤差も表示せよ.

i.

ii.

Note:

15

Exercise

Page 16: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

fa f(a)

m a+ (b� a)/2;fm f(m)if fafm > 0 then

for i = 1 to MAX do

a m

elseb m

end

fa fm

return m

二分法の疑似コード (Pseudo code)

end16

Page 17: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

0 = f(x⇤) = f(xk) + (x⇤ � xk)f0(xk) +

(x⇤ � xk)2

2f

00(⇠k),

x

⇤ = xk � f(xk)

f

0(xk).

(for some ⇠k between xk and x

⇤)

x

⇤ = xk � f(xk)

f

0(xk)� (x⇤ � xk)2

2(f 0(xk))f

00(⇠k)

(k = 1, 2, ...)xk+1 = xk � f(xk)

f

0(xk)

ニュートン法 (Newton’s Method)

テイラー展開:

従って,

ニュートン法の近似

繰り返す:

17

Page 18: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

(x1, f(x1))

(x2 = x1 � f(x1)/f 0(x1), 0)

f(x)

x

x1

ニュートン法 (Newton’s Method)

18

Page 19: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

(x3 = x2 � f(x2)/f 0(x2), 0)

x2

(x2, f(x2)) (x1, f(x1))

f(x)

x

x1

zoom in

ニュートン法 (Newton’s Method)

19

Page 20: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

• 必ずしも収束するとは言えない.

• 関数の微分を計算する事が不可欠である.

• 微分によって飛ぶ事がある.

ニュートン法の問題点

x0x0

20

f(x) = arctan(x)

Page 21: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

f(x) = sin(x)

Exercise

xk+1 = xk � f(xk)

f

0(xk)

3. ニュートン法のCプログラムを書きなさい.

ただし, とする.また,プログラムの出力を以下のように表示せよ.

Remember:

21

Page 22: Lecture 4 - 北海道大学mmc01.es.hokudai.ac.jp/.../courses/computer/lec/Lec4.pdfLecture 4 コンピュータその4 1 求根アルゴリズム Gnuplot xの範囲 Gnuplotの使い方

f(x) = x

2

f(x) = x

3+ x

2+ x+ 1 + cos(10x)

f(x) = x

3 � x� 3, x0 = 0

問題点4. 下記の関数に対するニュートン法の性質を 調べなさい.

(slow convergence)

22

(a cycle)