diff --git a/lecture_07/.ipynb_checkpoints/lecture_07-checkpoint.ipynb b/lecture_07/.ipynb_checkpoints/lecture_07-checkpoint.ipynb new file mode 100644 index 0000000..d7ff495 --- /dev/null +++ b/lecture_07/.ipynb_checkpoints/lecture_07-checkpoint.ipynb @@ -0,0 +1,1042 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%plot --format svg" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "setdefaults" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Roots: Open methods\n", + "## Newton-Raphson" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First-order approximation for the location of the root (i.e. assume the slope at the given point is constant, what is the solution when f(x)=0)\n", + "\n", + "$f'(x_{i})=\\frac{f(x_{i})-0}{x_{i}-x_{i+1}}$\n", + "\n", + "$x_{i+1}=x_{i}-\\frac{f(x_{i})}{f'(x_{i})}$\n", + "\n", + "Use Newton-Raphson to find solution when $e^{x}=x$" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "error_approx = 1\r\n" + ] + } + ], + "source": [ + "f= @(x) exp(-x)-x;\n", + "df= @(x) -exp(-x)-1;\n", + "\n", + "x_i= 0;\n", + "error_approx = abs((x_r-x_i)/x_r)\n", + "x_r=x_i;\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x_r = 0.50000\n", + "error_approx = 1\n" + ] + } + ], + "source": [ + "x_r = x_i-f(x_i)/df(x_i)\n", + "error_approx = abs((x_r-x_i)/x_r)\n", + "x_i=x_r;" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x_r = 0.56631\n", + "error_approx = 0.11709\n" + ] + } + ], + "source": [ + "x_r = x_i-f(x_i)/df(x_i)\n", + "error_approx = abs((x_r-x_i)/x_r)\n", + "x_i=x_r;" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x_r = 0.56714\n", + "error_approx = 0.0014673\n" + ] + } + ], + "source": [ + "x_r = x_i-f(x_i)/df(x_i)\n", + "error_approx = abs((x_r-x_i)/x_r)\n", + "x_i=x_r;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the bungee jumper example, we created a function f(m) that when f(m)=0, then the mass had been chosen such that at t=4 s, the velocity is 36 m/s. \n", + "\n", + "$f(m)=\\sqrt{\\frac{gm}{c_{d}}}\\tanh(\\sqrt{\\frac{gc_{d}}{m}}t)-v(t)$.\n", + "\n", + "to use the Newton-Raphson method, we need the derivative $\\frac{df}{dm}$\n", + "\n", + "$\\frac{df}{dm}=\\frac{1}{2}\\sqrt{\\frac{g}{mc_{d}}}\\tanh(\\sqrt{\\frac{gc_{d}}{m}}t)-\n", + "\\frac{g}{2m}\\mathrm{sech}^{2}(\\sqrt{\\frac{gc_{d}}{m}}t)$" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "setdefaults\n", + "g=9.81; % acceleration due to gravity\n", + "m=linspace(50, 200,100); % possible values for mass 50 to 200 kg\n", + "c_d=0.25; % drag coefficient\n", + "t=4; % at time = 4 seconds\n", + "v=36; % speed must be 36 m/s\n", + "f_m = @(m) sqrt(g*m/c_d).*tanh(sqrt(g*c_d./m)*t)-v; % anonymous function f_m\n", + "df_m = @(m) 1/2*sqrt(g./m/c_d).*tanh(sqrt(g*c_d./m)*t)-g/2./m*sech(sqrt(g*c_d./m)*t).^2;" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 142.74\r\n" + ] + } + ], + "source": [ + "newtraph(f_m,df_m,140,0.00001)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Secant Methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Not always able to evaluate the derivative. Approximation of derivative:\n", + "\n", + "$f'(x_{i})=\\frac{f(x_{i-1})-f(x_{i})}{x_{i-1}-x_{i}}$\n", + "\n", + "$x_{i+1}=x_{i}-\\frac{f(x_{i})}{f'(x_{i})}$\n", + "\n", + "$x_{i+1}=x_{i}-\\frac{f(x_{i})}{\\frac{f(x_{i-1})-f(x_{i})}{x_{i-1}-x_{i}}}=\n", + " x_{i}-\\frac{f(x_{i})(x_{i-1}-x_{i})}{f(x_{i-1})-f(x_{i})}$\n", + " \n", + "What values should $x_{i}$ and $x_{i-1}$ take?\n", + "\n", + "To reduce arbitrary selection of variables, use the\n", + "\n", + "## Modified Secant method\n", + "\n", + "Change the x evaluations to a perturbation $\\delta$. \n", + "\n", + "$x_{i+1}=x_{i}-\\frac{f(x_{i})(\\delta x_{i})}{f(x_{i}+\\delta x_{i})-f(x_{i})}$" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 142.74\r\n" + ] + } + ], + "source": [ + "mod_secant(f_m,1e-6,50,0.00001)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "error: 'plot_bool' undefined near line 12 column 6\n", + "error: called from\n", + " car_payments at line 12 column 3\n", + " mod_secant at line 22 column 8\n", + "error: 'Amt' undefined near line 1 column 14\n", + "error: evaluating argument list element number 1\n" + ] + } + ], + "source": [ + "Amt_numerical=mod_secant(@(A) car_payments(A,30000,0.05,5),1e-6,50,0.001)\n", + "car_payments(Amt,30000,0.05,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "error: 'Amt' undefined near line 1 column 1\r\n" + ] + } + ], + "source": [ + "Amt*12*5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Amortization calculation makes the same calculation for the monthly payment amount, A, paying off the principle amount, P, over n pay periods with monthly interest rate, r. " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Amt = 566.14\r\n" + ] + } + ], + "source": [ + "% Amortization calculation\n", + "A = @(P,r,n) P*(r*(1+r)^n)./((1+r)^n-1);\n", + "Amt=A(30000,0.05/12,5*12)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Matlab's function\n", + "\n", + "Matlab and Octave combine bracketing and open methods in the `fzero` function. " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'fzero' is a function from the file /usr/share/octave/4.0.0/m/optimization/fzero.m\n", + "\n", + " -- Function File: fzero (FUN, X0)\n", + " -- Function File: fzero (FUN, X0, OPTIONS)\n", + " -- Function File: [X, FVAL, INFO, OUTPUT] = fzero (...)\n", + " Find a zero of a univariate function.\n", + "\n", + " FUN is a function handle, inline function, or string containing the\n", + " name of the function to evaluate.\n", + "\n", + " X0 should be a two-element vector specifying two points which\n", + " bracket a zero. In other words, there must be a change in sign of\n", + " the function between X0(1) and X0(2). More mathematically, the\n", + " following must hold\n", + "\n", + " sign (FUN(X0(1))) * sign (FUN(X0(2))) <= 0\n", + "\n", + " If X0 is a single scalar then several nearby and distant values are\n", + " probed in an attempt to obtain a valid bracketing. If this is not\n", + " successful, the function fails.\n", + "\n", + " OPTIONS is a structure specifying additional options. Currently,\n", + " 'fzero' recognizes these options: \"FunValCheck\", \"OutputFcn\",\n", + " \"TolX\", \"MaxIter\", \"MaxFunEvals\". For a description of these\n", + " options, see *note optimset: XREFoptimset.\n", + "\n", + " On exit, the function returns X, the approximate zero point and\n", + " FVAL, the function value thereof.\n", + "\n", + " INFO is an exit flag that can have these values:\n", + "\n", + " * 1 The algorithm converged to a solution.\n", + "\n", + " * 0 Maximum number of iterations or function evaluations has\n", + " been reached.\n", + "\n", + " * -1 The algorithm has been terminated from user output\n", + " function.\n", + "\n", + " * -5 The algorithm may have converged to a singular point.\n", + "\n", + " OUTPUT is a structure containing runtime information about the\n", + " 'fzero' algorithm. Fields in the structure are:\n", + "\n", + " * iterations Number of iterations through loop.\n", + "\n", + " * nfev Number of function evaluations.\n", + "\n", + " * bracketx A two-element vector with the final bracketing of the\n", + " zero along the x-axis.\n", + "\n", + " * brackety A two-element vector with the final bracketing of the\n", + " zero along the y-axis.\n", + "\n", + " See also: optimset, fsolve.\n", + "\n", + "Additional help for built-in functions and operators is\n", + "available in the online version of the manual. Use the command\n", + "'doc ' to search the manual index.\n", + "\n", + "Help and information about Octave is also available on the WWW\n", + "at http://www.octave.org and via the help@octave.org\n", + "mailing list.\n" + ] + } + ], + "source": [ + "help fzero" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 563.79\r\n" + ] + } + ], + "source": [ + "fzero(@(A) car_payments(A,30000,0.05,5,0),500)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Comparison of Solvers\n", + "\n", + "It's helpful to compare to the convergence of different routines to see how quickly you find a solution. \n", + "\n", + "Comparing the freefall example\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "warning: axis: omitting non-positive data in log plot\n", + "warning: called from\n", + " __line__ at line 120 column 16\n", + " line at line 56 column 8\n", + " __plt__>__plt2vv__ at line 500 column 10\n", + " __plt__>__plt2__ at line 246 column 14\n", + " __plt__ at line 133 column 15\n", + " semilogy at line 60 column 10\n", + "warning: axis: omitting non-positive data in log plot\n", + "warning: axis: omitting non-positive data in log plot\n", + "warning: axis: omitting non-positive data in log plot\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\t10-14\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-12\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-8\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-6\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t102\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t104\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t50\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t150\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t200\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t250\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t300\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t350\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t400\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tnewton-raphson\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tnewton-raphson\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tmod-secant\n", + "\n", + "\t\n", + "\t\tmod-secant\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tfalse point\n", + "\n", + "\t\n", + "\t\tfalse point\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tbisection\n", + "\n", + "\t\n", + "\t\tbisection\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": [ + "N=20;\n", + "iterations = linspace(1,400,N);\n", + "ea_nr=zeros(1,N); % appr error Newton-Raphson\n", + "ea_ms=zeros(1,N); % appr error Modified Secant\n", + "ea_fp=zeros(1,N); % appr error false point method\n", + "ea_bs=zeros(1,N); % appr error bisect method\n", + "for i=1:length(iterations)\n", + " [root_nr,ea_nr(i),iter_nr]=newtraph(f_m,df_m,200,0,iterations(i));\n", + " [root_ms,ea_ms(i),iter_ms]=mod_secant(f_m,1e-6,300,0,iterations(i));\n", + " [root_fp,ea_fp(i),iter_fp]=falsepos(f_m,1,300,0,iterations(i));\n", + " [root_bs,ea_bs(i),iter_bs]=bisect(f_m,1,300,0,iterations(i));\n", + "end\n", + " \n", + "semilogy(iterations,abs(ea_nr),iterations,abs(ea_ms),iterations,abs(ea_fp),iterations,abs(ea_bs))\n", + "legend('newton-raphson','mod-secant','false point','bisection')" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ea_ms =\n", + "\n", + " Columns 1 through 6:\n", + "\n", + " 2.3382e+03 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", + "\n", + " Columns 7 through 12:\n", + "\n", + " 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", + "\n", + " Columns 13 through 18:\n", + "\n", + " 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", + "\n", + " Columns 19 and 20:\n", + "\n", + " 1.9171e-14 1.9171e-14\n", + "\n" + ] + } + ], + "source": [ + "ea_ms" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "warning: axis: omitting non-positive data in log plot\n", + "warning: called from\n", + " __line__ at line 120 column 16\n", + " line at line 56 column 8\n", + " __plt__>__plt2vv__ at line 500 column 10\n", + " __plt__>__plt2__ at line 246 column 14\n", + " __plt__ at line 133 column 15\n", + " semilogy at line 60 column 10\n", + "warning: axis: omitting non-positive data in log plot\n", + "warning: axis: omitting non-positive data in log plot\n", + "warning: axis: omitting non-positive data in log plot\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\t10-14\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-12\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-10\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-8\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-6\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t10-2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t100\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t102\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t104\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", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\tnewton-raphson\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\tnewton-raphson\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tmod-secant\n", + "\n", + "\t\n", + "\t\tmod-secant\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tfalse point\n", + "\n", + "\t\n", + "\t\tfalse point\n", + "\t\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\tbisection\n", + "\n", + "\t\n", + "\t\tbisection\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": [ + "N=20;\n", + "f= @(x) x^10-1;\n", + "df=@(x) 10*x^9;\n", + "iterations = linspace(1,50,N);\n", + "ea_nr=zeros(1,N); % appr error Newton-Raphson\n", + "ea_ms=zeros(1,N); % appr error Modified Secant\n", + "ea_fp=zeros(1,N); % appr error false point method\n", + "ea_bs=zeros(1,N); % appr error bisect method\n", + "for i=1:length(iterations)\n", + " [root_nr,ea_nr(i),iter_nr]=newtraph(f,df,0.5,0,iterations(i));\n", + " [root_ms,ea_ms(i),iter_ms]=mod_secant(f,1e-6,0.5,0,iterations(i));\n", + " [root_fp,ea_fp(i),iter_fp]=falsepos(f,0,5,0,iterations(i));\n", + " [root_bs,ea_bs(i),iter_bs]=bisect(f,0,5,0,iterations(i));\n", + "end\n", + " \n", + "semilogy(iterations,abs(ea_nr),iterations,abs(ea_ms),iterations,abs(ea_fp),iterations,abs(ea_bs))\n", + "legend('newton-raphson','mod-secant','false point','bisection')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ea_bs =\n", + "\n", + " Columns 1 through 6:\n", + "\n", + " 9.5357e+03 -4.7554e-01 -2.1114e-01 6.0163e-02 -2.4387e-03 6.1052e-04\n", + "\n", + " Columns 7 through 12:\n", + "\n", + " 2.2891e-04 -9.5367e-06 2.3842e-06 8.9407e-07 -2.2352e-07 9.3132e-09\n", + "\n", + " Columns 13 through 18:\n", + "\n", + " -2.3283e-09 -8.7311e-10 3.6380e-11 -9.0949e-12 -3.4106e-12 8.5265e-13\n", + "\n", + " Columns 19 and 20:\n", + "\n", + " -3.5527e-14 8.8818e-15\n", + "\n", + "ans = 16.208\n" + ] + } + ], + "source": [ + "ea_bs\n", + "newtraph(f,df,0.5,0,12)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 1.9683e+23\r\n" + ] + } + ], + "source": [ + "df(300)" + ] + }, + { + "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_07/lecture_07.ipynb b/lecture_07/lecture_07.ipynb index 0c3c2ca..d7ff495 100644 --- a/lecture_07/lecture_07.ipynb +++ b/lecture_07/lecture_07.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 25, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -45,7 +45,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -54,10 +54,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "error: 'c' undefined near line 1 column 1\n", - "error: 'x_r' undefined near line 1 column 21\n", - "error: evaluating argument list element number 1\n", - "error: 'x_r' undefined near line 1 column 5\n" + "error_approx = 1\r\n" ] } ], @@ -66,14 +63,13 @@ "df= @(x) -exp(-x)-1;\n", "\n", "x_i= 0;\n", - "c\n", "error_approx = abs((x_r-x_i)/x_r)\n", - "x_i=x_r;\n" + "x_r=x_i;\n" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -95,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 22, "metadata": { "collapsed": false }, @@ -117,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 23, "metadata": { "collapsed": false }, @@ -153,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -171,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -221,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -240,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -249,166 +245,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "Amt_numerical = 563.79\n", - "ans = -160.42\n" + "error: 'plot_bool' undefined near line 12 column 6\n", + "error: called from\n", + " car_payments at line 12 column 3\n", + " mod_secant at line 22 column 8\n", + "error: 'Amt' undefined near line 1 column 14\n", + "error: evaluating argument list element number 1\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-5000\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t0\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t5000\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t10000\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t15000\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t20000\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t25000\n", - "\t\n", - "\n", - "\n", - "\t\t\n", - "\t\t30000\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", - "\t\t\n", - "\t\t5\n", - "\t\n", - "\n", - "\n", - "\n", - "\n", - "\t\n", - "\n", - "\t\n", - "\t\tprinciple amount left ($)\n", - "\t\n", - "\n", - "\n", - "\t\n", - "\t\ttime (years)\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": [ @@ -418,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -427,7 +270,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "ans = 3.3968e+04\r\n" + "error: 'Amt' undefined near line 1 column 1\r\n" ] } ], @@ -444,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -474,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -555,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -585,7 +428,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -836,7 +679,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -847,17 +690,21 @@ "text": [ "ea_ms =\n", "\n", - " Columns 1 through 7:\n", + " Columns 1 through 6:\n", + "\n", + " 2.3382e+03 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", "\n", - " 43.43883 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000\n", + " Columns 7 through 12:\n", + "\n", + " 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", "\n", - " Columns 8 through 14:\n", + " Columns 13 through 18:\n", "\n", - " 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000\n", + " 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", "\n", - " Columns 15 through 20:\n", + " Columns 19 and 20:\n", "\n", - " 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000\n", + " 1.9171e-14 1.9171e-14\n", "\n" ] } @@ -868,7 +715,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -1106,7 +953,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 18, "metadata": { "collapsed": false }, @@ -1144,7 +991,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 19, "metadata": { "collapsed": false }, diff --git a/lecture_07/octave-workspace b/lecture_07/octave-workspace new file mode 100644 index 0000000..a214fe9 Binary files /dev/null and b/lecture_07/octave-workspace differ