{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Fitting in Chebyshev basis\n",
    "\n",
    "Plot noisy data and their polynomial fit in a Chebyshev basis\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61156 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 创建随机数生成器并固定种子 27446968，保证每次运行结果可复现\n",
    "rng = np.random.default_rng(27446968)\n",
    "\n",
    "# 在 [-1, 1] 区间均匀生成 2000 个横坐标点\n",
    "x = np.linspace(-1, 1, 2000)\n",
    "\n",
    "# 生成纵坐标：基础信号为 cos(x)，再叠加 [-0.3, 0.3] 区间的均匀随机噪声\n",
    "y = np.cos(x) + 0.3 * rng.random(2000)\n",
    "\n",
    "# 用 90 阶 Chebyshev 多项式对带噪数据进行最小二乘拟合，返回可调用多项式对象 p\n",
    "p = np.polynomial.Chebyshev.fit(x, y, 90)\n",
    "\n",
    "# 绘制原始数据散点图，红色圆点\n",
    "plt.plot(x, y, \"r.\")\n",
    "\n",
    "# 在同一张图上绘制拟合曲线，黑色实线，线宽 3 像素\n",
    "plt.plot(x, p(x), \"k-\", lw=3)\n",
    "\n",
    "# 显示最终图形\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
