{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "db86c5af-b769-4d51-afe0-c5b27884bf3b" } }, "source": [ "# Least-Squares Regression Workbook \n", "## CH EN 2450 - Numerical Methods\n", "**Prof. Tony Saad (www.tsaad.net)
Department of Chemical Engineering
University of Utah**" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "59bc4550-5ef3-4891-8817-9b7525d8d84d" } }, "source": [ "# Example 1\n", "The height of a person vs femur length is given by the following data:\n", "\n", "| femur length (cm) | height (cm) |\n", "| :----------------: |:-----------: |\n", "| 40\t | 163|\n", "|41\t|165|\n", "|43\t|167|\n", "|43\t|164|\n", "|44\t|168|\n", "|44\t|169|\n", "|45\t|170|\n", "|45\t|167|\n", "|48\t|170|\n", "|51\t|175|\n", "\n", "Plot this data and then perform regression to a line and a quadratic using the standard derivation as well as the normal equations. Also use numpy's polyfit and compare to you solutions." ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "2af0a0eb-9013-4aca-a8f6-f47bde2594c9" } }, "source": [ "Let's first get the boiler plate out of the way and import matplotlib and numpy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "nbpresent": { "id": "acff7541-c478-484b-be35-84becaf643e8" } }, "outputs": [], "source": [ "%matplotlib inline\n", "%config InlineBackend.figure_format = 'svg'\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "ce4a31b8-73b5-499f-b0a7-b287eb4b5bba" } }, "source": [ "Now place the input data in numpy arrays. We can enter these manually or import from a text file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "nbpresent": { "id": "ab215b66-b879-4f0c-bf43-87ba09d16247" } }, "outputs": [], "source": [ "xi = np.array([40,41,43,43,44,44,45,45,48,51])\n", "yi = np.array([163,165,167,164,168,169,170,167,170,175])" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "e2adf352-d57b-461c-9d45-a1b3f47b2a23" } }, "source": [ "Now plot the data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "nbpresent": { "id": "732e1de5-36c8-45bc-a0e6-8033b5538339" } }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n" ], "text/plain": [ "