{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from CoolProp.CoolProp import Props\n",
      "from math import sqrt\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "import scipy.optimize\n",
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Linearly Fitting Ancillary Equations"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Form of the Ancillary"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Each ancillary equation can be decomposed into a form like\n",
      "$$ L = \\sum_{i} n_i\\Theta^{t_i} $$\n",
      "where $\\Theta = 1-\\dfrac{T}{T_c}$ and the left hand side $L$ depends on the form to be fit. \n",
      "\n",
      "Four forms are commonly used depending on the curve being fit\n",
      "\n",
      "Saturated liquid:\n",
      "$$ L = \\dfrac{\\rho'}{\\rho_c}-1 $$\n",
      "\n",
      "Saturated vapor:\n",
      "$$ L = \\log\\left(\\dfrac{\\rho''}{\\rho_c}\\right)\\dfrac{T}{T_c} $$\n",
      "\n",
      "Saturation pressure:\n",
      "$$ L = \\log\\left(\\dfrac{p''}{p_c}\\right)\\dfrac{T}{T_c} $$\n",
      "\n",
      "The left hand side and right hand side can be evaluated for a large number of \"experimental\" data points obtained from the EOS.\n",
      "\n",
      "The best fit coefficients $n_i$ will yield the ancillary values\n",
      "\n",
      "$$\\sum_m L_m = \\sum_m\\left[\\sum_{i=0}^{I-1} n_i\\Theta_m^{t_i}\\right]$$\n",
      "\n",
      "The residual is then given by \n",
      "\n",
      "$$ \\zeta = a_{0}- \\sum_{i=0}^{I-1}n_ia_{i}$$\n",
      "\n",
      "where $a_0$ is $L$, and $a_i$ is $\\Theta^{t_i}$\n",
      "\n",
      "The sum of the squares of the residuals over the $M$ \"measured\" data points is given by\n",
      "\n",
      "$$ \\chi^2 = \\sum_{m=0}^{M-1}\\left(a_{0,m}- \\sum_{i=0}^{I-1}n_ia_{i,m}\\right)^2\\cdot \\frac{1}{\\sigma_m}$$\n",
      "\n",
      "We then build a $\\mathbf{A}$ matrix and $\\mathbf{b}$ vector, following the methods of Span (2000, p. 64) where the entries are\n",
      "\n",
      "$$ a_{ij} = \\sum_{m=0}^{M-1}\\left(\\Theta_m^{t_i}\\Theta_m^{t_j} \\right) $$\n",
      "\n",
      "and \n",
      "\n",
      "$$ b_{i} = \\sum_{m=0}^{M-1}\\left(\\Theta_m^{t_i}L_m \\right) $$\n",
      "\n",
      "And then solve the matrix equation\n",
      "\n",
      "$$\\mathbf{A}\\mathbf{n} = \\mathbf{b} $$\n",
      "\n",
      "to get $\\mathbf{n}$"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}