+ "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:'));",
0 commit comments