diff --git a/HW3/README.html b/HW3/README.html new file mode 100644 index 0000000..950e6d3 --- /dev/null +++ b/HW3/README.html @@ -0,0 +1,60 @@ + + + + + + + + + + +

Homework #3

+

due 2/15/17 by 11:59pm

+
    +
  1. Create a new github repository called ‘roots_and_optimization’.

    +
      +
    1. Add rcc02007 and pez16103 as collaborators.

    2. +
    3. Clone the repository to your computer.

    4. +
    5. Copy your projectile.m function into the ‘roots_and_optimization’ folder. Disable the plotting routine for the solvers

    6. +
    7. Use the four solvers falsepos.m, incsearch.m, newtraph.m and mod_secant.m to solve for the angle needed to reach h=1.72, with an initial speed of 1.5 m/s.

    8. +
    9. The newtraph.m function needs a derivative, calculate the derivative of your function with respect to theta, dprojectile_dtheta.m. This function should output d(h)/d(theta).

    10. +
    11. In your README.md file, you will document the following under the heading # Homework #3:

      +
        +
      1. Compare the number of iterations that each function needed to reach an accuracy of 0.00001%. Include a table in your README.md with:
      2. +
      +
      | solver | initial guess(es) | ea | number of iterations|
      +| --- | --- | --- | --- |
      +|falsepos   |  |  |  |
      +|incsearch  |  |  |  |
      +|newtraph   |  |  |  |
      +|mod_secant |  |  |  |
      +
        +
      1. Compare the convergence of the 4 methods. Plot the approximate error vs the number of iterations that the solver has calculated. Save the plot as convergence.png and display the plot in your README.md with:
      2. +
      +

      ![Plot of convergence for four numerical solvers.](convergence.png)

      +
        +
      1. In the README.md provide a description of the files used to create the table and the convergence plot.
      2. +
    12. +
  2. +
  3. The Newton-Raphson method and the modified secant method do not always converge to a solution. One simple example is the function f(x) = x*exp(-x^2). The root is at 0, but using the numerical solvers, newtraph.m and mod_secant.m, there are certain initial guesses that do not converge.

    +
      +
    1. Calculate the first 5 iterations for the Newton-Raphson method with an initial guess of x_i=2.

    2. +
    3. Add the results to a table in the README.md with:

    4. +
    +
    ### divergence of Newton-Raphson method
    +
    +| iteration | x_i | approx error |
    +| --- | --- | --- |
    +| 0 | 2 | n/a |
    +| 1 |   |     |
    +| 2 |   |     |
    +| 3 |   |     |
    +| 4 |   |     |
    +| 5 |   |     |
    +
      +
    1. Repeat steps a-b for an initial guess of 0.2. (But change the heading from ‘divergence’ to ‘convergence’)
    2. +
  4. +
  5. Commit your changes to your repository. Sync your local repository with github. Then copy and paste the “clone URL” into the following Google Form Homework #3

  6. +
+ + diff --git a/HW3/README.md b/HW3/README.md new file mode 100644 index 0000000..494e0d5 --- /dev/null +++ b/HW3/README.md @@ -0,0 +1,74 @@ +# Homework #3 +## due 2/15/17 by 11:59pm + + +1. Create a new github repository called 'roots_and_optimization'. + + a. Add rcc02007 and pez16103 as collaborators. + + b. Clone the repository to your computer. + + c. Copy your `projectile.m` function into the 'roots_and_optimization' folder. + *Disable the plotting routine for the solvers* + + d. Use the four solvers `falsepos.m`, `incsearch.m`, `newtraph.m` and `mod_secant.m` + to solve for the angle needed to reach h=1.72 m, with an initial speed of 1.5 m/s. + + e. The `newtraph.m` function needs a derivative, calculate the derivative of your + function with respect to theta, `dprojectile_dtheta.m`. This function should + output d(h)/d(theta). + + + f. In your `README.md` file, document the following under the heading `# + Homework #3`: + + i. Compare the number of iterations that each function needed to reach an + accuracy of 0.00001%. Include a table in your README.md with: + + ``` + | solver | initial guess(es) | ea | number of iterations| + | --- | --- | --- | --- | + |falsepos | | | | + |incsearch | | | | + |newtraph | | | | + |mod_secant | | | | + ``` + + ii. Compare the convergence of the 4 methods. Plot the approximate error vs the + number of iterations that the solver has calculated. Save the plot as + `convergence.png` and display the plot in your `README.md` with: + + `![Plot of convergence for four numerical solvers.](convergence.png)` + + iii. In the `README.md` provide a description of the files used to create the + table and the convergence plot. + +2. The Newton-Raphson method and the modified secant method do not always converge to a +solution. One simple example is the function f(x) = x*exp(-x^2). The root is at 0, but +using the numerical solvers, `newtraph.m` and `mod_secant.m`, there are certain initial +guesses that do not converge. + + a. Calculate the first 5 iterations for the Newton-Raphson method with an initial + guess of x_i=2. + + b. Add the results to a table in the `README.md` with: + + ``` + ### divergence of Newton-Raphson method + + | iteration | x_i | approx error | + | --- | --- | --- | + | 0 | 2 | n/a | + | 1 | | | + | 2 | | | + | 3 | | | + | 4 | | | + | 5 | | | + ``` + + c. Repeat steps a-b for an initial guess of 0.2. (But change the heading from + 'divergence' to 'convergence') + +3. Commit your changes to your repository. Sync your local repository with github. Then +copy and paste the "clone URL" into the following Google Form [Homework +#3](https://goo.gl/forms/UJBGwp0fQcSxImkq2) diff --git a/lecture_07/.newtraph.m.swp b/lecture_07/.newtraph.m.swp new file mode 100644 index 0000000..9759dd2 Binary files /dev/null and b/lecture_07/.newtraph.m.swp differ diff --git a/lecture_07/lecture_07.ipynb b/lecture_07/lecture_07.ipynb index d7ff495..08d29c4 100644 --- a/lecture_07/lecture_07.ipynb +++ b/lecture_07/lecture_07.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 13, "metadata": { "collapsed": true }, @@ -40,12 +40,12 @@ "\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$" + "Use Newton-Raphson to find solution when $e^{-x}=x$" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -54,7 +54,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "error_approx = 1\r\n" + "x_r = 0.50000\n", + "error_approx = 1\n" ] } ], @@ -63,13 +64,14 @@ "df= @(x) -exp(-x)-1;\n", "\n", "x_i= 0;\n", + "x_r = x_i-f(x_i)/df(x_i)\n", "error_approx = abs((x_r-x_i)/x_r)\n", - "x_r=x_i;\n" + "x_i=x_r;\n" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -78,8 +80,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "x_r = 0.50000\n", - "error_approx = 1\n" + "x_r = 0.56631\n", + "error_approx = 0.11709\n" ] } ], @@ -91,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -100,8 +102,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "x_r = 0.56631\n", - "error_approx = 0.11709\n" + "x_r = 0.56714\n", + "error_approx = 0.0014673\n" ] } ], @@ -113,7 +115,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -123,7 +125,7 @@ "output_type": "stream", "text": [ "x_r = 0.56714\n", - "error_approx = 0.0014673\n" + "error_approx = 2.2106e-07\n" ] } ], @@ -149,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": { "collapsed": true }, @@ -176,12 +178,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "ans = 142.74\r\n" + "root = 142.74\n", + "ea = 8.0930e-06\n", + "iter = 48\n" ] } ], "source": [ - "newtraph(f_m,df_m,140,0.00001)" + "[root,ea,iter]=newtraph(f_m,df_m,140,0.00001)" ] }, { @@ -217,7 +221,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -226,17 +230,24 @@ "name": "stdout", "output_type": "stream", "text": [ - "ans = 142.74\r\n" + "root = 142.74\n", + "ea = 3.0615e-07\n", + "iter = 7\n" ] } ], "source": [ - "mod_secant(f_m,1e-6,50,0.00001)" + "[root,ea,iter]=mod_secant(f_m,1,50,0.00001)" ] }, + { + "cell_type": "raw", + "metadata": {}, + "source": [] + }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -245,23 +256,159 @@ "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" + "ans = 1.1185e+04\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\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": [ - "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)" + "car_payments(400,30000,0.05,5,1)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -270,12 +417,195 @@ "name": "stdout", "output_type": "stream", "text": [ - "error: 'Amt' undefined near line 1 column 1\r\n" + "Amt_numerical = 5467.0\n", + "ans = 3.9755e-04\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\t100000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t200000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t300000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t400000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t500000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t600000\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t700000\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", + "\t\t\n", + "\t\t25\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t30\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": [ + "Amt_numerical=mod_secant(@(A) car_payments(A,700000,0.0875,30,0),1e-6,50,0.001)\n", + "car_payments(Amt_numerical,700000,0.0875,30,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 1.9681e+06\r\n" ] } ], "source": [ - "Amt*12*5" + "Amt_numerical*12*30" ] }, { @@ -287,7 +617,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -398,7 +728,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -428,7 +758,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 23, "metadata": { "collapsed": false }, @@ -611,7 +941,7 @@ "\t\n", "\n", "\n", - "\t\n", + "\t\n", "\t\n", "\tmod-secant\n", "\n", @@ -667,19 +997,20 @@ "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_nr,ea_nr(i),iter_nr]=newtraph(f_m,df_m,300,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", + "\n", + "setdefaults\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, + "execution_count": 22, "metadata": { "collapsed": false }, @@ -688,34 +1019,30 @@ "name": "stdout", "output_type": "stream", "text": [ - "ea_ms =\n", - "\n", - " Columns 1 through 6:\n", + "ea_nr =\n", "\n", - " 2.3382e+03 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", + " Columns 1 through 8:\n", "\n", - " Columns 7 through 12:\n", + " 6.36591 0.06436 0.00052 0.00000 0.00000 0.00000 0.00000 0.00000\n", "\n", - " 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", + " Columns 9 through 16:\n", "\n", - " Columns 13 through 18:\n", + " 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000\n", "\n", - " 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14\n", + " Columns 17 through 20:\n", "\n", - " Columns 19 and 20:\n", - "\n", - " 1.9171e-14 1.9171e-14\n", + " 0.00000 0.00000 0.00000 0.00000\n", "\n" ] } ], "source": [ - "ea_ms" + "ea_nr" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 26, "metadata": { "collapsed": false }, @@ -953,7 +1280,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 27, "metadata": { "collapsed": false }, @@ -962,30 +1289,26 @@ "name": "stdout", "output_type": "stream", "text": [ - "ea_bs =\n", + "ea_nr =\n", "\n", - " Columns 1 through 6:\n", + " Columns 1 through 7:\n", "\n", - " 9.5357e+03 -4.7554e-01 -2.1114e-01 6.0163e-02 -2.4387e-03 6.1052e-04\n", + " 99.03195 11.11111 11.11111 11.11111 11.11111 11.11111 11.11111\n", "\n", - " Columns 7 through 12:\n", + " Columns 8 through 14:\n", "\n", - " 2.2891e-04 -9.5367e-06 2.3842e-06 8.9407e-07 -2.2352e-07 9.3132e-09\n", + " 11.11111 11.11111 11.11111 11.11109 11.11052 11.10624 10.99684\n", "\n", - " Columns 13 through 18:\n", + " Columns 15 through 20:\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", + " 8.76956 2.12993 0.00000 0.00000 0.00000 0.00000\n", "\n", "ans = 16.208\n" ] } ], "source": [ - "ea_bs\n", + "ea_nr\n", "newtraph(f,df,0.5,0,12)" ] }, @@ -1008,6 +1331,99 @@ "df(300)" ] }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "f =\n", + "\n", + "@(x) tan (x) - (x - 1) .^ 2\n", + "\n", + "ans = 0.37375\n" + ] + } + ], + "source": [ + "% our class function\n", + "f= @(x) tan(x)-(x-1).^2\n", + "mod_secant(f,1e-3,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = -3.5577e-13\r\n" + ] + } + ], + "source": [ + "f(ans)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 0.39218\n", + "ans = 0.39219\n" + ] + } + ], + "source": [ + "tan(0.37375)\n", + "(0.37375-1)^2" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans =\n", + "\n", + " Columns 1 through 8:\n", + "\n", + " -1.0000 1.5574 -3.1850 -4.1425 -7.8422 -19.3805 -25.2910 -35.1286\n", + "\n", + " Columns 9 through 11:\n", + "\n", + " -55.7997 -64.4523 -80.3516\n", + "\n" + ] + } + ], + "source": [ + "f([0:10])" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/lecture_07/lecture_07.md b/lecture_07/lecture_07.md index ffdc48d..3aacefb 100644 --- a/lecture_07/lecture_07.md +++ b/lecture_07/lecture_07.md @@ -18,7 +18,7 @@ $f'(x_{i})=\frac{f(x_{i})-0}{x_{i}-x_{i+1}}$ $x_{i+1}=x_{i}-\frac{f(x_{i})}{f'(x_{i})}$ -Use Newton-Raphson to find solution when $e^{x}=x$ +Use Newton-Raphson to find solution when $e^{-x}=x$ ```octave @@ -26,12 +26,14 @@ f= @(x) exp(-x)-x; df= @(x) -exp(-x)-1; x_i= 0; +x_r = x_i-f(x_i)/df(x_i) error_approx = abs((x_r-x_i)/x_r) -x_r=x_i; +x_i=x_r; ``` - error_approx = 1 + x_r = 0.50000 + error_approx = 1 @@ -41,8 +43,8 @@ error_approx = abs((x_r-x_i)/x_r) x_i=x_r; ``` - x_r = 0.50000 - error_approx = 1 + x_r = 0.56631 + error_approx = 0.11709 @@ -52,8 +54,8 @@ error_approx = abs((x_r-x_i)/x_r) x_i=x_r; ``` - x_r = 0.56631 - error_approx = 0.11709 + x_r = 0.56714 + error_approx = 0.0014673 @@ -64,7 +66,7 @@ x_i=x_r; ``` x_r = 0.56714 - error_approx = 0.0014673 + error_approx = 2.2106e-07 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. @@ -90,10 +92,12 @@ 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 ```octave -newtraph(f_m,df_m,140,0.00001) +[root,ea,iter]=newtraph(f_m,df_m,140,0.00001) ``` - ans = 142.74 + root = 142.74 + ea = 8.0930e-06 + iter = 48 ## Secant Methods @@ -119,32 +123,46 @@ $x_{i+1}=x_{i}-\frac{f(x_{i})(\delta x_{i})}{f(x_{i}+\delta x_{i})-f(x_{i})}$ ```octave -mod_secant(f_m,1e-6,50,0.00001) +[root,ea,iter]=mod_secant(f_m,1,50,0.00001) ``` - ans = 142.74 + root = 142.74 + ea = 3.0615e-07 + iter = 7 ```octave -Amt_numerical=mod_secant(@(A) car_payments(A,30000,0.05,5),1e-6,50,0.001) -car_payments(Amt,30000,0.05,5) +car_payments(400,30000,0.05,5,1) ``` - error: 'plot_bool' undefined near line 12 column 6 - error: called from - car_payments at line 12 column 3 - mod_secant at line 22 column 8 - error: 'Amt' undefined near line 1 column 14 - error: evaluating argument list element number 1 + ans = 1.1185e+04 + + + +![svg](lecture_07_files/lecture_07_15_1.svg) ```octave -Amt*12*5 +Amt_numerical=mod_secant(@(A) car_payments(A,700000,0.0875,30,0),1e-6,50,0.001) +car_payments(Amt_numerical,700000,0.0875,30,1) ``` - error: 'Amt' undefined near line 1 column 1 + Amt_numerical = 5467.0 + ans = 3.9755e-04 + + + +![svg](lecture_07_files/lecture_07_16_1.svg) + + + +```octave +Amt_numerical*12*30 +``` + + ans = 1.9681e+06 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. @@ -257,12 +275,13 @@ ea_ms=zeros(1,N); % appr error Modified Secant ea_fp=zeros(1,N); % appr error false point method ea_bs=zeros(1,N); % appr error bisect method for i=1:length(iterations) - [root_nr,ea_nr(i),iter_nr]=newtraph(f_m,df_m,200,0,iterations(i)); + [root_nr,ea_nr(i),iter_nr]=newtraph(f_m,df_m,300,0,iterations(i)); [root_ms,ea_ms(i),iter_ms]=mod_secant(f_m,1e-6,300,0,iterations(i)); [root_fp,ea_fp(i),iter_fp]=falsepos(f_m,1,300,0,iterations(i)); [root_bs,ea_bs(i),iter_bs]=bisect(f_m,1,300,0,iterations(i)); end - + +setdefaults semilogy(iterations,abs(ea_nr),iterations,abs(ea_ms),iterations,abs(ea_fp),iterations,abs(ea_bs)) legend('newton-raphson','mod-secant','false point','bisection') ``` @@ -281,31 +300,27 @@ legend('newton-raphson','mod-secant','false point','bisection') -![svg](lecture_07_files/lecture_07_22_1.svg) +![svg](lecture_07_files/lecture_07_24_1.svg) ```octave -ea_ms +ea_nr ``` - ea_ms = + ea_nr = - Columns 1 through 6: + Columns 1 through 8: - 2.3382e+03 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 + 6.36591 0.06436 0.00052 0.00000 0.00000 0.00000 0.00000 0.00000 - Columns 7 through 12: + Columns 9 through 16: - 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 + 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 - Columns 13 through 18: + Columns 17 through 20: - 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 1.9171e-14 - - Columns 19 and 20: - - 1.9171e-14 1.9171e-14 + 0.00000 0.00000 0.00000 0.00000 @@ -344,32 +359,28 @@ legend('newton-raphson','mod-secant','false point','bisection') -![svg](lecture_07_files/lecture_07_24_1.svg) +![svg](lecture_07_files/lecture_07_26_1.svg) ```octave -ea_bs +ea_nr newtraph(f,df,0.5,0,12) ``` - ea_bs = - - Columns 1 through 6: + ea_nr = - 9.5357e+03 -4.7554e-01 -2.1114e-01 6.0163e-02 -2.4387e-03 6.1052e-04 + Columns 1 through 7: - Columns 7 through 12: + 99.03195 11.11111 11.11111 11.11111 11.11111 11.11111 11.11111 - 2.2891e-04 -9.5367e-06 2.3842e-06 8.9407e-07 -2.2352e-07 9.3132e-09 + Columns 8 through 14: - Columns 13 through 18: + 11.11111 11.11111 11.11111 11.11109 11.11052 11.10624 10.99684 - -2.3283e-09 -8.7311e-10 3.6380e-11 -9.0949e-12 -3.4106e-12 8.5265e-13 + Columns 15 through 20: - Columns 19 and 20: - - -3.5527e-14 8.8818e-15 + 8.76956 2.12993 0.00000 0.00000 0.00000 0.00000 ans = 16.208 @@ -383,6 +394,55 @@ df(300) +```octave +% our class function +f= @(x) tan(x)-(x-1).^2 +mod_secant(f,1e-3,1) +``` + + f = + + @(x) tan (x) - (x - 1) .^ 2 + + ans = 0.37375 + + + +```octave +f(ans) +``` + + ans = -3.5577e-13 + + + +```octave +tan(0.37375) +(0.37375-1)^2 +``` + + ans = 0.39218 + ans = 0.39219 + + + +```octave +f([0:10]) +``` + + ans = + + Columns 1 through 8: + + -1.0000 1.5574 -3.1850 -4.1425 -7.8422 -19.3805 -25.2910 -35.1286 + + Columns 9 through 11: + + -55.7997 -64.4523 -80.3516 + + + + ```octave ``` diff --git a/lecture_07/lecture_07.pdf b/lecture_07/lecture_07.pdf index 79b130c..70d8fc2 100644 Binary files a/lecture_07/lecture_07.pdf and b/lecture_07/lecture_07.pdf differ diff --git a/lecture_07/lecture_07_files/lecture_07_15_1.svg b/lecture_07/lecture_07_files/lecture_07_15_1.svg new file mode 100644 index 0000000..903c445 --- /dev/null +++ b/lecture_07/lecture_07_files/lecture_07_15_1.svg @@ -0,0 +1,131 @@ + + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 10000 + + + + + 15000 + + + + + 20000 + + + + + 25000 + + + + + 30000 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + 5 + + + + + + + + + principle amount left ($) + + + + + time (years) + + + + + gnuplot_plot_1a + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lecture_07/lecture_07_files/lecture_07_16_1.svg b/lecture_07/lecture_07_files/lecture_07_16_1.svg new file mode 100644 index 0000000..a3a2125 --- /dev/null +++ b/lecture_07/lecture_07_files/lecture_07_16_1.svg @@ -0,0 +1,151 @@ + + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + 100000 + + + + + 200000 + + + + + 300000 + + + + + 400000 + + + + + 500000 + + + + + 600000 + + + + + 700000 + + + + + 0 + + + + + 5 + + + + + 10 + + + + + 15 + + + + + 20 + + + + + 25 + + + + + 30 + + + + + + + + + principle amount left ($) + + + + + time (years) + + + + + gnuplot_plot_1a + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lecture_07/lecture_07_files/lecture_07_24_1.svg b/lecture_07/lecture_07_files/lecture_07_24_1.svg index a8b614c..b511b10 100644 --- a/lecture_07/lecture_07_files/lecture_07_24_1.svg +++ b/lecture_07/lecture_07_files/lecture_07_24_1.svg @@ -43,47 +43,47 @@ - + 10-14 - + 10-12 - + 10-10 - + 10-8 - + 10-6 - + 10-4 - + 10-2 - + 100 - + 102 @@ -98,28 +98,43 @@ - - 10 + + 50 + + + + + 100 - - 20 + + 150 - - 30 + + 200 - - 40 + + 250 + + + + + 300 + + + + + 350 - 50 + 400 @@ -141,7 +156,7 @@ - + mod-secant @@ -150,7 +165,7 @@ - + false point @@ -159,7 +174,7 @@ - + bisection @@ -168,7 +183,7 @@ - + diff --git a/lecture_07/lecture_07_files/lecture_07_26_1.svg b/lecture_07/lecture_07_files/lecture_07_26_1.svg new file mode 100644 index 0000000..a8b614c --- /dev/null +++ b/lecture_07/lecture_07_files/lecture_07_26_1.svg @@ -0,0 +1,182 @@ + + +Gnuplot +Produced by GNUPLOT 5.0 patchlevel 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 10-14 + + + + + 10-12 + + + + + 10-10 + + + + + 10-8 + + + + + 10-6 + + + + + 10-4 + + + + + 10-2 + + + + + 100 + + + + + 102 + + + + + 104 + + + + + 0 + + + + + 10 + + + + + 20 + + + + + 30 + + + + + 40 + + + + + 50 + + + + + + + + + + + + + newton-raphson + + + + + newton-raphson + + + + + + mod-secant + + + mod-secant + + + + + + false point + + + false point + + + + + + bisection + + + bisection + + + + + + + + + + + + + + + \ No newline at end of file