{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Worksheet to start analysis of dry ice model in Section 3.5.1.\n",
    "\n",
    "#The data, in time (minutes)/mass (grams) pairs.\n",
    "\n",
    "data = [[0., 25.525], [120.0, 24.512], [240.0, 23.524], [360.0, 22.639], \n",
    "        [480.0, 21.765], [600.0, 20.89], [720.0, 20.043], [840.0, 19.221],\n",
    "        [960.0, 18.431], [1080.0, 17.677], [1200.0, 16.936], [1320.0, 16.22],\n",
    "        [1440.0, 15.548], [1570.0, 14.828], [1680.0, 14.213], [1800.0, 13.553],\n",
    "        [1930.0, 12.91], [2040.0, 12.331], [2170.0, 11.689], [2280.0, 11.188],\n",
    "        [2410.0, 10.566], [2530.0, 10.043], [2690.0, 9.377], [2780.0, 9.011],\n",
    "        [2880.0, 8.616], [3060.0, 7.945], [3220.0, 7.404], [3380.0, 6.877],\n",
    "        [3480.0, 6.593], [3600.0, 6.244]];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Number of data points\n",
    "N = len(data);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#A plot:\n",
    "plt1 = scatter_plot(data);\n",
    "plt1.axes_labels(['time (seconds)','mass (grams)'])\n",
    "show(plt1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#A linear model (bad) of the form u(t) = b - m*t can be fit as follows:\n",
    "var('t m b');\n",
    "u(t,m,b) = b - m*t;\n",
    "SS = function('SS')(m,b); #Sum of squares function, depends on m, b.\n",
    "SS(m,b) = add((u(data[i][0],m,b)-data[i][1])^2 for i in range(N));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#A plot of SS helps in finding the minimum\n",
    "plot3d(log(SS), (m,0,0.01), (b,10,30),aspect_ratio=[1000,1,1]).show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Then minimize SS with respect to m and b.\n",
    "\n",
    "#Redo with your own (better/physically motivated) model for u(t)!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 9.2",
   "language": "sage",
   "name": "sagemath"
  },
  "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
}