{ "metadata": { "name": "", "signature": "sha256:3d9763ad8e713bc4c7be2162464d383f293b498ef88147f61f44be6689e11eb8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Linear algebra for multidimensional polynomial fitting" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import itertools\n", "# A small data set\n", "#T_in = np.array([20,24,35,40])+273.15\n", "#x_in = np.array([47,55,70,78,82])/100.0\n", "#rho_in = np.array([[1047,1033,1020,1000,990],[997,983,970,950,940],[947,933,920,900,895],[987,883,870,850,845]])\n", "#\n", "# large data set\n", "T_in = np.array([ -45 , -40 , -35 , -30 , -25 , -20 , -15 , -10])+273.15 # Kelvin\n", "x_in = np.array([ 5 , 10 , 15 , 20 , 25 , 30 , 35 ])/100.0 # mass fraction\n", " \n", "rho_in = np.array([\n", " [1064.0, 1054.6, 1045.3, 1036.3, 1027.4, 1018.6, 1010.0],\n", " [1061.3, 1052.1, 1043.1, 1034.3, 1025.6, 1017.0, 1008.6],\n", " [1057.6, 1048.8, 1040.1, 1031.5, 1023.1, 1014.8, 1006.7],\n", " [1053.1, 1044.6, 1036.2, 1028.0, 1019.9, 1012.0, 1004.1],\n", " [1047.5, 1039.4, 1031.5, 1023.7, 1016.0, 1008.4, 1000.9],\n", " [1040.7, 1033.2, 1025.7, 1018.4, 1011.2, 1004.0, 997.0],\n", " [1032.3, 1025.3, 1018.5, 1011.7, 1005.1, 998.5, 992.0],\n", " [1021.5, 1015.3, 1009.2, 1003.1, 997.1, 991.2, 985.4]]) # kg/m3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Minimizing the squared error $\\epsilon(\\mathbf{c}) = \\sqrt{\\sum (\\mathbf{z} - \\mathbf{A} \\cdot \\mathbf{c})^2 }$ can be achieved by solving the system of orthogonal equations given by $\\mathbf{A}^\\text{T}\\mathbf{A} \\cdot \\mathbf{c} =\\mathbf{A}^\\text{T}\\mathbf{z}$. Using Python tools, we can leave this to the software and we only have to construct the Van der Monde matrix $\\mathbf{A}$ of the independent variable and equate it with the result vector of the dependent variable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def getCoeffs1d(x,z,order):\n", " if (len(x)