本文简要介绍了Newton-Raphson方法及其R语言实现并给出几道练习题供参考使用。 下载PDF格式文档(Academia.edu)

  • Newton-Raphson Method
    Let $f(x)$ be a differentiable function and let $a_0$ be a guess for a solution to the equation $$f(x)=0$$ We can product a sequence of points $x=a_0, a_1, a_2, \dots $ via the recursive formula $$a_{n+1}=a_n-\frac{f(a_n)}{f'(a_n)}$$ that are successively better approximation of a solution to the equation $f(x)=0$.
  • R codes

    There are 4 parameters in this function:

    • f is the function you input.
    • tol is the tolerance (default $1e-7$).
    • x0 is the initial guess.
    • N is the default number (100) of iterations.

    The process will be end up until either the absolute difference between two adjacent approximations is less than tol, or the number of iterations reaches N.

  • Examples
    Generally speaking, the "guess" is important. More precisely, according to Intermediate Value Theorem we can find two values of which function value are larger and less than 0, respectively. Then choosing the one, which first derivative is larger than another, as the initial guess value in the iterative formula. This process will guarantee the convergence of roots. Let's see some examples.
    • Example 1
      Approximate the fifth root of 7.
      Solution:
      Denote $f(x)=x^5-7$. It is easily to know that $f(1)=-6 < 0$ and $f(2)=25 > 0$. Additionally, $f'(1)=5 < f'(2)=80$, so we set the initial guess value $x_0=2$. By Newton-Raphson method we get the result is 1.47577316159. And $$f(1.47577316159)\approx 1.7763568394e-15$$ which is very close to 0. R codes is below:

      # Example 1
      f = function(x){x^5 - 7}
      h = 1e - 7
      df.dx = function(x){(f(x + h) - f(x)) / h}
      df.dx(1); df.dx(2)
      # [1] 5.0000009999
      # [1] 80.0000078272
      app = newton(f, x0 = 2)
      app
      # [1] 1.68750003057 1.52264459615 1.47857137506 1.47578373325 1.47577316175
      # [6] 1.47577316159
      f(app[length(app)])
      # [1] 1.7763568394e-15
    • Example 2
      The function $f(x)=x^5-5x^4+5x^2-6$ has a root between 1 and 5. Approximate it by Newton-Raphson method.
      Solution:
      We try to calculate some values first. $f(1)=-5, f(2)=-34, f(3)=-123, f(4)=-182, f(5)=119$, so there should be a root between 4 and 5. Since $f'(4)=40 < f'(5)=675$, hence $x_0=5$ is a proper initial guess value. By Newton-Raphson method we get the result is 4.79378454069 and $$f(4.79378454069)\approx -2.84217094304e-14$$ which is a desired approximation. R codes is below:
      # Example 2
      f = function(x){x^5 - 5 * x^4 + 5 * x^2 - 6}
      x = c(1 : 5)
      f(x)
      # [1] -5 -34 -123 -182 119
      h = 1e-7
      df.dx = function(x){(f(x + h) - f(x)) / h}
      df.dx(4); df.dx(5)
      # [1] 40.0000163836
      # [1] 675.000053008
      app = newton(f, x0 = 5)
      app
      # [1] 4.82370371755 4.79453028339 4.79378501861 4.79378454069 4.79378454069
      f(app[length(app)])
      # [1] -2.84217094304e-14
    • Example 3
      A rectangular piece of cardboard of dimensions $8\times 17$ is used to make an open-top box by cutting out a small square of side $x$ from each corner and bending up the sides. Find a value of $x$ for which the box has volume 100.
      Solution:
      Firstly, building the model. $V(x)=x(8-2x)(17-2x)=100$, that is, we want to find the root of equation $$f(x)=x(8-2x)(17-2x)-100=0\Leftrightarrow f(x)=4x^3-50x^2+136x-100=0$$ We know that $0 < x < 4$ and hence try to calculate some non-negative integers: $$f(0)=-100, f(1)=-10, f(2)=4, f(3)=-34, f(4)=-100$$ Note that there are two intervals may have roots: $(1, 2)\cup (2,3)$. Since $$f'(1)=48 > f'(2)=-16 > f'(3)=-56$$ so we set the initial guess values $x_0=1$ and $x'_0=2$ (i.e. there are two separate iteration procedures). By using Newton-Raphson method we obtain the result are 11.26063715644 and 2.19191572127 respectively. Both of them are quite accurate. R codes is below:
      # Example 3
      f = function(x){4 * x^3 - 50 * x^2 + 136 * x - 100}
      x = c(0 : 4)
      f(x)
      # [1] -100 -10 4 -34 -100
      h = 1e-7
      df.dx = function(x){(f(x + h) - f(x)) / h}
      df.dx(1); df.dx(2); df.dx(3)
      # [1] 47.9999962977
      # [1] -16.0000024607
      # [1] -56.0000012229
      app1 = newton(f, x0 = 1)
      app2 = newton(f, x0 = 2)
      app1; app2
      # [1] 1.20833334940 1.25768359879 1.26062673622 1.26063715631 1.26063715644
      # [1] 2.24999996155 2.19469026652 2.19192282154 2.19191572132 2.19191572127
      f(app1[length(app1)]); f(app2[length(app2)])
      # [1] 2.84217094304e-14
      # [1] -2.84217094304e-14

Newton-Raphson算法简介及其R实现的更多相关文章

  1. 分类算法简介 基于R

    最近的关键字:分类算法,outlier detection, machine learning 简介: 此文将 k-means,decision tree,random forest,SVM(supp ...

  2. LARS 最小角回归算法简介

    最近开始看Elements of Statistical Learning, 今天的内容是线性模型(第三章..这本书东西非常多,不知道何年何月才能读完了),主要是在看变量选择.感觉变量选择这一块领域非 ...

  3. webrtc 的回声抵消(aec、aecm)算法简介(转)

    webrtc 的回声抵消(aec.aecm)算法简介        webrtc 的回声抵消(aec.aecm)算法主要包括以下几个重要模块:1.回声时延估计 2.NLMS(归一化最小均方自适应算法) ...

  4. AES算法简介

    AES算法简介 一. AES的结构 1.总体结构 明文分组的长度为128位即16字节,密钥长度可以为16,24或者32字节(128,192,256位).根据密钥的长度,算法被称为AES-128,AES ...

  5. 排列熵算法简介及c#实现

    一.   排列熵算法简介: 排列熵算法(Permutation Entroy)为度量时间序列复杂性的一种方法,算法描述如下: 设一维时间序列: 采用相空间重构延迟坐标法对X中任一元素x(i)进行相空间 ...

  6. <算法图解>读书笔记:第1章 算法简介

    阅读书籍:[美]Aditya Bhargava◎著 袁国忠◎译.人民邮电出版社.<算法图解> 第1章 算法简介 1.2 二分查找 一般而言,对于包含n个元素的列表,用二分查找最多需要\(l ...

  7. AI - 机器学习常见算法简介(Common Algorithms)

    机器学习常见算法简介 - 原文链接:http://usblogs.pwc.com/emerging-technology/machine-learning-methods-infographic/ 应 ...

  8. STL所有算法简介 (转) http://www.cnblogs.com/yuehui/archive/2012/06/19/2554300.html

    STL所有算法简介 STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baid ...

  9. PageRank 算法简介

    有两篇文章一篇讲解(下面copy)< PageRank算法简介及Map-Reduce实现>来源:http://www.cnblogs.com/fengfenggirl/p/pagerank ...

随机推荐

  1. ul、li实现横向导航按钮

    好久没写博客了,主要是懒得呼气都不想呼,上周分给我一个新的任务,就是自己新建一个系统,快速极限开发,虽然之前自己也做过小的系统,但毕竟是自己做,随着自己的心意做,没有做其他的限制等,现在呢是给公司做, ...

  2. strlen 与 sizeof 的区别

    void ngx_time_init(void) { ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1 ...

  3. 深入理解计算机系统(2.7)---二进制浮点数,IEEE标准(重要)

    2.6我们进行了二进制整数运算的最后一役,本次LZ将和各位一起进入浮点数的世界,这里没有无符号,没有补码,但是有各种各样的惊奇.倘若你真正的进入了浮点数的世界,一定会发现它原来是这么有意思,而不是像之 ...

  4. MVC架构设计——EF-Code First

    详情参考:http://www.cnblogs.com/guomingfeng/archive/2013/05/28/mvc-ef-repository.html

  5. c/c++模板的定义和实现分开的问题及其解决方案

    注意c/c++模板的定义和实现- -                                       定义一个类一般都是在头文件中进行类声明,在cpp文件中实现,但使用模板时应注意目前的C ...

  6. jq不包含某属性

    jq解释属性选择器时有以下四种: 上面都是带某属性或者属性为某值的情况,还有一种情况是不带某属性怎么办? 答案是同属性不为某值. 如 <a b='c' class="d"&g ...

  7. 腾讯云CentOS 安装MediaWiki

    参考 : https://www.digitalocean.com/community/tutorials/how-to-install-mediawiki-on-centos-7 //安装好很多次终 ...

  8. Ceph性能优化总结(v0.94)

    优化方法论 做任何事情还是要有个方法论的,“授人以鱼不如授人以渔”的道理吧,方法通了,所有的问题就有了解决的途径.通过对公开资料的分析进行总结,对分布式存储系统的优化离不开以下几点: 1. 硬件层面 ...

  9. Java--笔记(4)

    31.中间件是一种独立的系统软件或服务程序,分布式应用软件借助这种软件在不同的技术之间共享资源.中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯.是连接两个独立应用程序或独立系统的软 ...

  10. Ubuntu14.04下安装tomcat

    1.官方网站下载最新的tomcat:http://tomcat.apache.org/download-80.cgi在ubuntu上,我们下载zip和tar.gz.Ubuntu14.04安装和配置To ...