{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c20bba77",
   "metadata": {},
   "source": [
    "# Week 12: Persamaan Panas (Difusi) 1D\n",
    "\n",
    "Persamaan difusi/panas 1D dapat diterapkan pada konduksi panas, difusi muatan di semikonduktor, atau penyebaran polutan.\n",
    "\n",
    "$\\frac{\\partial u}{\\partial t} = \\alpha \\frac{\\partial^2 u}{\\partial x^2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9565b029",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact, FloatSlider, IntSlider\n",
    "\n",
    "def simulasi_panas(alpha=0.1, max_waktu=2.0, langkah=50):\n",
    "    L = 10.0\n",
    "    nx = 50\n",
    "    dx = L / (nx - 1)\n",
    "    \n",
    "    # Syarat kestabilan FTCS\n",
    "    dt = (dx**2) / (2 * alpha * 1.5) \n",
    "    \n",
    "    u = np.zeros(nx)\n",
    "    # Kondisi awal: suhu tinggi di tengah\n",
    "    u[int(nx/2)-5 : int(nx/2)+5] = 100.0\n",
    "    \n",
    "    x = np.linspace(0, L, nx)\n",
    "    \n",
    "    # Simulasi hingga waktu t\n",
    "    waktu_target = max_waktu * (langkah / 100.0)\n",
    "    n_steps = int(waktu_target / dt) if dt > 0 else 0\n",
    "    \n",
    "    u_baru = np.copy(u)\n",
    "    for _ in range(n_steps):\n",
    "        for i in range(1, nx-1):\n",
    "            u_baru[i] = u[i] + alpha * dt / dx**2 * (u[i+1] - 2*u[i] + u[i-1])\n",
    "        u = np.copy(u_baru)\n",
    "        \n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.plot(x, u, 'r-', lw=2, marker='o', markersize=4)\n",
    "    plt.ylim(0, 110)\n",
    "    plt.xlabel('Posisi (x)')\n",
    "    plt.ylabel('Suhu (u)')\n",
    "    plt.title(rf\"Persamaan Panas 1D ($\\alpha$ = {alpha}) pada t = {waktu_target:.3f}\")\n",
    "    plt.grid(True)\n",
    "    plt.show()\n",
    "\n",
    "interact(simulasi_panas, \n",
    "         alpha=FloatSlider(value=0.5, min=0.1, max=2.0, step=0.1, description=r'$\\alpha$'),\n",
    "         max_waktu=FloatSlider(value=5.0, min=1.0, max=10.0, step=1.0, description='Max t'),\n",
    "         langkah=IntSlider(value=0, min=0, max=100, step=1, description='% Waktu'));"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
