牛頓迭代法與二分法計(jì)算平方根

因?yàn)椴皇强瓢喑錾砦竟玻约词咕幊桃欢螘r(shí)間也時(shí)常感覺自身基礎(chǔ)知識(shí)非常不扎實(shí),于是在最近開始補(bǔ)習(xí)算法和計(jì)算機(jī)理論的基礎(chǔ)知識(shí)。

目前看的算法書籍是《算法》(第四版)亏较,由Robert Sedgewick以及Kevin Wayne編寫的,由于不可能把所有的練習(xí)都寫成博客記錄下來掩缓,于是就在學(xué)習(xí)過程中雪情,挑選一些有意思的寫成筆記,以便日后參考以及與同行互相交流你辣。

今天要準(zhǔn)備寫的就是非常經(jīng)典的牛頓迭代法求平方根巡通,事實(shí)上現(xiàn)在的絕大部分編程語言中,標(biāo)準(zhǔn)庫中都已經(jīng)為我們準(zhǔn)備好了計(jì)算平方根的函數(shù)舍哄,但是本著學(xué)習(xí)的精神宴凉,今天我們也要寫出一個(gè)求平方根的函數(shù)。

牛頓法是一種在實(shí)數(shù)域和復(fù)數(shù)域上近似求解方程的方法蠢熄。方法使用函數(shù) f(x)的泰勒級(jí)數(shù)的前面幾項(xiàng)來尋找方程f(x)=0的根跪解。首先我們先來看函數(shù)圖像。

首先签孔,選擇一個(gè)接近函數(shù)f(x)零點(diǎn)的x0,計(jì)算相應(yīng)的f(x0)和切線斜率f'(x0)(這里f'表示函數(shù)f的導(dǎo)數(shù))叉讥。
也就是求如下方程的解:

我們將新求得的點(diǎn) x坐標(biāo)命名為x1,通常x1會(huì)比x0更接近方程f(x)=0的解饥追。因此我們現(xiàn)在可以利用x1開始下一輪迭代图仓。迭代公式可化簡(jiǎn)為如下所示:

而求平方根的方程我們可以看成f(x) = x^2 - a,a即為我們要求平方根的常數(shù)。

于是在算法代碼的編寫上但绕,我們也可以用這種猜的思想救崔,來近似求解這個(gè)平方根,我們需要定義一個(gè)精度捏顺,若Xn+1-Xn的值小于我們的精度值六孵,那么我們即可以認(rèn)為Xn為我們要求的解。

所以算法代碼編寫如下(采用Java示例)幅骄。

/**
 * 牛頓迭代法求平方根
 * @param  number   求值的數(shù)
 * @param  accuracy 精度
 * @return          Double
 */
public static double NewtonSqrt(double number, double accuracy) {
         //第一個(gè)猜測(cè)值
        double guess = number / 2;
        int count = 0;
        if (number < 0) {
            return Double.NaN;
        }
        //當(dāng)兩個(gè)猜測(cè)的差值大于精度即return
        while (Math.abs(guess - (number / guess)) > accuracy) {
            //迭代公式推導(dǎo)而成
            guess = (guess + (number / guess)) / 2;
            count++;
            System.out.printf("try count = %d, guess = %f\n", count, guess);
        }
        System.out.printf("final result = %f\n", guess);
        return guess;
    }

牛頓迭代法求平方根的代碼就如上面所示劫窒,而接下來為了體現(xiàn)牛頓迭代法的優(yōu)勢(shì),我們?cè)賹懸粋€(gè)二分法計(jì)算平方根的算法拆座,來對(duì)比:

    public static double DichotomySqrt(double number, double accuracy) {
        double higher = number;
        double lower = 0.0;
        double middle = (lower + higher) / 2;
        double last_middle = 0.00;
        int count = 0;
        if (number < 0) {
            return Double.NaN;
        }
        while (Math.abs(middle - last_middle) > accuracy) {
            if (middle * middle > number) {
                higher = middle;
            } else {
                lower = middle;
            }
            last_middle = middle;
            middle = (lower + higher) / 2;
            count++;
            System.out.printf("Dichotomy try count = %d, guess = %f\n", count, last_middle);
        }
        System.out.printf("Dichotomy final result = %f\n", last_middle);
        return last_middle;
    }

二分法的講解就不多說了主巍,跟牛頓迭代法的驗(yàn)證結(jié)果相似冠息,看精度差是否在定義范圍內(nèi)。

那么接下來我們來測(cè)試二分法和牛頓迭代法求值的效率孕索。


    public static void main(String[] args) {
        double result = NewtonSqrt(2,1e-3);
        double dichotomyRes = DichotomySqrt(2,1e-3);
    }

先看小精度情況下逛艰,求2的平方根

try count = 1 guess = 1.5
try count = 2 guess = 1.4166666666666665
try count = 3 guess = 1.4142156862745097
final result = 1.4142156862745097

Dichotomy try count = 1 guess = 1.0
Dichotomy try count = 2 guess = 1.5
Dichotomy try count = 3 guess = 1.25
Dichotomy try count = 4 guess = 1.375
Dichotomy try count = 5 guess = 1.4375
Dichotomy try count = 6 guess = 1.40625
Dichotomy try count = 7 guess = 1.421875
Dichotomy try count = 8 guess = 1.4140625
Dichotomy try count = 9 guess = 1.41796875
Dichotomy try count = 10 guess = 1.416015625
Dichotomy final result = 1.416015625

可以看到牛頓迭代法計(jì)算了3次,二分法計(jì)算了10次搞旭。

而精度稍大的時(shí)候


    public static void main(String[] args) {
        double result = NewtonSqrt(2,1e-15);
        double dichotomyRes = DichotomySqrt(2,1e-15);
    }

try count = 1 guess = 1.5
try count = 2 guess = 1.4166666666666665
try count = 3 guess = 1.4142156862745097
try count = 4 guess = 1.4142135623746899
try count = 5 guess = 1.414213562373095
final result = 1.414213562373095

Dichotomy try count = 1 guess = 1.0
Dichotomy try count = 2 guess = 1.5
Dichotomy try count = 3 guess = 1.25
Dichotomy try count = 4 guess = 1.375
Dichotomy try count = 5 guess = 1.4375
Dichotomy try count = 6 guess = 1.40625
Dichotomy try count = 7 guess = 1.421875
Dichotomy try count = 8 guess = 1.4140625
Dichotomy try count = 9 guess = 1.41796875
Dichotomy try count = 10 guess = 1.416015625
Dichotomy try count = 11 guess = 1.4150390625
Dichotomy try count = 12 guess = 1.41455078125
Dichotomy try count = 13 guess = 1.414306640625
Dichotomy try count = 14 guess = 1.4141845703125
Dichotomy try count = 15 guess = 1.41424560546875
Dichotomy try count = 16 guess = 1.414215087890625
Dichotomy try count = 17 guess = 1.4141998291015625
Dichotomy try count = 18 guess = 1.4142074584960938
Dichotomy try count = 19 guess = 1.4142112731933594
Dichotomy try count = 20 guess = 1.4142131805419922
Dichotomy try count = 21 guess = 1.4142141342163086
Dichotomy try count = 22 guess = 1.4142136573791504
Dichotomy try count = 23 guess = 1.4142134189605713
Dichotomy try count = 24 guess = 1.4142135381698608
Dichotomy try count = 25 guess = 1.4142135977745056
Dichotomy try count = 26 guess = 1.4142135679721832
Dichotomy try count = 27 guess = 1.414213553071022
Dichotomy try count = 28 guess = 1.4142135605216026
Dichotomy try count = 29 guess = 1.414213564246893
Dichotomy try count = 30 guess = 1.4142135623842478
Dichotomy try count = 31 guess = 1.4142135614529252
Dichotomy try count = 32 guess = 1.4142135619185865
Dichotomy try count = 33 guess = 1.4142135621514171
Dichotomy try count = 34 guess = 1.4142135622678325
Dichotomy try count = 35 guess = 1.4142135623260401
Dichotomy try count = 36 guess = 1.414213562355144
Dichotomy try count = 37 guess = 1.4142135623696959
Dichotomy try count = 38 guess = 1.4142135623769718
Dichotomy try count = 39 guess = 1.4142135623733338
Dichotomy try count = 40 guess = 1.4142135623715149
Dichotomy try count = 41 guess = 1.4142135623724243
Dichotomy try count = 42 guess = 1.414213562372879
Dichotomy try count = 43 guess = 1.4142135623731065
Dichotomy try count = 44 guess = 1.4142135623729928
Dichotomy try count = 45 guess = 1.4142135623730496
Dichotomy try count = 46 guess = 1.414213562373078
Dichotomy try count = 47 guess = 1.4142135623730923
Dichotomy try count = 48 guess = 1.4142135623730994
Dichotomy try count = 49 guess = 1.4142135623730958
Dichotomy try count = 50 guess = 1.414213562373094
Dichotomy final result = 1.414213562373094

這里就一目了然了散怖,所以有時(shí)候,寫代碼一定不能想著功能實(shí)現(xiàn)了就好选脊,在算法的效率上一定要多多思考杭抠。

不再舉栗子了,免得有湊字?jǐn)?shù)的嫌疑恳啥。下次再討論咯偏灿。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市钝的,隨后出現(xiàn)的幾起案子翁垂,更是在濱河造成了極大的恐慌,老刑警劉巖硝桩,帶你破解...
    沈念sama閱讀 211,817評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沿猜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡碗脊,警方通過查閱死者的電腦和手機(jī)啼肩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,329評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來衙伶,“玉大人祈坠,你說我怎么就攤上這事∈妇ⅲ” “怎么了赦拘?”我有些...
    開封第一講書人閱讀 157,354評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)芬沉。 經(jīng)常有香客問我躺同,道長(zhǎng),這世上最難降的妖魔是什么丸逸? 我笑而不...
    開封第一講書人閱讀 56,498評(píng)論 1 284
  • 正文 為了忘掉前任蹋艺,我火速辦了婚禮,結(jié)果婚禮上黄刚,老公的妹妹穿的比我還像新娘车海。我一直安慰自己,他們只是感情好隘击,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,600評(píng)論 6 386
  • 文/花漫 我一把揭開白布侍芝。 她就那樣靜靜地躺著,像睡著了一般埋同。 火紅的嫁衣襯著肌膚如雪州叠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,829評(píng)論 1 290
  • 那天凶赁,我揣著相機(jī)與錄音咧栗,去河邊找鬼。 笑死虱肄,一個(gè)胖子當(dāng)著我的面吹牛致板,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咏窿,決...
    沈念sama閱讀 38,979評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼斟或,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了集嵌?” 一聲冷哼從身側(cè)響起萝挤,我...
    開封第一講書人閱讀 37,722評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎根欧,沒想到半個(gè)月后怜珍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,189評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凤粗,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,519評(píng)論 2 327
  • 正文 我和宋清朗相戀三年酥泛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嫌拣。...
    茶點(diǎn)故事閱讀 38,654評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柔袁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出亭罪,到底是詐尸還是另有隱情瘦馍,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布应役,位于F島的核電站情组,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏箩祥。R本人自食惡果不足惜院崇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,940評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望袍祖。 院中可真熱鬧底瓣,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,762評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至茁肠,卻和暖如春患民,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背垦梆。 一陣腳步聲響...
    開封第一講書人閱讀 31,993評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工匹颤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人托猩。 一個(gè)月前我還...
    沈念sama閱讀 46,382評(píng)論 2 360
  • 正文 我出身青樓印蓖,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親京腥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赦肃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,543評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容

  • C語言的學(xué)習(xí)要從基礎(chǔ)開始,這里是100個(gè)經(jīng)典的算法-1C語言的學(xué)習(xí)要從基礎(chǔ)開始绞旅,這里是100個(gè)經(jīng)典的 算法 題目:...
    Poison_19ce閱讀 1,129評(píng)論 0 0
  • 轉(zhuǎn)載自http://wanwu.tech/2017/03/15/functions-and-closures/ 簡(jiǎn)...
    quitus閱讀 502評(píng)論 0 0
  • 因?yàn)榇邓哪芰Σ患寻诔ⅲ砸却騻€(gè)草稿,今天的吹水過程大概是:1因悲、牛頓迭代法的演繹過程2堕汞、牛頓迭代法求n次方根3、牛...
    pointertan閱讀 2,619評(píng)論 0 1
  • 牛頓迭代法的作用是使用迭代法來求解函數(shù)方程的根晃琳,簡(jiǎn)單的說就是不斷地求取切線的過程.對(duì)于形如f(x)=0的方程讯检,首先...
    Joe_HUST閱讀 2,597評(píng)論 0 1
  • 轉(zhuǎn)自Poll 的筆記 閱讀目錄 梯度下降法(Gradient Descent) 牛頓法和擬牛頓法(Newton's...
    JSong1122閱讀 1,374評(píng)論 0 3