|
358 | 358 | }, |
359 | 359 | { |
360 | 360 | "cell_type": "markdown", |
361 | | - "source": "### 동적 탐색<br>Interactive Exploration\n\n반복 횟수를 바꾸어 가며 구간이 어떻게 좁아지는지 관찰해 보자.<br>\nChange the number of iterations and observe how the interval narrows down.", |
| 361 | + "source": [ |
| 362 | + "### 동적 탐색<br>Interactive Exploration\n", |
| 363 | + "\n", |
| 364 | + "반복 횟수를 바꾸어 가며 구간이 어떻게 좁아지는지 관찰해 보자.<br>\n", |
| 365 | + "Change the number of iterations and observe how the interval narrows down.\n" |
| 366 | + ], |
362 | 367 | "metadata": {} |
363 | 368 | }, |
364 | 369 | { |
365 | 370 | "cell_type": "code", |
366 | | - "source": "from ipywidgets import interact, IntSlider\n\ndef plot_bisection(iterations):\n x_l, x_u = 0.0, 6.0\n\n # 이분법 반복\n # Bisection iterations\n for k in range(iterations):\n x_m = (x_l + x_u) * 0.5\n if f(x_u) * f(x_m) < 0:\n x_l = x_m\n elif f(x_l) * f(x_m) < 0:\n x_u = x_m\n else:\n break\n\n x_m = (x_l + x_u) * 0.5\n\n # f(x) 곡선\n # f(x) curve\n x_plot = py.linspace(0, 6, 200)\n py.plot(x_plot, f(x_plot), 'k-', label='$f(x)=x^2-10$')\n py.axhline(y=0, color='r', linestyle='--', alpha=0.5)\n\n # 현재 구간 표시\n # Show current interval\n py.axvline(x=x_l, color='blue', linestyle='--', alpha=0.7, label=f'$x_L$ = {x_l:.6f}')\n py.axvline(x=x_u, color='green', linestyle='--', alpha=0.7, label=f'$x_U$ = {x_u:.6f}')\n py.axvspan(x_l, x_u, alpha=0.15, color='yellow')\n\n # 중점 표시\n # Show midpoint\n py.plot(x_m, f(x_m), 'rs', markersize=10, label=f'$x_m$ = {x_m:.6f}')\n\n interval = x_u - x_l\n py.title(f'Iteration {iterations}, Interval = {interval:.6f}, f($x_m$) = {f(x_m):.2e}')\n py.xlabel('x')\n py.ylabel('f(x)')\n py.legend(loc='upper left')\n py.grid(True)\n py.show()\n\ninteract(plot_bisection, iterations=IntSlider(min=0, max=30, step=1, value=0,\n description='Iterations:'));", |
| 371 | + "source": [ |
| 372 | + "from ipywidgets import interact, IntSlider\n", |
| 373 | + "\n", |
| 374 | + "def plot_bisection(iterations):\n", |
| 375 | + " x_l, x_u = 0.0, 6.0\n", |
| 376 | + "\n", |
| 377 | + " # 이분법 반복\n", |
| 378 | + " # Bisection iterations\n", |
| 379 | + " for k in range(iterations):\n", |
| 380 | + " x_m = (x_l + x_u) * 0.5\n", |
| 381 | + " if f(x_u) * f(x_m) < 0:\n", |
| 382 | + " x_l = x_m\n", |
| 383 | + " elif f(x_l) * f(x_m) < 0:\n", |
| 384 | + " x_u = x_m\n", |
| 385 | + " else:\n", |
| 386 | + " break\n", |
| 387 | + "\n", |
| 388 | + " x_m = (x_l + x_u) * 0.5\n", |
| 389 | + "\n", |
| 390 | + " # f(x) 곡선\n", |
| 391 | + " # f(x) curve\n", |
| 392 | + " x_plot = py.linspace(0, 6, 200)\n", |
| 393 | + " py.plot(x_plot, f(x_plot), 'k-', label='$f(x)=x^2-10$')\n", |
| 394 | + " py.axhline(y=0, color='r', linestyle='--', alpha=0.5)\n", |
| 395 | + "\n", |
| 396 | + " # 현재 구간 표시\n", |
| 397 | + " # Show current interval\n", |
| 398 | + " py.axvline(x=x_l, color='blue', linestyle='--', alpha=0.7, label=f'$x_L$ = {x_l:.6f}')\n", |
| 399 | + " py.axvline(x=x_u, color='green', linestyle='--', alpha=0.7, label=f'$x_U$ = {x_u:.6f}')\n", |
| 400 | + " py.axvspan(x_l, x_u, alpha=0.15, color='yellow')\n", |
| 401 | + "\n", |
| 402 | + " # 중점 표시\n", |
| 403 | + " # Show midpoint\n", |
| 404 | + " py.plot(x_m, f(x_m), 'rs', markersize=10, label=f'$x_m$ = {x_m:.6f}')\n", |
| 405 | + "\n", |
| 406 | + " interval = x_u - x_l\n", |
| 407 | + " py.title(f'Iteration {iterations}, Interval = {interval:.6f}, f($x_m$) = {f(x_m):.2e}')\n", |
| 408 | + " py.xlabel('x')\n", |
| 409 | + " py.ylabel('f(x)')\n", |
| 410 | + " py.legend(loc='upper left')\n", |
| 411 | + " py.grid(True)\n", |
| 412 | + " py.show()\n", |
| 413 | + "\n", |
| 414 | + "interact(plot_bisection, iterations=IntSlider(min=0, max=30, step=1, value=0,\n", |
| 415 | + " description='Iterations:'));\n" |
| 416 | + ], |
367 | 417 | "metadata": {}, |
368 | 418 | "execution_count": null, |
369 | 419 | "outputs": [] |
370 | 420 | }, |
371 | 421 | { |
372 | 422 | "cell_type": "markdown", |
373 | | - "source": "반복 횟수를 0에서 30까지 바꾸어 보자. 구간의 폭과 $f(x_m)$이 어떻게 줄어드는가?<br>\nTry changing the number of iterations from 0 to 30. How do the interval width and $f(x_m)$ decrease?", |
| 423 | + "source": [ |
| 424 | + "반복 횟수를 0에서 30까지 바꾸어 보자. 구간의 폭과 $f(x_m)$이 어떻게 줄어드는가?<br>\n", |
| 425 | + "Try changing the number of iterations from 0 to 30. How do the interval width and $f(x_m)$ decrease?\n" |
| 426 | + ], |
374 | 427 | "metadata": {} |
375 | 428 | }, |
376 | 429 | { |
|
0 commit comments