{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "%precision 3\n",
    "\n",
    "import math\n",
    "import PolynomialRegression as pr\n",
    "\n",
    "MAX_POINTS = 20\n",
    "TWO_PI     = 2*math.pi\n",
    "H          = TWO_PI/MAX_POINTS\n",
    "\n",
    "xs = []\n",
    "ys = []\n",
    "\n",
    "for i in range(MAX_POINTS):\n",
    "    x = i*H\n",
    "    xs.append(x)\n",
    "    ys.append(math.sin(x))\n",
    "    \n",
    "pr.plot_polynomial_regression(xs, ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A, b, a = pr.polynomial_regression(3, xs, ys)\n",
    "\n",
    "print('Coefficient matrix A:')\n",
    "print(A)\n",
    "\n",
    "print()\n",
    "print('Right hand side b:')\n",
    "print(b)\n",
    "\n",
    "print()\n",
    "print('Regression coefficients a:')\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_and_estimate(xs, ys, degree):\n",
    "    a = pr.plot_polynomial_regression(xs, ys, degree)\n",
    "    print(f'Degree {degree}: estimate of sin(π) = {pr.at(math.pi, a, degree):.3f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_and_estimate(xs, ys, degree=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_and_estimate(xs, ys, degree=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_and_estimate(xs, ys, degree=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_and_estimate(xs, ys, degree=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_and_estimate(xs, ys, degree=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
