Newton iterative method for equation root

The second essay

In late November 9102, engineering a male Cao ** To calculate the equation f (x) = 0 root, where f (x) expression is:

Because the real number range of f (x) = 0 of the root too, so this study only -2 <<x 2 of the case. This equation long ugly, Cao ** watching feel uncomfortable, the guide to give a f '(x)

This formula is more ugly, but we have Newton iterative method can be constructed iterative sequence {xn} meet:

Wherein f '(xn) is not equal to 0. It can be shown as long as the initial value of a good selection, sequence may be required to converge to the root. Root can then write a program.

Function on the first image (drawn by Desmos), there are five zeros seen on the specified range. Then, it near zero value.

Then in effect

 

The result is good.

Finally, the codes .f (x) and f '(x) function is passed calc delegate manner. Note delegate instantiation

Public Delegate Function myfunc(x As Double) As Double
Public Function func0(x As Double) As Double
    Return Exp(x) + Pow(x, 4) * Sin(Pow(x, 3))
End Function
Public Function func0derive(x As Double) As Double
    Return Exp(x) + 4 * Pow(x, 3) * Sin(Pow(x, 3)) + 3 * Pow(x, 6) * Cos(Pow(x, 3))
End Function
Dim f0 As New myfunc(AddressOf func0)
Dim fd0 As New myfunc(AddressOf func0derive)

 

calc的参数中f和fd分别是指向f(x)和f'(x)的函数指针,x0为初值,eps为精度,cnt为迭代次数

用传引用的方式,通过sol返回计算结果.

返回True为没有出错,False为出错.

 1 Public Function Calc(f As myfunc, fd As myfunc, x0 As Double, eps As Double, cnt As Integer, ByRef sol As Double) As Boolean
 2         If cnt <= 0 Or f Is Nothing Or fd Is Nothing Then
 3             Return False
 4         End If
 5         Try
 6             sol = 0
 7             Dim x As Double = x0, c0 As Integer = 0
 8             While Math.Abs(x) > eps And cnt > c0
 9                 x = x - f(x) / fd(x)
10                 c0 += 1
11             End While
12             sol = x
13             Return True
14         Catch ex As Exception
15             Return False
16         End Try
17     End Function

 

 

Guess you like

Origin www.cnblogs.com/woshilxcdexuesheng/p/11964097.html