diff --git a/HW1/README.md b/HW1/README.md index 426c9c5..bb1c7b4 100644 --- a/HW1/README.md +++ b/HW1/README.md @@ -55,3 +55,7 @@ vii. Paste this into the Homework #1 Google form reponse. (https://goo.gl/forms/jINjsioRLwQOiYtl2)[https://goo.gl/forms/jINjsioRLwQOiYtl2] +![Step 9](g9.png) + + viii. Click on the gear for "Settings" then "Collaborators" on the left menu. Add + `rcc02007` (Ryan C. Cooper) as a collaborator. diff --git a/HW1/g9.png b/HW1/g9.png new file mode 100644 index 0000000..e27469d Binary files /dev/null and b/HW1/g9.png differ diff --git a/README.md b/README.md index a7c930e..eebd665 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ matlab/octave functions and programming best practices. **Instructor**: Prof. Ryan C. Cooper (ryan.c.cooper@uconn.edu) -**Office hours**: Fridays 10am-12pm in Engineering II room 315 +**Office hours**: Mon 2:30-4:30pm and Thur 11am-1pm in Engineering II room 315 ## Teaching Assistants: -- Graduate: **TBD** -- Office hours: 2 hours / week in office **TBD** +- Graduate: Peiyu Zhang +- Office hours: 2 hours / week **Prerequisite:** CE 3110, MATH 2410Q diff --git a/lecture_01/lecture_01_notes.pdf b/lecture_01/lecture_01_notes.pdf new file mode 100644 index 0000000..d84a44d Binary files /dev/null and b/lecture_01/lecture_01_notes.pdf differ diff --git a/lecture_02/README.md b/lecture_02/README.md new file mode 100644 index 0000000..12365ce --- /dev/null +++ b/lecture_02/README.md @@ -0,0 +1,174 @@ + +# Solution to Form #1 + + +```octave +[1,2,3]*[1;2;3] +``` + + ans = + + 14 + + + + +```octave +[1,2,3]*[1;2;3]=? +``` + + +```octave + +``` + +# The first source of error is roundoff error +## Just storing a number in a computer requires rounding + + +```octave +fprintf('realmax = %1.20e\n',realmax) +fprintf('realmin = %1.20e\n',realmin) +fprintf('maximum relative error = %1.20e\n',eps) + +``` + + realmax = 1.79769313486231570815e+308 + realmin = 2.22507385850720138309e-308 + maximum relative error = 2.22044604925031308085e-16 + + + +```octave +s=1; +for i=1:1000 + s=s+eps/10; +end +s==1 +``` + + ans = 1 + + +# Freefall Model (revisited) +## Octave solution (will run same on Matlab) + +Set default values in Octave for linewidth and text size + + +```octave +%plot --format svg +``` + + +```octave +set (0, "defaultaxesfontname", "Helvetica") +set (0, "defaultaxesfontsize", 18) +set (0, "defaulttextfontname", "Helvetica") +set (0, "defaulttextfontsize", 18) +set (0, "defaultlinelinewidth", 4) +``` + +Define time from 0 to 12 seconds with `N` timesteps +function defined as `freefall` + + +```octave +function [v_analytical,v_terminal,t]=freefall(N) + t=linspace(0,12,N)'; + c=0.25; m=60; g=9.81; v_terminal=sqrt(m*g/c); + + v_analytical = v_terminal*tanh(g*t/v_terminal); + v_numerical=zeros(length(t),1); + delta_time =diff(t); + for i=1:length(t)-1 + v_numerical(i+1)=v_numerical(i)+(g-c/m*v_numerical(i)^2)*delta_time(i); + end + % Print values near 0,2,4,6,8,10,12 seconds + indices = round(linspace(1,length(t),7)); + fprintf('time (s)|vel analytical (m/s)|vel numerical (m/s)\n') + fprintf('-----------------------------------------------\n') + M=[t(indices),v_analytical(indices),v_numerical(indices)]; + fprintf('%7.1f | %18.2f | %15.2f\n',M(:,1:3)'); + plot(t,v_analytical,'-',t,v_numerical,'o-') +end + +``` + + +```octave +[v_analytical,v_terminal,t]=freefall(120); +``` + + time (s)|vel analytical (m/s)|vel numerical (m/s) + ----------------------------------------------- + 0.0 | 0.00 | 0.00 + 2.0 | 18.76 | 18.82 + 4.0 | 32.64 | 32.80 + 6.1 | 40.79 | 40.97 + 8.0 | 44.80 | 44.94 + 10.0 | 46.84 | 46.93 + 12.0 | 47.77 | 47.82 + + + +![svg](output_13_1.svg) + + +# Types of error +## Freefall is example of "truncation error" +### Truncation error results from approximating exact mathematical procedure + +We approximated the derivative as $\delta v/\delta t\approx\Delta v/\Delta t$ + +Can reduce error by decreasing step size -> $\Delta t$=`delta_time` + +## Another example of truncation error is a Taylor series (or Maclaurin if centered at a=0) + +Taylor series: +$f(x)=f(a)+f'(a)(x-a)+\frac{f''(a)}{2!}(x-a)^{2}+\frac{f'''(a)}{3!}(x-a)^{3}+...$ + +We can approximate the next value in a function by adding Taylor series terms: + +|Approximation | formula | +|---|-------------------------| +|$0^{th}$-order | $f(x_{i+1})=f(x_{i})+R_{1}$ | +|$1^{st}$-order | $f(x_{i+1})=f(x_{i})+f'(x_{i})h+R_{2}$ | +|$2^{nd}$-order | $f(x_{i+1})=f(x_{i})+f'(x_{i})h+\frac{f''(x_{i})}{2!}h^{2}+R_{3}$| +|$n^{th}$-order | $f(x_{i+1})=f(x_{i})+f'(x_{i})h+\frac{f''(x_{i})}{2!}h^{2}+...\frac{f^{(n)}}{n!}h^{n}+R_{n}$| + +Where $R_{n}=\frac{f^{(n+1)}(\xi)}{(n+1)!}h^{n+1}$ is the error associated with truncating the approximation at order $n$. + +The $n^{th}$-order approximation estimates that the unknown function, $f(x)$, is equal to an $n^{th}$-order polynomial. + +In the Freefall example, we estimated the function with a $1^{st}$-order approximation, so + +$v(t_{i+1})=v(t_{i})+v'(t_{i})(t_{i+1}-t_{i})+R_{1}$ + +$v'(t_{i})=\frac{v(t_{i+1})-v(t_{i})}{t_{i+1}-t_{i}}-\frac{R_{1}}{t_{i+1}-t_{i}}$ + +$\frac{R_{1}}{t_{i+1}-t_{i}}=\frac{v''(\xi)}{2!}(t_{i+1}-t_{i})$ + +or the truncation error for a first-order Taylor series approximation is + +$\frac{R_{1}}{t_{i+1}-t_{i}}=O(\Delta t)$ + + +1. digital representation of a number is rarely exact + +2. arithmetic (+,-,/,\*) causes roundoff error + + +```octave +fprintf('%1.20f\n',double(pi)) +fprintf('%1.20f\n',single(pi)) +``` + + 3.14159265358979311600 + 3.14159274101257324219 + + + +```octave + +``` diff --git a/lecture_02/lecture_02.ipynb b/lecture_02/lecture_02.ipynb new file mode 100644 index 0000000..3e0a9e6 --- /dev/null +++ b/lecture_02/lecture_02.ipynb @@ -0,0 +1,597 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution to Form #1" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans =\n", + "\n", + " 1 2 3\n", + "\n" + ] + } + ], + "source": [ + "[1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "[1,2,3]*[1;2;3]=?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The first source of error is roundoff error\n", + "## Just storing a number in a computer requires rounding" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "realmax = 1.79769313486231570815e+308\n", + "realmin = 2.22507385850720138309e-308\n", + "maximum relative error = 2.22044604925031308085e-16\n" + ] + } + ], + "source": [ + "fprintf('realmax = %1.20e\\n',realmax)\n", + "fprintf('realmin = %1.20e\\n',realmin)\n", + "fprintf('maximum relative error = %1.20e\\n',eps)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 1\r\n" + ] + } + ], + "source": [ + "s=1;\n", + "for i=1:1000\n", + " s=s+eps/10;\n", + "end\n", + "s==1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Freefall Model (revisited)\n", + "## Octave solution (will run same on Matlab)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Set default values in Octave for linewidth and text size" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%plot --format svg" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "set (0, \"defaultaxesfontname\", \"Helvetica\")\n", + "set (0, \"defaultaxesfontsize\", 18)\n", + "set (0, \"defaulttextfontname\", \"Helvetica\")\n", + "set (0, \"defaulttextfontsize\", 18) \n", + "set (0, \"defaultlinelinewidth\", 4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Define time from 0 to 12 seconds with `N` timesteps \n", + "function defined as `freefall`" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "function [v_analytical,v_terminal,t]=freefall(N)\n", + " t=linspace(0,12,N)';\n", + " c=0.25; m=60; g=9.81; v_terminal=sqrt(m*g/c);\n", + "\n", + " v_analytical = v_terminal*tanh(g*t/v_terminal);\n", + " v_numerical=zeros(length(t),1);\n", + " delta_time =diff(t);\n", + " for i=1:length(t)-1\n", + " v_numerical(i+1)=v_numerical(i)+(g-c/m*v_numerical(i)^2)*delta_time(i);\n", + " end\n", + " % Print values near 0,2,4,6,8,10,12 seconds\n", + " indices = round(linspace(1,length(t),7));\n", + " fprintf('time (s)|vel analytical (m/s)|vel numerical (m/s)\\n')\n", + " fprintf('-----------------------------------------------\\n')\n", + " M=[t(indices),v_analytical(indices),v_numerical(indices)];\n", + " fprintf('%7.1f | %18.2f | %15.2f\\n',M(:,1:3)');\n", + " plot(t,v_analytical,'-',t,v_numerical,'o-')\n", + "end\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "time (s)|vel analytical (m/s)|vel numerical (m/s)\n", + "-----------------------------------------------\n", + " 0.0 | 0.00 | 0.00\n", + " 2.0 | 18.76 | 18.82\n", + " 4.0 | 32.64 | 32.80\n", + " 6.1 | 40.79 | 40.97\n", + " 8.0 | 44.80 | 44.94\n", + " 10.0 | 46.84 | 46.93\n", + " 12.0 | 47.77 | 47.82\n" + ] + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "Gnuplot\n", + "Produced by GNUPLOT 5.0 patchlevel 3 \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t \n", + "\t \n", + "\t\n", + "\t\n", + "\t \n", + "\t \n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t30\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t40\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t50\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t6\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t8\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t12\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tgnuplot_plot_2a\n", + "\n", + "\t\t \n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[v_analytical,v_terminal,t]=freefall(120);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Types of error\n", + "## Freefall is example of \"truncation error\"\n", + "### Truncation error results from approximating exact mathematical procedure" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We approximated the derivative as $\\delta v/\\delta t\\approx\\Delta v/\\Delta t$\n", + "\n", + "Can reduce error by decreasing step size -> $\\Delta t$=`delta_time`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Another example of truncation error is a Taylor series (or Maclaurin if centered at a=0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Taylor series:\n", + "$f(x)=f(a)+f'(a)(x-a)+\\frac{f''(a)}{2!}(x-a)^{2}+\\frac{f'''(a)}{3!}(x-a)^{3}+...$\n", + "\n", + "We can approximate the next value in a function by adding Taylor series terms:\n", + "\n", + "|Approximation | formula |\n", + "|---|-------------------------|\n", + "|$0^{th}$-order | $f(x_{i+1})=f(x_{i})+R_{1}$ |\n", + "|$1^{st}$-order | $f(x_{i+1})=f(x_{i})+f'(x_{i})h+R_{2}$ |\n", + "|$2^{nd}$-order | $f(x_{i+1})=f(x_{i})+f'(x_{i})h+\\frac{f''(x_{i})}{2!}h^{2}+R_{3}$|\n", + "|$n^{th}$-order | $f(x_{i+1})=f(x_{i})+f'(x_{i})h+\\frac{f''(x_{i})}{2!}h^{2}+...\\frac{f^{(n)}}{n!}h^{n}+R_{n}$|\n", + "\n", + "Where $R_{n}=\\frac{f^{(n+1)}(\\xi)}{(n+1)!}h^{n+1}$ is the error associated with truncating the approximation at order $n$.\n", + "\n", + "The $n^{th}$-order approximation estimates that the unknown function, $f(x)$, is equal to an $n^{th}$-order polynomial. \n", + "\n", + "In the Freefall example, we estimated the function with a $1^{st}$-order approximation, so \n", + "\n", + "$v(t_{i+1})=v(t_{i})+v'(t_{i})(t_{i+1}-t_{i})+R_{1}$\n", + "\n", + "$v'(t_{i})=\\frac{v(t_{i+1})-v(t_{i})}{t_{i+1}-t_{i}}-\\frac{R_{1}}{t_{i+1}-t_{i}}$\n", + "\n", + "$\\frac{R_{1}}{t_{i+1}-t_{i}}=\\frac{v''(\\xi)}{2!}(t_{i+1}-t_{i})$\n", + "\n", + "or the truncation error for a first-order Taylor series approximation is\n", + "\n", + "$\\frac{R_{1}}{t_{i+1}-t_{i}}=O(\\Delta t)$\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. digital representation of a number is rarely exact\n", + "\n", + "2. arithmetic (+,-,/,\\*) causes roundoff error" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.14159265358979311600\n", + "3.14159274101257324219\n" + ] + } + ], + "source": [ + "fprintf('%1.20f\\n',double(pi))\n", + "fprintf('%1.20f\\n',single(pi))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Octave", + "language": "octave", + "name": "octave" + }, + "language_info": { + "file_extension": ".m", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "octave", + "version": "0.19.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/lecture_02/lecture_02.pdf b/lecture_02/lecture_02.pdf new file mode 100644 index 0000000..edad8fc Binary files /dev/null and b/lecture_02/lecture_02.pdf differ diff --git a/lecture_02/notes.pdf b/lecture_02/notes.pdf new file mode 100755 index 0000000..66e503c Binary files /dev/null and b/lecture_02/notes.pdf differ