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 }