牛頓迭代法

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   ar   2014   代碼   

牛頓迭代

詳細介紹見維基百科 http://zh.wikipedia.org/wiki/牛頓法


 


舉一個栗子


找平方根

給一個數a,求其平方根。

 

設其平方根為x 則有 x^2 - a = 0,設函數f(x) = x^2 - a,

取x0的初值盡量靠近a的平方根(因為初值的選擇影響迭代的次數)

根據 f(x0)  = (x0 - x).f’(x0)

--->  x = x0 -  f(x0)/f’(x0)   ①

--->  x0 = x                        ②

重複①、②直到abs(x-x0)<=eps,達到精度要求即可。

代碼:

a = input();

x0 = Init_Value;

While( fabs(x-x0)>eps){

       x0 = x;

       x = x0 - f(x0)/f’(x0);

}

 

Eg.  a  = 2 , eps = 1e-8

X0 = 1.0

迭代過程中x的變化

1.5000000000000000

1.4166666666666667

1.4142156862745099

1.4142135623746899

1.4142135623730951

 

總的來說

F(x)函數連續,且在零點範圍內收斂。

迭代的次數受x0和精度影響。

迭代次數很少相對(二分),(精度倍增)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.