{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6122ea1a",
   "metadata": {},
   "source": [
    "# The following code should be added for the problem listed in the comment for each code cell in Assignment 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84447219",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q8\n",
    "\n",
    "#the following code line should be added to Q8 before your 'your code here' answer\n",
    "x_axis = np.linspace(1, 100, 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc38fbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q10\n",
    "\n",
    "#put the following code in Q10 before 'your code here' in that cell. \n",
    "from scipy.optimize import curve_fit\n",
    "\n",
    "def linear_func(x, a, b):\n",
    "    return (a * x) + b\n",
    "\n",
    "def fit_r2(ydata, yfit):\n",
    "    residuals = ydata - yfit # difference between data and fit\n",
    "    ss_res = np.sum(residuals**2) # sum of the squares (ss) of the residuals\n",
    "    ss_tot = np.sum((ydata - np.mean(ydata))**2) # total sum of squares\n",
    "    r2 = 1 - (ss_res / ss_tot) # r-squared    \n",
    "    return r2\n",
    "\n",
    "xdata = xs[0,:]\n",
    "ydata = ys[0,:]\n",
    "popt, _ = curve_fit(linear_func, xdata, ydata)\n",
    "yfit = linear_func(xdata, *popt)\n",
    "\n",
    "# Because it can be non-intuitive, the below code will plot\n",
    "# your data and the fits, so you can test your code out\n",
    "#\n",
    "# indices = np.argsort(xdata)\n",
    "# yfit_sorted = yfit[indices]\n",
    "# xfit_sorted = xdata[indices]\n",
    "# plt.plot(xdata, ydata, 'o', label='data')\n",
    "# plt.plot(xfit_sorted, yfit_sorted, '--', label='fit')\n",
    "# plt.legend()\n",
    "# plt.show()"
   ]
  }
 ],
 "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
