{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4c1ca8ef-59b6-4de0-b5ff-ab48c25d28a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "99c042bf-04ae-486c-9737-1cd0f6497108",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    return (x + 3)**2\n",
    "\n",
    "def grad_f(x):\n",
    "    return 2 * (x + 3)  # derivative of f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e403baa2-9a95-4318-9c8f-06b89a9ae55f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_current = 2          # starting point\n",
    "learning_rate = 0.1    # step size\n",
    "tolerance = 1e-6       # convergence tolerance\n",
    "max_iterations = 25    # maximum iterations\n",
    "history = [x_current]  # sotring history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a570e44c-426c-49a2-a629-cec7b75b7ce9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iteration 1: x = 1.0000, f(x) = 16.0000\n",
      "Iteration 2: x = 0.2000, f(x) = 10.2400\n",
      "Iteration 3: x = -0.4400, f(x) = 6.5536\n",
      "Iteration 4: x = -0.9520, f(x) = 4.1943\n",
      "Iteration 5: x = -1.3616, f(x) = 2.6844\n",
      "Iteration 6: x = -1.6893, f(x) = 1.7180\n",
      "Iteration 7: x = -1.9514, f(x) = 1.0995\n",
      "Iteration 8: x = -2.1611, f(x) = 0.7037\n",
      "Iteration 9: x = -2.3289, f(x) = 0.4504\n",
      "Iteration 10: x = -2.4631, f(x) = 0.2882\n",
      "Iteration 11: x = -2.5705, f(x) = 0.1845\n",
      "Iteration 12: x = -2.6564, f(x) = 0.1181\n",
      "Iteration 13: x = -2.7251, f(x) = 0.0756\n",
      "Iteration 14: x = -2.7801, f(x) = 0.0484\n",
      "Iteration 15: x = -2.8241, f(x) = 0.0309\n",
      "Iteration 16: x = -2.8593, f(x) = 0.0198\n",
      "Iteration 17: x = -2.8874, f(x) = 0.0127\n",
      "Iteration 18: x = -2.9099, f(x) = 0.0081\n",
      "Iteration 19: x = -2.9279, f(x) = 0.0052\n",
      "Iteration 20: x = -2.9424, f(x) = 0.0033\n",
      "Iteration 21: x = -2.9539, f(x) = 0.0021\n",
      "Iteration 22: x = -2.9631, f(x) = 0.0014\n",
      "Iteration 23: x = -2.9705, f(x) = 0.0009\n",
      "Iteration 24: x = -2.9764, f(x) = 0.0006\n",
      "Iteration 25: x = -2.9811, f(x) = 0.0004\n"
     ]
    }
   ],
   "source": [
    "for i in range(max_iterations):\n",
    "    gradient = grad_f(x_current)\n",
    "    x_next = x_current - learning_rate * gradient  # update step\n",
    "    \n",
    "    # Check convergence\n",
    "    if abs(x_next - x_current) < tolerance:\n",
    "        print(f\"Converged after {i+1} iterations.\")\n",
    "        break\n",
    "    \n",
    "    x_current = x_next\n",
    "    history.append(x_current)\n",
    "    print(f\"Iteration {i+1}: x = {x_current:.4f}, f(x) = {f(x_current):.4f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fc2c152e-569a-4510-b57b-c56285c7b116",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Local minima at x = -2.981110534068521\n",
      "Function value at local minima y = 0.00035681192317650156\n"
     ]
    }
   ],
   "source": [
    "print(\"Local minima at x =\", x_current)\n",
    "print(\"Function value at local minima y =\", f(x_current))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f7c4e645-0ce3-474f-9ce2-d5ef9737b4bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29644 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(history, [f(val) for val in history], marker='o')\n",
    "plt.xlabel(\"x values\")\n",
    "plt.ylabel(\"f(x)\")\n",
    "plt.title(\"Gradient Descent Convergence\")\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4d0ec3f-7b9c-46e5-9264-aa884eccf121",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
