{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5df251e",
   "metadata": {},
   "source": [
    "# Week 3: Simulasi Transien Rangkaian RC dan RL\n",
    "\n",
    "Persamaan diferensial orde pertama untuk rangkaian RC seri dan RL seri.\n",
    "\n",
    "## Rangkaian RC Seri\n",
    "$R \\frac{dq}{dt} + \\frac{q}{C} = V(t)$\n",
    "\n",
    "Atau dalam arus $I(t)$:\n",
    "$R I + \\frac{1}{C} \\int I dt = V(t) \\implies R \\frac{dI}{dt} + \\frac{I}{C} = \\frac{dV}{dt}$\n",
    "\n",
    "## Rangkaian RL Seri\n",
    "$L \\frac{dI}{dt} + R I = V(t)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36f13e5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact, FloatSlider\n",
    "\n",
    "def simulasi_rc_rl(R=100.0, C=10.0, L=0.1, V0=5.0, tipe='RC'):\n",
    "    t = np.linspace(0, 0.05, 500)\n",
    "    \n",
    "    if tipe == 'RC':\n",
    "        # Kapasitor pengisian\n",
    "        C_F = C * 1e-6\n",
    "        tau = R * C_F\n",
    "        vc = V0 * (1 - np.exp(-t/tau))\n",
    "        i = (V0/R) * np.exp(-t/tau)\n",
    "        \n",
    "        fig, ax1 = plt.subplots(figsize=(8, 4))\n",
    "        ax1.plot(t*1000, vc, 'b-', label=r'$V_C(t)$')\n",
    "        ax1.set_xlabel('Waktu (ms)')\n",
    "        ax1.set_ylabel('Tegangan (V)', color='b')\n",
    "        ax1.tick_params(axis='y', labelcolor='b')\n",
    "        \n",
    "        ax2 = ax1.twinx()\n",
    "        ax2.plot(t*1000, i*1000, 'r--', label=r'$I(t)$')\n",
    "        ax2.set_ylabel('Arus (mA)', color='r')\n",
    "        ax2.tick_params(axis='y', labelcolor='r')\n",
    "        \n",
    "        plt.title(rf\"Respons Transien RC ($\\tau = {tau*1000:.2f}$ ms)\")\n",
    "        fig.tight_layout()\n",
    "        plt.show()\n",
    "        \n",
    "    elif tipe == 'RL':\n",
    "        # Induktor pengisian\n",
    "        tau = L / R\n",
    "        i = (V0/R) * (1 - np.exp(-t/tau))\n",
    "        vl = V0 * np.exp(-t/tau)\n",
    "        \n",
    "        fig, ax1 = plt.subplots(figsize=(8, 4))\n",
    "        ax1.plot(t*1000, i*1000, 'r-', label=r'$I(t)$')\n",
    "        ax1.set_xlabel('Waktu (ms)')\n",
    "        ax1.set_ylabel('Arus (mA)', color='r')\n",
    "        ax1.tick_params(axis='y', labelcolor='r')\n",
    "        \n",
    "        ax2 = ax1.twinx()\n",
    "        ax2.plot(t*1000, vl, 'b--', label=r'$V_L(t)$')\n",
    "        ax2.set_ylabel('Tegangan (V)', color='b')\n",
    "        ax2.tick_params(axis='y', labelcolor='b')\n",
    "        \n",
    "        plt.title(rf\"Respons Transien RL ($\\tau = {tau*1000:.2f}$ ms)\")\n",
    "        fig.tight_layout()\n",
    "        plt.show()\n",
    "\n",
    "interact(simulasi_rc_rl, \n",
    "         R=FloatSlider(value=100.0, min=10.0, max=1000.0, step=10.0, description='R (Ohm)'),\n",
    "         C=FloatSlider(value=10.0, min=1.0, max=100.0, step=1.0, description='C (uF)'),\n",
    "         L=FloatSlider(value=0.1, min=0.01, max=1.0, step=0.01, description='L (H)'),\n",
    "         V0=FloatSlider(value=5.0, min=1.0, max=24.0, step=1.0, description='V0 (V)'),\n",
    "         tipe=['RC', 'RL']);"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
