diff --git a/HW5/README.md b/HW5/README.md index a8495ea..953348b 100644 --- a/HW5/README.md +++ b/HW5/README.md @@ -4,7 +4,7 @@ *Include all work as either an m-file script, m-file function, or example code included with \`\`\` and document your code in the README.md file* -1. Create a new github repository called 'linear_algebra'. +1. Create a new github repository called 'linear_algebra'. a. Add rcc02007 and pez16103 as collaborators. @@ -13,7 +13,7 @@ with \`\`\` and document your code in the README.md file* 2. Create an LU-decomposition function called `lu_tridiag.m` that takes 3 vectors as inputs and calculates the LU-decomposition of a tridiagonal matrix. The output should be 3 vectors, the diagonal of the Upper matrix, and the two off-diagonal vectors of the Lower -and Upper matrices. +and Upper matrices. ```[ud,uo,lo]=lu_tridiag(e,f,g);``` @@ -26,7 +26,7 @@ the backslash solver `\`, create an algebraic solution* 4. Test your function on the matrices A3, A4, ..., A10 generated with `test_arrays.m` Solving for `b=ones(N,1);` where N is the size of A. In your `README.md` file, compare -the norm of the error between your result and the result of AN\b. +the norm of the error between your result and the result of AN\b. ``` | size of A | norm(error) | @@ -40,10 +40,10 @@ the norm of the error between your result and the result of AN\b. 5. In the system shown above, determine the three differential equations for the position of masses 1, 2, and 3. Solve for the vibrational modes of the spring-mass system if k1=10 N/m, k2=k3=20 N/m, and k4=10 N/m. The masses are m1=1 kg, m2=2 kg and m3=4 kg. Determine -the eigenvalues and natural frequencies. +the eigenvalues and natural frequencies. 6. The curvature of a slender column subject to an axial load P (Fig. P13.10) can be -modeled by +modeled by $\frac{d^{2}y}{dx^{2}} + p^{2} y = 0$ @@ -54,7 +54,7 @@ about its neutral axis. This model can be converted into an eigenvalue problem by substituting a centered finite-difference approximation for the second derivative to give -$\frac{y_{i+1} -2y_{i} + y_{i-1} }{\Delta x^{2}}+ p^{2} y_{i}$ +$\frac{y_{i+1} -2y_{i} + y_{i-1} }{\Delta x^{2}}+ p^{2} y_{i}$ where i = a node located at a position along the rod’s interior, and $\Delta x$ = the spacing between nodes. This equation can be expressed as $y_{i-1} - (2 - \Delta x^{2} @@ -66,11 +66,11 @@ Determine the eigenvalues for a 5-segment (4-interior nodes), 6-segment (5-inter nodes), and 10-segment (9-interior nodes). Using the modulus and moment of inertia of a pole for pole-vaulting ( [http://people.bath.ac.uk/taf21/sports_whole.htm](http://people.bath.ac.uk/taf21/sports_whole.htm)) -E=76E9 Pa, I=4E-8 m^4, and L= 5m. +E=76E9 Pa, I=4E-8 m^4, and L= 5m. Include a table in the `README.md` that shows the following results: What are the largest and smallest eigenvalues for the beam? How many eigenvalues are -there? +there? ``` | # of segments | largest | smallest | # of eigenvalues | diff --git a/Pull.png b/Pull.png new file mode 100644 index 0000000..d25c512 Binary files /dev/null and b/Pull.png differ diff --git a/extra_credit_1/data.csv b/extra_credit_1/data.csv index 8e4a5ce..282ff39 100644 --- a/extra_credit_1/data.csv +++ b/extra_credit_1/data.csv @@ -1,3 +1,8 @@ radius (cm), angle (degrees) 0, 0 2.25, 62 +3.81, 140 +6.35, 116 +.635, 88 +7.62, 12 +1.27, 180 diff --git a/lecture_01/lecture_01.md b/lecture_01/lecture_01.md index 807e888..196a29e 100644 --- a/lecture_01/lecture_01.md +++ b/lecture_01/lecture_01.md @@ -106,6 +106,5 @@ plot(t,v_analytical,'-',t,v_numerical,'o-') ![plot of velocities](https://github.uconn.edu/rcc02007/ME3255S2017/blob/master/lecture_01/output_10_0.svg) - + diff --git a/lecture_07/lecture_07.ipynb b/lecture_07/lecture_07.ipynb index 08d29c4..66fa9a8 100644 --- a/lecture_07/lecture_07.ipynb +++ b/lecture_07/lecture_07.ipynb @@ -1435,22 +1435,23 @@ } ], "metadata": { + "anaconda-cloud": {}, "kernelspec": { - "display_name": "Octave", - "language": "octave", - "name": "octave" + "display_name": "Python [conda root]", + "language": "python", + "name": "conda-root-py" }, "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" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" } }, "nbformat": 4, diff --git a/lecture_07/mod_secant.m b/lecture_07/mod_secant.m index a3caa10..df97983 100644 --- a/lecture_07/mod_secant.m +++ b/lecture_07/mod_secant.m @@ -14,13 +14,13 @@ % ea = approximate relative error (%) % iter = number of iterations if nargin<3,error('at least 3 input arguments required'),end -if nargin<4 || isempty(es),es=0.0001;end +if nargin<4 || isempty(es),es=0.0001;end if nargin<5 || isempty(maxit),maxit=50;end -iter = 0; +iter = 0 while (1) xrold = xr; dfunc=(func(xr+dx)-func(xr))./dx; - xr = xr - func(xr)/dfunc; + xr = xr - func(xr)/dfunc; %calculate as an approximation iter = iter + 1; if xr ~= 0 ea = abs((xr - xrold)/xr) * 100; diff --git a/lecture_10/GaussNaive.m b/lecture_10/GaussNaive.m index d8c60d3..9153c13 100644 --- a/lecture_10/GaussNaive.m +++ b/lecture_10/GaussNaive.m @@ -13,7 +13,9 @@ % forward elimination for k = 1:n-1 for i = k+1:n - factor = Aug(i,k)/Aug(k,k); + factor = Aug(i,k)/Aug(k,k); + %this is naive because if a coefficient in the matrix is zero + %then divide by zero errot. Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb); end end diff --git "a/lecture_15/\340\244\267\001" "b/lecture_15/\340\244\267\001" deleted file mode 100644 index e69de29..0000000 diff --git "a/lecture_15/\360&\341\001" "b/lecture_15/\360&\341\001" deleted file mode 100644 index e69de29..0000000 diff --git a/lecture_17/.ipynb_checkpoints/lecture_17-checkpoint.ipynb b/lecture_17/.ipynb_checkpoints/lecture_17-checkpoint.ipynb index 2fd6442..4d8aeeb 100644 --- a/lecture_17/.ipynb_checkpoints/lecture_17-checkpoint.ipynb +++ b/lecture_17/.ipynb_checkpoints/lecture_17-checkpoint.ipynb @@ -1,6 +1,2711 @@ { - "cells": [], - "metadata": {}, + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "setdefaults" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%plot --format svg" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![question 1](q1.png)\n", + "\n", + "![question 2](q2.png)\n", + "\n", + "### Project Ideas so far\n", + "\n", + "- Nothing yet...probably something heat transfer related\n", + "\n", + "- Modeling Propulsion or Propagation of Sound Waves\n", + "\n", + "- Low Thrust Orbital Transfer\n", + "\n", + "- Tracking motion of a satellite entering orbit until impact\n", + "\n", + "- What ever you think is best.\n", + "\n", + "- You had heat transfer project as an option; that sounded cool\n", + "\n", + "- Heat transfer through a pipe\n", + "\n", + "- I would prefer to do something with beam/plate mechanics or vibrations than a heat transfer or thermo problem\n", + "\n", + "- Modeling Couette flow with a pressure gradient using a discretized form of the Navier-Stokes equation and comparing it to the analytical solution\n", + "\n", + "- Software to instruct a robotic arm to orient itself based on input from a gyroscope" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Questions from you:\n", + "\n", + "How was your spring break\n", + "\n", + "Are grades for hw 3 and 4 going to be posted?\n", + "\n", + "For class occasionally switching to doc cam and working through problems by hand might help get ideas across.\n", + "\n", + "Are you assigning those who do not have groups to groups?\n", + "\n", + "How do you graph a standard distribution in Matlab?\n", + "\n", + "What is the longest code you have written?\n", + "\n", + "how to approach probability for hw 5?\n", + "??\n", + "\n", + "Will you be assigning groups to people who do not currently have one? \n", + "\n", + "what are some basic guidelines we should use to brainstorm project ideas?\n", + "\n", + "Are you a fan of bananas?\n", + "\n", + "Going through code isn't the most helpful, because you can easily lose interest. But I am not sure what else you can do.\n", + "\n", + "Has lecture 15 been posted yet? Looking in the repository I can't seem to find it." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Linear Least Squares Regression\n", + "\n", + "When approximating a set of data as a polynomial, there will always be error introduced (except in 2 cases). \n", + "\n", + "For a straight line, the actual data points, $y_{i}$ as a function of the independent variable, $x_{i}$, is:\n", + "\n", + "$y_{i}=a_{0}+a_{1}x_{i}+e_{i}$\n", + "\n", + "where $a_{0}$ and $a_{1}$ are the intercept and slope of the line and $e_{i}$ is the error between the approximate function and the recorded data point. \n", + "\n", + "We make the following assumptions in this analysis:\n", + "\n", + "1. Each x has a fixed value; it is not random and is known without error.\n", + "\n", + "2. The y values are independent random variables and all have the same variance.\n", + "\n", + "3. The y values for a given x must be normally distributed.\n", + "\n", + "The total error is \n", + "\n", + "$\\sum_{i=1}^{n}e_{i}=\\sum_{i=1}^{n}(y_{i}-a_{0}-a_{1}x_{i})$\n", + "\n", + "we don't care about the sign though. One approach has been demonstrated to provide a unique solution is minimizing the sum of squares error or\n", + "\n", + "$S_{r}=\\sum_{i=1}^{n}e_{i}^{2}=\\sum_{i=1}^{n}(y_{i}-a_{0}-a_{1}x_{i})^{2}$\n", + "\n", + "Where, $S_{r}$ is the sum of squares error (SSE). \n", + "\n", + "$\\frac{\\partial S_{r}}{\\partial a_{0}}=-2\\sum(y_{i}-a_{0}-a_{1}x_{i})$\n", + "\n", + "$\\frac{\\partial S_{r}}{\\partial a_{1}}=-2\\sum(y_{i}-a_{0}-a_{1}x_{i})x_{i}$\n", + "\n", + "The minimum $S_{r}$ occurrs when the partial derivatives are 0. \n", + "\n", + "$\\sum y_{i}= \\sum a_{0}+\\sum a_{1}x_{i}$\n", + "\n", + "$\\sum x_{i}y_{i}= \\sum a_{0}x_{i}+\\sum a_{1}x_{i}^{2}$\n", + "\n", + "$\\left[\\begin{array}{c}\n", + "\\sum y_{i}\\\\\n", + "\\sum x_{i}y_{i}\\end{array}\\right]=\n", + "\\left[\\begin{array}{cc}\n", + "n & \\sum x_{i}\\\\\n", + "\\sum x_{i} & \\sum x_{i}^{2}\\end{array}\\right]\n", + "\\left[\\begin{array}{c}\n", + "a_{0}\\\\\n", + "a_{1}\\end{array}\\right]$\n", + "\n", + "\n", + "$b=Ax$\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example \n", + "\n", + "Find drag coefficient with best-fit line to experimental data\n", + "\n", + "|i | v (m/s) | F (N) |\n", + "|---|---|---|\n", + "|1 | 10 | 25 |\n", + "|2 | 20 | 70 |\n", + "|3 | 30 | 380|\n", + "|4 | 40 | 550|\n", + "|5 | 50 | 610|\n", + "|6 | 60 | 1220|\n", + "|7 | 70 | 830 |\n", + "|8 |80 | 1450|" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a =\n", + "\n", + " -234.286\n", + " 19.470\n", + "\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\t-500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2000\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\t60\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t70\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t80\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tvelocity (m/s)\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\tForce (N)\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tdata\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tdata\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\n", + "\t\n", + "\tbest-fit\n", + "\n", + "\t\n", + "\t\tbest-fit\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "drag_data=[...\n", + "1 , 10 , 25 \n", + "2 , 20 , 70 \n", + "3 , 30 , 380\n", + "4 , 40 , 550\n", + "5 , 50 , 610\n", + "6 , 60 , 1220\n", + "7 , 70 , 830 \n", + "8 ,80 , 1450];\n", + "x=drag_data(:,2);\n", + "y=drag_data(:,3);\n", + "\n", + "b=[sum(y);sum(x.*y)];\n", + "A=[length(x),sum(x);\n", + " sum(x), sum(x.^2)];\n", + " \n", + "a=A\\b\n", + "\n", + "plot(x,y,'o',x,a(1)+a(2)*x)\n", + "legend('data','best-fit','Location','NorthWest')\n", + "xlabel('Force (N)')\n", + "ylabel('velocity (m/s)')" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-2000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-500\n", + "\t\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\t60\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t70\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t80\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tresiduals (N)\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\tvelocity (m/s)\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\tModel does not capture measurements\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tdata\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tdata\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot(x,y-a(1)-40*x)\n", + "legend('data','best-fit','Location','NorthWest')\n", + "ylabel('residuals (N)')\n", + "xlabel('velocity (m/s)')\n", + "title('Model does not capture measurements')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How do we know its a \"good\" fit? \n", + "\n", + "Can compare the sum of squares error to the total sum of squares of the dependent variable (here F). \n", + "\n", + "$S_{r}=\\sum(y_{i}-a_{0}-a_{1}x_{i})^{2}$\n", + "\n", + "$S_{t}=\\sum(y_{i}-\\bar{y})^{2}$\n", + "\n", + "Then, we can calculate the *coefficient of determination*, $r^{2}$ or *correlation coefficient*, r. \n", + "\n", + "$r^{2}=\\frac{S_{t}-S_{r}}{S_{t}}$\n", + "\n", + "This represents the relative improvement of assuming that y is a function of x (if the x-values are not random and the y-values are random)\n", + "\n", + "For further information regarding statistical work on regression, look at \n", + "[NIST Statistics Handbook](http://www.itl.nist.gov/div898/handbook/pmd/section4/pmd44.htm)" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "St = 1.8083e+06\n", + "St = 1.8083e+06\n" + ] + } + ], + "source": [ + "Sr=sum((y-a(1)-a(2)*x).^2);\n", + "St=std(y)^2*(length(y)-1)\n", + "St=sum((y-mean(y)).^2)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "r2 = 0.88049\n", + "r = 0.93834\n" + ] + } + ], + "source": [ + "r2=(St-Sr)/St\n", + "r=sqrt(r2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Limiting cases \n", + "\n", + "#### $r^{2}=0$ $S_{r}=S{t}$\n", + "\n", + "#### $r^{2}=1$ $S_{r}=0$" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t0.2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.6\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.8\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.6\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.8\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tCase 1\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tCase 1\n", + "\t\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", + "\n", + "\t\n", + "\tCase 2\n", + "\n", + "\t\n", + "\t\tCase 2\n", + "\t\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", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "gen_examples\n", + "plot(x1(1:50:end),y1(1:50:end),'s',x2,y2,'o')\n", + "legend('Case 1','Case 2','Location','NorthWest')" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a1 =\n", + "\n", + " 0.0482496\n", + " 0.0017062\n", + "\n", + "a2 =\n", + "\n", + " 0\n", + " 1\n", + "\n", + "Sr1 = 0.83505\n", + "St1 = 0.83529\n", + "coefficient of determination in Case 1 is 0.000291\n", + "Sr2 = 0\n", + "St2 = 1.0185\n", + "coefficient of determination in Case 2 is 1.000000\n" + ] + } + ], + "source": [ + "b1=[sum(y1);sum(x1.*y1)];\n", + "A1=[length(x1),sum(x1);\n", + " sum(x1), sum(x1.^2)];\n", + " \n", + "a1=A1\\b1\n", + "\n", + "b2=[sum(y2);sum(x2.*y2)];\n", + "A2=[length(x2),sum(x2);\n", + " sum(x2), sum(x2.^2)];\n", + " \n", + "a2=A2\\b2\n", + "\n", + "Sr1=sum((y1-a1(1)-a1(2)*x1).^2)\n", + "St1=sum((y1-mean(y1)).^2)\n", + "fprintf('coefficient of determination in Case 1 is %f\\n',1-Sr1/St1)\n", + "\n", + "Sr2=sum((y2-a2(1)-a2(2)*x2).^2)\n", + "St2=sum((y2-mean(y2)).^2)\n", + "\n", + "fprintf('coefficient of determination in Case 2 is %f\\n',1-Sr2/St2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# General Linear Regression" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In general, you may want to fit other polynomials besides degree-1 (straight-lines)\n", + "\n", + "$y=a_{0}+a_{1}x+a_{2}x^{2}+\\cdots+a_{m}x^{m}+e$\n", + "\n", + "Now, the solution for $a_{0},~a_{1},...a_{m}$ is the minimization of m+1-dependent linear equations. \n", + "\n", + "Consider the following data:\n", + "\n", + "| x | y |\n", + "|---|---|\n", + "| 0.00 | 21.50 |\n", + "| 2.00 | 20.84 |\n", + "| 4.00 | 23.19 |\n", + "| 6.00 | 22.69 |\n", + "| 8.00 | 30.27 |\n", + "| 10.00 | 40.11 |\n", + "| 12.00 | 43.31 |\n", + "| 14.00 | 54.79 |\n", + "| 16.00 | 70.88 |\n", + "| 18.00 | 89.48 |\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\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\t60\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t70\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t80\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t90\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\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", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "load xy_data.csv\n", + "x=xy_data(1,:)';\n", + "y=xy_data(2,:)';\n", + "plot(x,y,'o')" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "xy_data =\n", + "\n", + " Columns 1 through 7:\n", + "\n", + " 0.00000 2.00000 4.00000 6.00000 8.00000 10.00000 12.00000\n", + " 21.50114 20.84153 23.19201 22.69498 30.26687 40.11075 43.30543\n", + "\n", + " Columns 8 through 11:\n", + "\n", + " 14.00000 16.00000 18.00000 20.00000\n", + " 54.78730 70.88443 89.48368 97.28135\n", + "\n" + ] + } + ], + "source": [ + "xy_data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The model can be rewritten as \n", + "\n", + "$y=\\left[Z\\right]a+e$\n", + "\n", + "where $a=\\left[\\begin{array}{c}\n", + " a_{0}\\\\\n", + " a_{1}\\\\\n", + " a_{2}\\end{array}\\right]$\n", + " \n", + "$[Z]=\\left[\\begin{array} \n", + "1 & x_{1} & x_{1}^{2} \\\\\n", + "1 & x_{2} & x_{2}^{2} \\\\\n", + "1 & x_{3} & x_{3}^{2} \\\\\n", + "1 & x_{4} & x_{4}^{2} \\\\\n", + "1 & x_{5} & x_{5}^{2} \\\\\n", + "1 & x_{6} & x_{6}^{2} \\\\\n", + "1 & x_{7} & x_{7}^{2} \\\\\n", + "1 & x_{8} & x_{8}^{2} \\\\\n", + "1 & x_{9} & x_{9}^{2} \\\\\n", + "1 & x_{10} & x_{10}^{2} \\end{array}\\right]$\n", + "\n", + "The sum of squares residuals for this model is\n", + "\n", + "$S_{r}=\\sum_{i=1}^{n}\\left(y_{i}-\\sum_{j=0}^{m}a_{j}z_{ji}\\right)$\n", + "\n", + "Minimizing this function results in\n", + "\n", + "$y=[Z]a$\n", + "\n", + "->**A standard Linear Algebra Problem**\n", + "\n", + "*the vector a is unknown, and Z is calculated based upon the assumed function*\n" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Z =\n", + "\n", + " 1 0 0\n", + " 1 2 4\n", + " 1 4 16\n", + " 1 6 36\n", + " 1 8 64\n", + " 1 10 100\n", + " 1 12 144\n", + " 1 14 196\n", + " 1 16 256\n", + " 1 18 324\n", + " 1 20 400\n", + "\n", + "a =\n", + "\n", + " 21.40341\n", + " -0.81538\n", + " 0.23935\n", + "\n" + ] + } + ], + "source": [ + "Z=[x.^0,x,x.^2]\n", + "\n", + "a=Z\\y" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t20\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t40\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t60\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t80\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t120\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\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", + "\n", + "\t\n", + "\tgnuplot_plot_2a\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "x_fcn=linspace(min(x),max(x));\n", + "plot(x,y,'o',x_fcn,a(1)+a(2)*x_fcn+a(3)*x_fcn.^2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### General Coefficient of Determination\n", + "\n", + "$r^{2}=\\frac{S_{t}-S_{r}}{S_{t}}=1-\\frac{S_{r}}{S_t}$" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "St = 27.923\n", + "Sr = 2.6366\n" + ] + } + ], + "source": [ + "St=std(y)\n", + "Sr=std(y-a(1)-a(2)*x-a(3)*x.^2)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the coefficient of determination for this fit is 0.880485\n", + "the correlation coefficient this fit is 0.938342\n" + ] + } + ], + "source": [ + "r2=1-Sr/St;\n", + "r=sqrt(r2);\n", + "\n", + "fprintf('the coefficient of determination for this fit is %f',r2)\n", + "fprintf('the correlation coefficient this fit is %f',r)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Compare this to a straight line fit" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "St = 27.923\n", + "Sr = 9.2520\n", + "the coefficient of determination for this fit is 0.668655\n", + "the correlation coefficient this fit is 0.817713\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\t20\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t40\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t60\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t80\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t120\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\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", + "\n", + "\t\n", + "\tgnuplot_plot_2a\n", + "\n", + "\t\n", + "\t\n", + "\tgnuplot_plot_3a\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "Z=[ones(size(x)) x];\n", + "a_line=Z\\y;\n", + "x_fcn=linspace(min(x),max(x));\n", + "plot(x,y,'o',x_fcn,a(1)+a(2)*x_fcn+a(3)*x_fcn.^2,...\n", + "x_fcn,a_line(1)+a_line(2)*x_fcn)\n", + "St=std(y)\n", + "Sr=std(y-a_line(1)-a_line(2)*x)\n", + "r2=1-Sr/St;\n", + "r=sqrt(r2);\n", + "\n", + "fprintf('the coefficient of determination for this fit is %f',r2)\n", + "fprintf('the correlation coefficient this fit is %f',r)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tresiduals of straight line\n", + "\t\n", + "\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot(x,y-a_line(1)-a_line(2)*x)\n", + "title('residuals of straight line')" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\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\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tresiduals of parabola\n", + "\t\n", + "\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot(x,y-a(1)-a(2)*x-a(3)*x.^2)\n", + "title('residuals of parabola')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warning \n", + "**Coefficient of determination reduction does not always mean a better fit**\n", + "\n", + "Try the function, \n", + "\n", + "$y(x)=a_0+a_{1}x+a_{2}x^{2}+a_{4}x^{4}+a_{5}x^{5}+a_{5}x^{5}+a_{6}x^{6}+a_{7}x^{7}+a_{8}x^{8}$" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a_overfit =\n", + "\n", + " 2.1487e+01\n", + " -1.4264e+01\n", + " 1.5240e+01\n", + " -6.0483e+00\n", + " 1.1887e+00\n", + " -1.2651e-01\n", + " 7.4379e-03\n", + " -2.2702e-04\n", + " 2.8063e-06\n", + "\n" + ] + } + ], + "source": [ + "Z=[ones(size(x)) x x.^2 x.^3 x.^4 x.^5 x.^6 x.^7 x.^8];\n", + "a_overfit=Z\\y" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t20\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t40\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t60\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t80\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t120\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tdata\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tdata\n", + "\t\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", + "\n", + "\t\n", + "\tparabola\n", + "\n", + "\t\n", + "\t\tparabola\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tn=8-fit\n", + "\n", + "\t\n", + "\t\tn=8-fit\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot(x,y,'o',x_fcn,a(1)+a(2)*x_fcn+a(3)*x_fcn.^2,...\n", + "x_fcn,a_overfit(1)+a_overfit(2)*x_fcn+a_overfit(3)*x_fcn.^2+...\n", + "a_overfit(4)*x_fcn.^3+a_overfit(5)*x_fcn.^4+...\n", + "a_overfit(6)*x_fcn.^5+a_overfit(7)*x_fcn.^6+...\n", + "a_overfit(8)*x_fcn.^7+a_overfit(9)*x_fcn.^8)\n", + "legend('data','parabola','n=8-fit','Location','NorthWest')" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "St = 27.923\n", + "Sr = 0.77320\n", + "the coefficient of determination for this fit is 0.972309\n", + "the correlation coefficient this fit is 0.986057\n" + ] + } + ], + "source": [ + "St=std(y)\n", + "Sr=std(y-polyval(a_overfit(end:-1:1),x))\n", + "r2=1-Sr/St;\n", + "r=sqrt(r2);\n", + "\n", + "fprintf('the coefficient of determination for this fit is %f',r2)\n", + "fprintf('the correlation coefficient this fit is %f',r)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Linear Regression is only limited by the ability to separate the parameters from the function to achieve\n", + "\n", + "$y=[Z]a$\n", + "\n", + "$Z$ can be any function of the independent variable(s)\n", + "\n", + "**Example**:\n", + "We assume two functions are added together, sin(t) and sin(3t). What are the amplitudes?\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\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", + "\t\t\n", + "\t\t14\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "load sin_data.csv\n", + "t=sin_data(1,:)';\n", + "y=sin_data(2,:)';\n", + "plot(t,y)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a =\n", + "\n", + " 0.99727\n", + " 0.50251\n", + "\n" + ] + } + ], + "source": [ + "Z=[sin(t) sin(3*t)];\n", + "a=Z\\y" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\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", + "\t\t\n", + "\t\t14\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\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", + "\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", + "\tgnuplot_plot_2a\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot(t,y,'.',t,a(1)*sin(t)+a(2)*sin(3*t))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "anaconda-cloud": {}, + "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_17/lecture_17.ipynb b/lecture_17/lecture_17.ipynb index 35e61da..4d8aeeb 100644 --- a/lecture_17/lecture_17.ipynb +++ b/lecture_17/lecture_17.ipynb @@ -2687,6 +2687,7 @@ } ], "metadata": { + "anaconda-cloud": {}, "kernelspec": { "display_name": "Octave", "language": "octave", diff --git a/lecture_20/.ipynb_checkpoints/lecture_20-checkpoint.ipynb b/lecture_20/.ipynb_checkpoints/lecture_20-checkpoint.ipynb index 2fd6442..0dbea1b 100644 --- a/lecture_20/.ipynb_checkpoints/lecture_20-checkpoint.ipynb +++ b/lecture_20/.ipynb_checkpoints/lecture_20-checkpoint.ipynb @@ -1,6 +1,2198 @@ { - "cells": [], - "metadata": {}, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "setdefaults" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%plot --format svg" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Questions from last class\n", + "\n", + "The interp2 function uses splines to interpolate between data points. What are three options for interpolation:\n", + "\n", + "- cubic spline\n", + "\n", + "- piecewise cubic spline\n", + "\n", + "- linear spline\n", + "\n", + "- quadratic spline\n", + "\n", + "- fourth-order spline\n", + "\n", + "![q1](q1.png)\n", + "\n", + "Numerical integration is a general application of the Newton-Cotes formulas. What is the first order approximation of the Newton-Cotes formula? *\n", + "\n", + "- trapezoidal rule\n", + "\n", + "- Simpson's 1/3 rule\n", + "\n", + "- Simpson's 3/8 rule\n", + "\n", + "- linear approximation of integral\n", + "\n", + "- constant approximation of integral (sum(f(x)*dx))\n", + "\n", + "![q2](q2.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Questions from you\n", + "\n", + "- Is spring here to stay?\n", + " \n", + " - [Punsxatawney Phil](http://www.groundhog.org/)\n", + "\n", + "- The time is now.\n", + "\n", + "- Final Project Sheet?\n", + " \n", + " - coming this evening/tomorrow\n", + "\n", + "- can you provide some sort of hw answer key?\n", + " \n", + " - we can go through some of the HW \n", + "\n", + "- What's the most 1337 thing you've ever done?\n", + "\n", + " - sorry, I'm n00b to this\n", + "\n", + "- Can we do out more examples by hand (doc cam or drawing on computer notepad) instead of with pre-written code?\n", + "\n", + " - forthcoming\n", + "\n", + "- Favorite movie?\n", + " \n", + " - Big Lebowski\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Integrals in practice\n", + "\n", + "### Example: Compare toughness of two steels\n", + "\n", + "![Stress-strain plot of steel](steel_psi.jpg)\n", + "\n", + "Use the plot shown to determine the toughness of stainless steel and the toughness of structural steel.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "toughness of structural steel is 10.2 psi\n", + "toughness of stainless steel is 18.6 psi\n" + ] + } + ], + "source": [ + "fe_c=load('structural_steel_psi.jpg.dat');\n", + "fe_cr =load('stainless_steel_psi.jpg.dat');\n", + "\n", + "fe_c_toughness=trapz(fe_c(:,1),fe_c(:,2));\n", + "fe_cr_toughness=trapz(fe_cr(:,1),fe_cr(:,2));\n", + "\n", + "fprintf('toughness of structural steel is %1.1f psi\\n',fe_c_toughness)\n", + "fprintf('toughness of stainless steel is %1.1f psi',fe_cr_toughness)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Gauss Quadrature (for functions)\n", + "\n", + "Evaluating an integral, we assumed a polynomial form for each Newton-Cotes approximation.\n", + "\n", + "If we can evaluate the function at any point, it makes more sense to choose points more wisely rather than just using endpoints\n", + "\n", + "![trapezoidal example](trap_example.png)\n", + "\n", + "Let us set up two unknown constants, $c_{0}$ and $x_{0}$ and determine a *wise* place to evaluate f(x) such that \n", + "\n", + "$I=c_{0}f(x_{0})$\n", + "\n", + "and I is exact for polynomial of n=0, 1\n", + "\n", + "$\\int_{a}^{b}1dx=b-a=c_{0}$\n", + "\n", + "$\\int_{a}^{b}xdx=\\frac{b^2-a^2}{2}=c_{0}x_{0}$\n", + "\n", + "so $c_{0}=b-a$ and $x_{0}=\\frac{b+a}{2}$\n", + "\n", + "$I=\\int_{a}^{b}f(x)dx \\approx (b-a)f\\left(\\frac{b+a}{2}\\right)$\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "f1 =\n", + "\n", + "@(x) x + 1\n", + "\n", + "f2 =\n", + "\n", + "@(x) 1 / 2 * x .^ 2 + x + 1\n", + "\n", + "f3 =\n", + "\n", + "@(x) 1 / 6 * x .^ 3 + 1 / 2 * x .^ 2 + x\n", + "\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\t-1000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1500\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-20\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t15\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t20\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "f1=@(x) x+1\n", + "f2=@(x) 1/2*x.^2+x+1\n", + "f3=@(x) 1/6*x.^3+1/2*x.^2+x\n", + "plot(linspace(-18,18),f3(linspace(-18,18)))" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "integral of f1 from 2 to 3 = 3.500000\n", + "integral of f1 from 2 to 3 ~ 3.500000\n", + "integral of f2 from 2 to 3 = 6.666667\n", + "integral of f2 from 2 to 3 ~ 6.625000\n" + ] + } + ], + "source": [ + "fprintf('integral of f1 from 2 to 3 = %f',f2(3)-f2(2))\n", + "fprintf('integral of f1 from 2 to 3 ~ %f',(3-2)*f1(3/2+2/2))\n", + "\n", + "fprintf('integral of f2 from 2 to 3 = %f',f3(3)-f3(2))\n", + "fprintf('integral of f2 from 2 to 3 ~ %f',(3-2)*f2(3/2+2/2))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This process is called **Gauss Quadrature**. Usually, the bounds are fixed at -1 and 1 instead of a and b\n", + "\n", + "$I=c_{0}f(x_{0})$\n", + "\n", + "and I is exact for polynomial of n=0, 1\n", + "\n", + "$\\int_{-1}^{1}1dx=b-a=c_{0}$\n", + "\n", + "$\\int_{-1}^{1}xdx=\\frac{1^2-(-1)^2}{2}=c_{0}x_{0}$\n", + "\n", + "so $c_{0}=2$ and $x_{0}=0$\n", + "\n", + "$I=\\int_{-1}^{1}f(x)dx \\approx 2f\\left(0\\right)$\n", + "\n", + "Now, integrals can be performed with a change of variable\n", + "\n", + "a=2\n", + "\n", + "b=3\n", + "\n", + "x= 2 to 3\n", + "\n", + "or $x_{d}=$ -1 to 1\n", + "\n", + "$x=a_{1}+a_{2}x_{d}$\n", + "\n", + "at $x_{d}=-1$, x=a\n", + "\n", + "at $x_{d}=1$, x=b\n", + "\n", + "so \n", + "\n", + "$x=\\frac{(b+a) +(b-a)x_{d}}{2}$\n", + "\n", + "$dx=\\frac{b-a}{2}dx_{d}$\n", + "\n", + "$\\int_{2}^{3}x+1dx=\\int_{-1}^{1}\\left(\\frac{(2+3) +(3-2)x_{d}}{2}\n", + "+1\\right)\n", + "\\frac{3-2}{2}dx_{d}$\n", + "\n", + "$\\int_{2}^{3}x+1dx=\\int_{-1}^{1}\\left(\\frac{5 +x_{d}}{2}\n", + "+1\\right)\n", + "\\frac{3-2}{2}dx_{d}$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "$\\int_{2}^{3}x+1dx=\\int_{-1}^{1}\\left(\\frac{7}{4}+\\frac{1}{4}x_{d}\\right)dx_{d}=2\\frac{7}{4}=3.5$" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "function I=gauss_1pt(func,a,b)\n", + " % Gauss quadrature using single point\n", + " % exact for n<1 polynomials\n", + " c0=2;\n", + " xd=0;\n", + " dx=(b-a)/2;\n", + " x=(b+a)/2+(b-a)/2*xd;\n", + " I=func(x).*dx*c0;\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 3.5000\r\n" + ] + } + ], + "source": [ + "gauss_1pt(f1,2,3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## General Gauss weights and points\n", + "\n", + "![Gauss quadrature table](gauss_weights.png)\n", + "\n", + "### If you need to evaluate an integral, to increase accuracy, increase number of Gauss points\n", + "\n", + "### Adaptive Quadrature\n", + "\n", + "Matlab/Octave built-in functions use two types of adaptive quadrature to increase accuracy of integrals of functions. \n", + "\n", + "1. `quad`: Simpson quadrature good for nonsmooth functions\n", + "\n", + "2. `quadl`: Lobatto quadrature good for smooth functions\n", + "\n", + "```matlab\n", + "q = quad(fun, a, b, tol, trace, p1, p2, …)\n", + "fun : function to be integrates\n", + "a, b: integration bounds\n", + "tol: desired absolute tolerance (default: 10-6)\n", + "trace: flag to display details or not\n", + "p1, p2, …: extra parameters for fun\n", + "quadl has the same arguments\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 6.6667\n", + "ans = 6.6667\n", + "ans = 6.6667\n" + ] + } + ], + "source": [ + "% integral of quadratic\n", + "quad(f2,2,3)\n", + "quadl(f2,2,3)\n", + "f3(3)-f3(2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Numerical Differentiation\n", + "\n", + "Expanding the Taylor Series:\n", + "\n", + "$f(x_{i+1})=f(x_{i})+f'(x_{i})h+\\frac{f''(x_{i})}{2!}h^2+\\cdots$" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tLow noise in sin wave\n", + "\t\n", + "\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tgnuplot_plot_2a\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "x=linspace(-pi,pi);\n", + "y_smooth=sin(x);\n", + "y_noise =y_smooth+rand(size(x))*0.1-0.05;\n", + "plot(x,y_smooth,x,y_noise)\n", + "title('Low noise in sin wave')" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tNoise Amplified with derivative\n", + "\t\n", + "\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tgnuplot_plot_2a\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "% Central Difference derivative\n", + "\n", + "dy_smooth=zeros(size(x));\n", + "dy_smooth([1,end])=NaN;\n", + "dy_smooth(2:end-1)=(y_smooth(3:end)-y_smooth(1:end-2))/2/(x(2)-x(1));\n", + "\n", + "dy_noise=zeros(size(x));\n", + "dy_noise([1,end])=NaN;\n", + "dy_noise(2:end-1)=(y_noise(3:end)-y_noise(1:end-2))/2/(x(2)-x(1));\n", + "\n", + "plot(x,dy_smooth,x,dy_noise)\n", + "title('Noise Amplified with derivative')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Reduce noise\n", + "\n", + "Options:\n", + "\n", + "1. Fit a function and take derivative\n", + " \n", + " a. splines won't help much\n", + " \n", + " b. best fit curve (better)\n", + " \n", + "2. Smooth data (does not matter if you smooth before/after derivative)" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tderiv of spline\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tderiv of spline\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tno change\n", + "\n", + "\t\n", + "\t\tno change\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "y_spline_i1=interp1(x,y_noise,x+0.1);\n", + "y_spline_in1=interp1(x,y_noise,x-0.1);\n", + "dy_spline=(y_spline_i1-y_spline_in1)/0.2;\n", + "plot(x,dy_spline,x,dy_noise)\n", + "legend('deriv of spline','no change')" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a = 0.99838\r\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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\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\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "Z=[sin(x')];\n", + "a=Z\\y_noise'\n", + "plot(x,a*sin(x),x,y_noise)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\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 \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": [ + "plot(x,a*cos(x),x,dy_smooth,'o')" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a =\n", + "\n", + " 1 2 3 4 5\n", + "\n", + "pa =\n", + "\n", + " 2 1 1 2 3 4 5 5 4\n", + "\n", + "ans =\n", + "\n", + " 1 9\n", + "\n" + ] + } + ], + "source": [ + "a=[1,2,3,4,5]\n", + "pa=[a(4/2:-1:1) a a(end:-1:end-4/2+1)]\n", + "size(pa)" + ] + }, + { + "cell_type": "code", + "execution_count": 137, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans =\n", + "\n", + " 1 100\n", + "\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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\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 \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": [ + "% Smooth data\n", + "N=10; % average data between 10 points (forward/backward)\n", + "y_data=[y_noise(N/2:-1:1) y_noise y_noise(end:-1:end-N/2+1)];\n", + "y_filter=y_data;\n", + "for i=6:length(x)\n", + " y_filter(i)=mean(y_data(i-5:i+5));\n", + "end\n", + "y_filter=y_filter(N/2:end-N/2-1);\n", + "size(y_filter)\n", + "plot(x,y_filter,x,y_noise,'.')" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "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\t-1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t3\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t4\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\t\n", + "\t\tNoise Amplified with derivative\n", + "\t\n", + "\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tgnuplot_plot_2a\n", + "\n", + "\t\n", + "\t\n", + "\tgnuplot_plot_3a\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", + "\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": [ + "dy_filter=zeros(size(x));\n", + "dy_filter([1,end])=NaN;\n", + "dy_filter(2:end-1)=(y_filter(3:end)-y_filter(1:end-2))/2/(x(2)-x(1));\n", + "\n", + "plot(x,dy_smooth,x,dy_filter,x,dy_noise,'.')\n", + "title('Noise Amplified with derivative')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "anaconda-cloud": {}, + "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_20/lecture_20.html b/lecture_20/lecture_20.html new file mode 100644 index 0000000..5a7a4c6 --- /dev/null +++ b/lecture_20/lecture_20.html @@ -0,0 +1,13993 @@ + + + +lecture_20 + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
In [1]:
+
+
+
setdefaults
+
+ +
+
+
+ +
+
+
+
In [2]:
+
+
+
%plot --format svg
+
+ +
+
+
+ +
+
+
+
+
+
+

Questions from last class

The interp2 function uses splines to interpolate between data points. What are three options for interpolation:

+
    +
  • cubic spline

    +
  • +
  • piecewise cubic spline

    +
  • +
  • linear spline

    +
  • +
  • quadratic spline

    +
  • +
  • fourth-order spline

    +
  • +
+

q1

+

Numerical integration is a general application of the Newton-Cotes formulas. What is the first order approximation of the Newton-Cotes formula? *

+
    +
  • trapezoidal rule

    +
  • +
  • Simpson's 1/3 rule

    +
  • +
  • Simpson's 3/8 rule

    +
  • +
  • linear approximation of integral

    +
  • +
  • constant approximation of integral (sum(f(x)*dx))

    +
  • +
+

q2

+ +
+
+
+
+
+
+
+
+

Questions from you

    +
  • Is spring here to stay?

    + +
  • +
  • The time is now.

    +
  • +
  • Final Project Sheet?

    +
      +
    • coming this evening/tomorrow
    • +
    +
  • +
  • can you provide some sort of hw answer key?

    +
      +
    • we can go through some of the HW
    • +
    +
  • +
  • What's the most 1337 thing you've ever done?

    +
      +
    • sorry, I'm n00b to this
    • +
    +
  • +
  • Can we do out more examples by hand (doc cam or drawing on computer notepad) instead of with pre-written code?

    +
      +
    • forthcoming
    • +
    +
  • +
  • Favorite movie?

    +
      +
    • Big Lebowski
    • +
    +
  • +
+ +
+
+
+
+
+
+
+
+

Integrals in practice

Example: Compare toughness of two steels

Stress-strain plot of steel

+

Use the plot shown to determine the toughness of stainless steel and the toughness of structural steel.

+ +
+
+
+
+
+
In [49]:
+
+
+
fe_c=load('structural_steel_psi.jpg.dat');
+fe_cr =load('stainless_steel_psi.jpg.dat');
+
+fe_c_toughness=trapz(fe_c(:,1),fe_c(:,2));
+fe_cr_toughness=trapz(fe_cr(:,1),fe_cr(:,2));
+
+fprintf('toughness of structural steel is %1.1f psi\n',fe_c_toughness)
+fprintf('toughness of stainless steel is %1.1f psi',fe_cr_toughness)
+
+ +
+
+
+ +
+
+ + +
+
+
toughness of structural steel is 10.2 psi
+toughness of stainless steel is 18.6 psi
+
+
+
+ +
+
+ +
+
+
+
+
+
+

Gauss Quadrature (for functions)

Evaluating an integral, we assumed a polynomial form for each Newton-Cotes approximation.

+

If we can evaluate the function at any point, it makes more sense to choose points more wisely rather than just using endpoints

+

trapezoidal example

+

Let us set up two unknown constants, $c_{0}$ and $x_{0}$ and determine a wise place to evaluate f(x) such that

+

$I=c_{0}f(x_{0})$

+

and I is exact for polynomial of n=0, 1

+

$\int_{a}^{b}1dx=b-a=c_{0}$

+

$\int_{a}^{b}xdx=\frac{b^2-a^2}{2}=c_{0}x_{0}$

+

so $c_{0}=b-a$ and $x_{0}=\frac{b+a}{2}$

+

$I=\int_{a}^{b}f(x)dx \approx (b-a)f\left(\frac{b+a}{2}\right)$

+ +
+
+
+
+
+
In [147]:
+
+
+
f1=@(x) x+1
+f2=@(x) 1/2*x.^2+x+1
+f3=@(x) 1/6*x.^3+1/2*x.^2+x
+plot(linspace(-18,18),f3(linspace(-18,18)))
+
+ +
+
+
+ +
+
+ + +
+
+
f1 =
+
+@(x) x + 1
+
+f2 =
+
+@(x) 1 / 2 * x .^ 2 + x + 1
+
+f3 =
+
+@(x) 1 / 6 * x .^ 3 + 1 / 2 * x .^ 2 + x
+
+
+
+
+ +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1000 + + + + + -500 + + + + + 0 + + + + + 500 + + + + + 1000 + + + + + 1500 + + + + + -20 + + + + + -15 + + + + + -10 + + + + + -5 + + + + + 0 + + + + + 5 + + + + + 10 + + + + + 15 + + + + + 20 + + + + + + + + + gnuplot_plot_1a + + + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [148]:
+
+
+
fprintf('integral of f1 from 2 to 3 = %f',f2(3)-f2(2))
+fprintf('integral of f1 from 2 to 3 ~ %f',(3-2)*f1(3/2+2/2))
+
+fprintf('integral of f2 from 2 to 3 = %f',f3(3)-f3(2))
+fprintf('integral of f2 from 2 to 3 ~ %f',(3-2)*f2(3/2+2/2))
+
+ +
+
+
+ +
+
+ + +
+
+
integral of f1 from 2 to 3 = 3.500000
+integral of f1 from 2 to 3 ~ 3.500000
+integral of f2 from 2 to 3 = 6.666667
+integral of f2 from 2 to 3 ~ 6.625000
+
+
+
+ +
+
+ +
+
+
+
+
+
+

This process is called Gauss Quadrature. Usually, the bounds are fixed at -1 and 1 instead of a and b

+

$I=c_{0}f(x_{0})$

+

and I is exact for polynomial of n=0, 1

+

$\int_{-1}^{1}1dx=b-a=c_{0}$

+

$\int_{-1}^{1}xdx=\frac{1^2-(-1)^2}{2}=c_{0}x_{0}$

+

so $c_{0}=2$ and $x_{0}=0$

+

$I=\int_{-1}^{1}f(x)dx \approx 2f\left(0\right)$

+

Now, integrals can be performed with a change of variable

+

a=2

+

b=3

+

x= 2 to 3

+

or $x_{d}=$ -1 to 1

+

$x=a_{1}+a_{2}x_{d}$

+

at $x_{d}=-1$, x=a

+

at $x_{d}=1$, x=b

+

so

+

$x=\frac{(b+a) +(b-a)x_{d}}{2}$

+

$dx=\frac{b-a}{2}dx_{d}$

+

$\int_{2}^{3}x+1dx=\int_{-1}^{1}\left(\frac{(2+3) +(3-2)x_{d}}{2} ++1\right) +\frac{3-2}{2}dx_{d}$

+

$\int_{2}^{3}x+1dx=\int_{-1}^{1}\left(\frac{5 +x_{d}}{2} ++1\right) +\frac{3-2}{2}dx_{d}$

+ +
+
+
+
+
+
+
+
+

$\int_{2}^{3}x+1dx=\int_{-1}^{1}\left(\frac{7}{4}+\frac{1}{4}x_{d}\right)dx_{d}=2\frac{7}{4}=3.5$

+ +
+
+
+
+
+
In [15]:
+
+
+
function I=gauss_1pt(func,a,b)
+    % Gauss quadrature using single point
+    % exact for n<1 polynomials
+    c0=2;
+    xd=0;
+    dx=(b-a)/2;
+    x=(b+a)/2+(b-a)/2*xd;
+    I=func(x).*dx*c0;
+end
+
+ +
+
+
+ +
+
+
+
In [13]:
+
+
+
gauss_1pt(f1,2,3)
+
+ +
+
+
+ +
+
+ + +
+
+
ans =  3.5000

+
+
+
+ +
+
+ +
+
+
+
+
+
+

General Gauss weights and points

Gauss quadrature table

+

If you need to evaluate an integral, to increase accuracy, increase number of Gauss points

Adaptive Quadrature

Matlab/Octave built-in functions use two types of adaptive quadrature to increase accuracy of integrals of functions.

+
    +
  1. quad: Simpson quadrature good for nonsmooth functions

    +
  2. +
  3. quadl: Lobatto quadrature good for smooth functions

    +
  4. +
+
q = quad(fun, a, b, tol, trace, p1, p2, …)
+fun : function to be integrates
+a, b: integration bounds
+tol: desired absolute tolerance (default: 10-6)
+trace: flag to display details or not
+p1, p2, …: extra parameters for fun
+quadl has the same arguments
+
+ +
+
+
+
+
+
In [149]:
+
+
+
% integral of quadratic
+quad(f2,2,3)
+quadl(f2,2,3)
+f3(3)-f3(2)
+
+ +
+
+
+ +
+
+ + +
+
+
ans =  6.6667
+ans =  6.6667
+ans =  6.6667
+
+
+
+ +
+
+ +
+
+
+
+
+
+

Numerical Differentiation

Expanding the Taylor Series:

+

$f(x_{i+1})=f(x_{i})+f'(x_{i})h+\frac{f''(x_{i})}{2!}h^2+\cdots$

+ +
+
+
+
+
+
In [82]:
+
+
+
x=linspace(-pi,pi);
+y_smooth=sin(x);
+y_noise =y_smooth+rand(size(x))*0.1-0.05;
+plot(x,y_smooth,x,y_noise)
+title('Low noise in sin wave')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1.5 + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + 1.5 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + Low noise in sin wave + + + + + gnuplot_plot_1a + + + + + + gnuplot_plot_2a + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [98]:
+
+
+
% Central Difference derivative
+
+dy_smooth=zeros(size(x));
+dy_smooth([1,end])=NaN;
+dy_smooth(2:end-1)=(y_smooth(3:end)-y_smooth(1:end-2))/2/(x(2)-x(1));
+
+dy_noise=zeros(size(x));
+dy_noise([1,end])=NaN;
+dy_noise(2:end-1)=(y_noise(3:end)-y_noise(1:end-2))/2/(x(2)-x(1));
+
+plot(x,dy_smooth,x,dy_noise)
+title('Noise Amplified with derivative')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1.5 + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + 1.5 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + Noise Amplified with derivative + + + + + gnuplot_plot_1a + + + + + + gnuplot_plot_2a + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
+
+
+

Reduce noise

Options:

+
    +
  1. Fit a function and take derivative

    +

    a. splines won't help much

    +

    b. best fit curve (better)

    +
  2. +
  3. Smooth data (does not matter if you smooth before/after derivative)

    +
  4. +
+ +
+
+
+
+
+
In [99]:
+
+
+
y_spline_i1=interp1(x,y_noise,x+0.1);
+y_spline_in1=interp1(x,y_noise,x-0.1);
+dy_spline=(y_spline_i1-y_spline_in1)/0.2;
+plot(x,dy_spline,x,dy_noise)
+legend('deriv of spline','no change')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1.5 + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + 1.5 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + + + + + deriv of spline + + + + + deriv of spline + + + + + + no change + + + no change + + + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [100]:
+
+
+
Z=[sin(x')];
+a=Z\y_noise'
+plot(x,a*sin(x),x,y_noise)
+
+ +
+
+
+ +
+
+ + +
+
+
a =  0.99838

+
+
+
+ +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1.5 + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + 1.5 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + gnuplot_plot_1a + + + + + + gnuplot_plot_2a + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [103]:
+
+
+
plot(x,a*cos(x),x,dy_smooth,'o')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + gnuplot_plot_1a + + + + + + gnuplot_plot_2a + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [120]:
+
+
+
a=[1,2,3,4,5]
+pa=[a(4/2:-1:1) a a(end:-1:end-4/2+1)]
+size(pa)
+
+ +
+
+
+ +
+
+ + +
+
+
a =
+
+   1   2   3   4   5
+
+pa =
+
+   2   1   1   2   3   4   5   5   4
+
+ans =
+
+   1   9
+
+
+
+
+ +
+
+ +
+
+
+
In [137]:
+
+
+
% Smooth data
+N=10; % average data between 10 points (forward/backward)
+y_data=[y_noise(N/2:-1:1) y_noise y_noise(end:-1:end-N/2+1)];
+y_filter=y_data;
+for i=6:length(x)
+    y_filter(i)=mean(y_data(i-5:i+5));
+end
+y_filter=y_filter(N/2:end-N/2-1);
+size(y_filter)
+plot(x,y_filter,x,y_noise,'.')
+
+ +
+
+
+ +
+
+ + +
+
+
ans =
+
+     1   100
+
+
+
+
+ +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1.5 + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + 1.5 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + gnuplot_plot_1a + + + + + + gnuplot_plot_2a + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [138]:
+
+
+
dy_filter=zeros(size(x));
+dy_filter([1,end])=NaN;
+dy_filter(2:end-1)=(y_filter(3:end)-y_filter(1:end-2))/2/(x(2)-x(1));
+
+plot(x,dy_smooth,x,dy_filter,x,dy_noise,'.')
+title('Noise Amplified with derivative')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -1.5 + + + + + -1 + + + + + -0.5 + + + + + 0 + + + + + 0.5 + + + + + 1 + + + + + 1.5 + + + + + -4 + + + + + -3 + + + + + -2 + + + + + -1 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + + + + + Noise Amplified with derivative + + + + + gnuplot_plot_1a + + + + + + gnuplot_plot_2a + + + + gnuplot_plot_3a + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ +
+
+
+
In [ ]:
+
+
+

+
+ +
+
+
+ +
+
+
+ + diff --git a/lecture_20/lecture_20.ipynb b/lecture_20/lecture_20.ipynb index da8c4ac..639f684 100644 --- a/lecture_20/lecture_20.ipynb +++ b/lecture_20/lecture_20.ipynb @@ -2169,6 +2169,7 @@ } ], "metadata": { + "anaconda-cloud": {}, "kernelspec": { "display_name": "Octave", "language": "octave", diff --git a/lecture_20/lecture_20.pdf b/lecture_20/lecture_20.pdf new file mode 100644 index 0000000..3b6d6ca Binary files /dev/null and b/lecture_20/lecture_20.pdf differ