{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#A helpful script for Exercise 3.4.10, modeling a cooling potato.\n",
    "\n",
    "#The data, in time/temperature pairs:\n",
    "data = [[0, 204], [2, 193], [4, 184], [8, 169], [10, 162], [13, 156], [17, 149], [20, 143], [24, 138], [30, 130]]\n",
    "N = len(data);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#However, we will operate on the quantities (t, log(u(t)-A)-log(u(0)-A), with u(0)=204 and A = 72.\n",
    "logdata = [[data[i][0],log(data[i][1]-72)-log(204-72)] for i in range(N)];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#A plot:\n",
    "plt1 = scatter_plot(logdata);\n",
    "show(plt1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#We seek to fit a line y = -k*t to this\n",
    "var('t k');\n",
    "u(t,k) = -k*t; #The model function to fit\n",
    "SS = function('SS')(k);\n",
    "SS(k) = add((u(logdata[i][0],k)-logdata[i][1])^2 for i in range(N));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Now minimize SS(k) with respect to k."
   ]
  }
 ],
 "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
}