{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import numpy.linalg as LA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solve Matrix Equation : $\\large{Ax = b}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: 1 Modelling "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>no.</th>\n",
       "      <th>f</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.720482</td>\n",
       "      <td>0.737451</td>\n",
       "      <td>4.10</td>\n",
       "      <td>0.8695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.812391</td>\n",
       "      <td>0.884248</td>\n",
       "      <td>3.65</td>\n",
       "      <td>0.5750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.813530</td>\n",
       "      <td>0.863871</td>\n",
       "      <td>3.55</td>\n",
       "      <td>0.5125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.532572</td>\n",
       "      <td>0.872902</td>\n",
       "      <td>3.85</td>\n",
       "      <td>0.6520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.697305</td>\n",
       "      <td>0.891665</td>\n",
       "      <td>4.70</td>\n",
       "      <td>0.5400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>0.733407</td>\n",
       "      <td>0.880975</td>\n",
       "      <td>0.70</td>\n",
       "      <td>0.7745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>0.654187</td>\n",
       "      <td>0.733362</td>\n",
       "      <td>2.25</td>\n",
       "      <td>0.7130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>0.709219</td>\n",
       "      <td>0.839979</td>\n",
       "      <td>-1.10</td>\n",
       "      <td>0.9320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>0.750032</td>\n",
       "      <td>0.917038</td>\n",
       "      <td>-1.25</td>\n",
       "      <td>0.7335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>0.713197</td>\n",
       "      <td>0.915913</td>\n",
       "      <td>0.90</td>\n",
       "      <td>0.7345</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>0.521182</td>\n",
       "      <td>0.885547</td>\n",
       "      <td>-0.15</td>\n",
       "      <td>0.7685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>0.824820</td>\n",
       "      <td>0.884369</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.7090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>0.770785</td>\n",
       "      <td>0.873444</td>\n",
       "      <td>1.25</td>\n",
       "      <td>0.4890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>0.772823</td>\n",
       "      <td>0.726831</td>\n",
       "      <td>3.40</td>\n",
       "      <td>0.5375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>0.600803</td>\n",
       "      <td>0.638667</td>\n",
       "      <td>1.80</td>\n",
       "      <td>0.8985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>0.792857</td>\n",
       "      <td>0.753639</td>\n",
       "      <td>1.10</td>\n",
       "      <td>0.5705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>0.800259</td>\n",
       "      <td>0.819985</td>\n",
       "      <td>0.80</td>\n",
       "      <td>0.4965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>0.537606</td>\n",
       "      <td>0.847556</td>\n",
       "      <td>-0.25</td>\n",
       "      <td>0.8340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>0.575679</td>\n",
       "      <td>0.884614</td>\n",
       "      <td>2.85</td>\n",
       "      <td>0.7870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>0.821320</td>\n",
       "      <td>0.863913</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.6245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21</td>\n",
       "      <td>0.325209</td>\n",
       "      <td>0.757005</td>\n",
       "      <td>-0.20</td>\n",
       "      <td>0.7630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22</td>\n",
       "      <td>0.790763</td>\n",
       "      <td>0.899136</td>\n",
       "      <td>5.30</td>\n",
       "      <td>0.4470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23</td>\n",
       "      <td>0.803798</td>\n",
       "      <td>0.897501</td>\n",
       "      <td>3.45</td>\n",
       "      <td>0.3915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>0.785717</td>\n",
       "      <td>0.895834</td>\n",
       "      <td>4.40</td>\n",
       "      <td>0.4190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25</td>\n",
       "      <td>0.754932</td>\n",
       "      <td>0.808529</td>\n",
       "      <td>3.80</td>\n",
       "      <td>0.5265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26</td>\n",
       "      <td>0.793982</td>\n",
       "      <td>0.892413</td>\n",
       "      <td>5.10</td>\n",
       "      <td>0.4350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>0.789723</td>\n",
       "      <td>0.881186</td>\n",
       "      <td>3.40</td>\n",
       "      <td>0.4320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28</td>\n",
       "      <td>0.797951</td>\n",
       "      <td>0.936163</td>\n",
       "      <td>3.20</td>\n",
       "      <td>0.5280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29</td>\n",
       "      <td>0.796906</td>\n",
       "      <td>0.934264</td>\n",
       "      <td>2.80</td>\n",
       "      <td>0.4445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30</td>\n",
       "      <td>0.800149</td>\n",
       "      <td>0.941757</td>\n",
       "      <td>3.00</td>\n",
       "      <td>0.4490</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    no.         f         x     y       z\n",
       "0     1  0.720482  0.737451  4.10  0.8695\n",
       "1     2  0.812391  0.884248  3.65  0.5750\n",
       "2     3  0.813530  0.863871  3.55  0.5125\n",
       "3     4  0.532572  0.872902  3.85  0.6520\n",
       "4     5  0.697305  0.891665  4.70  0.5400\n",
       "5     6  0.733407  0.880975  0.70  0.7745\n",
       "6     7  0.654187  0.733362  2.25  0.7130\n",
       "7     8  0.709219  0.839979 -1.10  0.9320\n",
       "8     9  0.750032  0.917038 -1.25  0.7335\n",
       "9    10  0.713197  0.915913  0.90  0.7345\n",
       "10   11  0.521182  0.885547 -0.15  0.7685\n",
       "11   12  0.824820  0.884369  0.60  0.7090\n",
       "12   13  0.770785  0.873444  1.25  0.4890\n",
       "13   14  0.772823  0.726831  3.40  0.5375\n",
       "14   15  0.600803  0.638667  1.80  0.8985\n",
       "15   16  0.792857  0.753639  1.10  0.5705\n",
       "16   17  0.800259  0.819985  0.80  0.4965\n",
       "17   18  0.537606  0.847556 -0.25  0.8340\n",
       "18   19  0.575679  0.884614  2.85  0.7870\n",
       "19   20  0.821320  0.863913  0.75  0.6245\n",
       "20   21  0.325209  0.757005 -0.20  0.7630\n",
       "21   22  0.790763  0.899136  5.30  0.4470\n",
       "22   23  0.803798  0.897501  3.45  0.3915\n",
       "23   24  0.785717  0.895834  4.40  0.4190\n",
       "24   25  0.754932  0.808529  3.80  0.5265\n",
       "25   26  0.793982  0.892413  5.10  0.4350\n",
       "26   27  0.789723  0.881186  3.40  0.4320\n",
       "27   28  0.797951  0.936163  3.20  0.5280\n",
       "28   29  0.796906  0.934264  2.80  0.4445\n",
       "29   30  0.800149  0.941757  3.00  0.4490"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/data1.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{equation}\n",
    "f=a+bx+cy+dz\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Normal equation are\n",
    "\n",
    "$\\sum_{}^{}{f}=na+b\\sum{x}+c\\sum{y}+d\\sum{z}$\n",
    "\n",
    "$\\sum_{}^{}{xf}=a\\sum{x}+b\\sum{x^2}+c\\sum{xy}+d\\sum{xz}$\n",
    "\n",
    "$\\sum_{}^{}{yf}=a\\sum{y}+b\\sum{xy}+c\\sum{y^2}+d\\sum{yz}$\n",
    "\n",
    "$\\sum_{}^{}{zf}=a\\sum{z}+b\\sum{xz}+c\\sum{zy}+d\\sum{z^2}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{equation}\n",
    "A=\n",
    "\\begin{pmatrix}\n",
    "a11 & a12 & a13 &a14\\\\\n",
    "a21 & a22 & a23 &a24\\\\\n",
    "a31 & a32 & a33 &a34\\\\\n",
    "a41 & a42 & a43 &a44\\\\\n",
    "\\end{pmatrix}\n",
    "\\end{equation}\n",
    "\\begin{equation}\n",
    "b=\n",
    "\\begin{pmatrix}\n",
    "b1 \\\\\n",
    "b2 \\\\\n",
    "b3 \\\\\n",
    "b4\\\\\n",
    "\\end{pmatrix}\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "n=30\n",
    "a11=n\n",
    "a12=np.sum(df['x'])     #sum of x\n",
    "a13=np.sum(df['y'])     #sum of y\n",
    "a14=np.sum(df['z'])     #sum of z\n",
    "a21=np.sum(df['x'])     #sum of x\n",
    "a22=np.sum(df['x']**2)  #sum of x2\n",
    "a23=np.sum(np.dot(df['x'],df['y']))   #sum of xy\n",
    "a24=np.sum(np.dot(df['x'],df['z']))   #sum of xz\n",
    "a31=np.sum(df['y'])                   #sum of y\n",
    "a32=np.sum(np.dot(df['y'],df['x']))   #sum of xy\n",
    "a33=np.sum(df['y']**2)                #sum of y2   \n",
    "a34=np.sum(np.dot(df['y'],df['z']))   #sum of yz\n",
    "a41=np.sum(df['z'])                   #sum of z\n",
    "a42=np.sum(np.dot(df['z'],df['x']))   #sum of xz\n",
    "a43=np.sum(np.dot(df['z'],df['y']))   #sum of yz\n",
    "a44=np.sum(df['z']**2)                #sum of z2\n",
    "b1=np.sum(df['f'])                    #sum of f\n",
    "b2=np.sum(np.dot(df['f'],df['x']))    #sum of fx\n",
    "b3=np.sum(np.dot(df['f'],df['y']))    #sum of fy\n",
    "b4=np.sum(np.dot(df['f'],df['z']))    #sum of fz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.85507492,  0.14860913, -0.00131447, -0.41792768])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[a11,a12,a13,a14],[a21,a22,a23,a24],[a31,a32,a33,a34],[a41,a42,a43,a44]])\n",
    "b = np.array([b1,b2,b3,b4])\n",
    "x = LA.solve(A,b)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " a=      0.855\n",
      "\n",
      " b= 0.149\n",
      "\n",
      " c= -0.00131\n",
      "\n",
      " d= -0.418\n"
     ]
    }
   ],
   "source": [
    "print('\\n a=',\"{:10.3}\".format(x[0]))\n",
    "print('\\n b=',\"{:.3}\".format(x[1]))\n",
    "print('\\n c=',\"{:.3}\".format(x[2]))\n",
    "print('\\n d=',\"{:.3}\".format(x[3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.5958894003599622,\n",
       " 0.7413760360958024,\n",
       " 0.7645997139268501,\n",
       " 0.7072466193451008,\n",
       " 0.7557255547611699,\n",
       " 0.6613906839806531,\n",
       " 0.6631192534951209,\n",
       " 0.5918408249814666,\n",
       " 0.68644830702124,\n",
       " 0.6830369792734646,\n",
       " 0.6656950521790324,\n",
       " 0.6894008781179288,\n",
       " 0.7788669964212885,\n",
       " 0.7339832421031306,\n",
       " 0.5721125629994265,\n",
       " 0.7271988454495809,\n",
       " 0.7683794858588099,\n",
       " 0.6328064581873758,\n",
       " 0.6538812940618572,\n",
       " 0.7214786253986405,\n",
       " 0.6489567737789436,\n",
       " 0.7949143877809594,\n",
       " 0.8202981096648657,\n",
       " 0.8073086989485949,\n",
       " 0.7501958066178496,\n",
       " 0.7991933185714644,\n",
       " 0.8010132155323733,\n",
       " 0.7693252220065355,\n",
       " 0.8044656816412954,\n",
       " 0.8034356104392159]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_exp=[] # expected value of f\n",
    "for i in range(n):\n",
    "    f_exp.append(x[0]+x[1]*df.iloc[i,2]+x[2]*df.iloc[i,3]+x[3]*df.iloc[i,4])\n",
    "f_exp    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{equation}\n",
    "RMSE=\\sqrt{\\frac{1}{n}\\sum{(f-\\hat{f})^2}}\\\\\n",
    "R^2=1-\\frac{\\sum{(f-\\hat{f})^2}}{\\sum{(f-\\bar{f})^2}}\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "RMSE=np.sqrt(np.sum((df['f']-f_exp)**2)/n)\n",
    "R_square=1-(np.sum(np.sum(np.sum((df['f']-f_exp)**2))/np.sum((df['f']-df['f'].mean())**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " RMSE= 0.093\n",
      "\n",
      " R square= 0.36\n"
     ]
    }
   ],
   "source": [
    "print('\\n RMSE=',\"{:.2}\".format(RMSE))\n",
    "print('\\n R square=',\"{:.2}\".format(R_square))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example:2 Electric circuit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![img](image/circuit.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate I1,I2,I3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using Kirchhoff's laws in junction a,loop abdea,aegha"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$I3=I1+I2$\n",
    "\n",
    "$25.1I1+24+40.5I2=0$\n",
    "\n",
    "$-18+40.5I2+78.25I3=0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 18.9867374, -12.3596817,   6.6270557])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1,1,-1],[25.1,40.5,0],[0,40.5,78.25]]) #arranging I1,I2,I3\n",
    "b = np.array([0,-24,18])\n",
    "x = LA.solve(A,b)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " I1= 19.0\n",
      "\n",
      " I2= -12.4\n",
      "\n",
      " I3= 6.63\n"
     ]
    }
   ],
   "source": [
    "print('\\n I1=',\"{:.3}\".format(x[0]))\n",
    "print('\\n I2=',\"{:.3}\".format(x[1]))\n",
    "print('\\n I3=',\"{:.3}\".format(x[2]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.6"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}