Newton's method in C #

Source: Internet
Author: User
1 Public   Static   Class Bondinterestmath
2 {
3 Public   Delegate   Double Function ( Double X );
4
5 // F (x) = cos (x)-3 * POW (x, 3) = 0
6 Public   Static   Double Func ( Double X)
7 {
8 Return Math. Cos (X) -   3   * Math. Pow (X, 3 );
9 }
10
11 Public   Static   Double Derivative ( Double X)
12 {
13 Return   - Math. Sin (X) -   9   * Math. Pow (X, 2 );
14 }
15
16 Public   Static   Double Newtonmethod (function func, function derivative, Double X0, Double Epsilon)
17 {
18 // Max interation number 10000 by default
19 Int Maxtimes =   10000 ;
20 Double Guess = X0;
21 Double Result = X0;
22 For ( Int I =   1 ; I <= Maxtimes; I ++ )
23 {
24 Guess = Result;
25 Result = Guess - Func (guess) / Derivative (guess );
26 If (Math. Abs (Result - Guess) < Epsilon | Math. Abs (func (result )) < Epsilon)
27 {
28 Console. writeline ( " Iteration = {0} " , I );
29 Break ;
30 }
31 }
32 Return Result;
33 }
34 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.