Newton-Raphson算法简介及其R实现

本文简要介绍了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
      
时间: 2024-08-29 04:04:17

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

分类算法简介 基于R

最近的关键字:分类算法,outlier detection, machine learning 简介: 此文将 k-means,decision tree,random forest,SVM(support vector mechine),人工神经网络(Artificial Neural Network,简称ANN )这几种常见的算法 apply 在同一个数据集 spam,看各种方法预测错误率,或准确率,旨在追求预测准确性,辨识出这几种方法的实用性,对背后的理论依据,大量的数学公式,不作讨论(能

最小生成树 kruskal算法简介

生成树--在一个图中的一个联通子图  使得所有的节点都被(访问) 最小生成树 (MST) 即联通子图的总代价(路程)最小 已知的一个图 有n个点 m条边 kruskal的算法如下 先对边从小到大排序 从最小的边起,不停的合并这条边的两个节点到一个集合,如果这条边的两个节点已经在一个集合里,则无视,否则形成回路(显然错误)直到所有的节点并到一个集合里 这里需要用到并查集来合并节点 1 int cmp(const int i,const int j) { 2 return w[i] < w[j];

双目视觉算法简介

http://blog.csdn.net/u010784534/article/details/50437612 转载自:http://blog.sina.com.cn/s/blog_4a540be60102v44s.html 1. 双目视觉算法简介 1.1. 双目视觉简介 双目视觉广泛应用在机器人导航,精密工业测量.物体识别.虚拟现实.场景重建,勘测领域. 什么是双目视觉? 双目视觉是模拟人类视觉原理,使用计算机被动感知距离的方法.从两个或者多个点观察一个物体,获取在不同视角下的图像,根据图像

爬山算法和模拟退火算法简介(转)

源:爬山算法和模拟退火算法简介 一. 爬山算法 ( Hill Climbing ) 介绍模拟退火前,先介绍爬山算法.爬山算法是一种简单的贪心搜索算法,该算法每次从当前解的临近解空间中选择一个最优解作为当前解,直到达到一个局部最优解. 爬山算法实现很简单,其主要缺点是会陷入局部最优解,而不一定能搜索到全局最优解.如图1所示:假设C点为当前解,爬山算法搜索到A点这个局部最优解就会停止搜索,因为在A点无论向那个方向小幅度移动都不能得到更优的解. 图1 二. 模拟退火(SA,Simulated Anne

机器学习算法简介

欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:吴懿伦 导语: 本文是对机器学习算法的一个概览,以及个人的学习小结.通过阅读本文,可以快速地对机器学习算法有一个比较清晰的了解.本文承诺不会出现任何数学公式及推导,适合茶余饭后轻松阅读,希望能让读者比较舒适地获取到一点有用的东西. 引言 本文是对机器学习算法的一个概览,以及个人的学习小结.通过阅读本文,可以快速地对机器学习算法有一个比较清晰的了解.本文承诺不会出现任何数学公式及推导,适合茶余饭后轻松阅读,希望能让读者比较舒适地获取到

TF-IDF算法简介

TF-IDF算法全称为term frequency–inverse document frequency.TF就是term frequency的缩写,意为词频.IDF则是inverse document frequency的缩写,意为逆文档频率. 该算法在信息处理中通常用来抽取关键词.比如,对一个文章提取关键词作为搜索词,就可以采用TF-IDF算法. 要找出一篇文章中的关键词,通常的思路就是,就是找到出现次数最多的词.如果某个词很重要,它应该在这篇文章中多次出现.于是,我们进行"词频"

AES算法简介

AES算法简介 一. AES的结构 1.总体结构 明文分组的长度为128位即16字节,密钥长度可以为16,24或者32字节(128,192,256位).根据密钥的长度,算法被称为AES-128,AES-192或者AE-256. 2.明文密钥组织方式 3.一些相关的的术语定义和表示 • 状态(State):密码运算的中间结果称为状态. • State的表示:状态用以字节为基本构成元素的矩阵阵列来表示,该阵列有4行,列数记为Nb. Nb=分组长度(bits)÷ 32.Nb可以取的值为4,对应的分组长

Java哈希散列算法简介 - MD5 &amp; SHA-512

Java哈希散列算法简介 - MD5 & SHA-512 在日常的开发工作中,我们常常会碰到这样的一个场景:我们需要有一种可靠的行之有效的方法来检验跟判断数据在传输过程当中的完整性.最常见的一种情况就是当我们传输文件的时候,由于网络故障或者其他的一些因素,可能会出现我们下载下来的文件不完整,这给我们日常的开发和维护带了一些难题:另外的一个较为常用的场景就是:有没有一种行之有效的方法让我们可以很方便的判断服务器上的文件是不是有最新的数据更新,比如我们现在的移动Hybird App开发,我们经常会发

MD5算法 简介

MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能 l 输入任意长度的信息,经过处理,输出为128位的信息(数字指纹): l 不同的输入得到的不同的结果(唯一性): l 根据128位的输出结果不可能反推出输入的信息(不可逆): MD5用途 1.防止被篡改: 1)比如发送一个电子文档,发送前,我先得到MD5的输出结果a.然后在对方收到电子文档后,对方也得到一个M