牛顿法求根号12

1.牛顿法公式

2.具体推到过程

  1.找近似值得点3作为起始点

        2.将 x=3 带入 y=x**2, ----> 得到点(3,9)

        3.在曲线y=x**2上的点(3,9)处求切线方程 z=ax+b

        4.在z直线上求点(x,12)

        5. x --> y=x**2  --> 近视值

        6. abs(y-12) < 0.00000001 时收敛,否则重复step 2-5

3. 使用go 语言实现该算法

  

package main

import (
	"fmt"
	"math"
)


func sqrt(y float64) float64 {
        x := 1.0
        for {
	    next_x :=  x + (y -x*x)/(2*x)
            if math.Abs(next_x*next_x - y) < 0.000000000001 {
		return next_x
            }
            x = next_x
	}
		 
}

func main(){
	fmt.Println(sqrt(12.0))
	fmt.Println(math.Sqrt(12.0))
}

  for 循环参考:https://www.cnblogs.com/yejg1212/archive/2013/03/18/2967020.html

 

posted @ 2018-02-04 04:02  瓦肯船长  阅读(496)  评论(0)    收藏  举报