逐次代入法的定义:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Public Class Successive
Dim eps As Double = 0.001
Dim Iteration As Integer = 1000
Public Sub New()
End Sub
Public Sub New(ByVal eps As Double, ByVal iteration As Integer)
Me.eps = eps
Me.Iteration = iteration
End Sub
Public Function Solution(ByVal G As Func(Of Double, Double), ByVal x As Double) As Double
Dim i As Integer = 0
Dim x_new, x_old As Double
x_old = x
Do
i += 1
x_new = G(x_old)
If Math.Abs(x_old - x_new) < eps Then Return x_new
x_old = x_new
Loop While i < Iteration
Throw New Exception()
End Function
End Class
Dim eps As Double = 0.001
Dim Iteration As Integer = 1000
Public Sub New()
End Sub
Public Sub New(ByVal eps As Double, ByVal iteration As Integer)
Me.eps = eps
Me.Iteration = iteration
End Sub
Public Function Solution(ByVal G As Func(Of Double, Double), ByVal x As Double) As Double
Dim i As Integer = 0
Dim x_new, x_old As Double
x_old = x
Do
i += 1
x_new = G(x_old)
If Math.Abs(x_old - x_new) < eps Then Return x_new
x_old = x_new
Loop While i < Iteration
Throw New Exception()
End Function
End Class
测试代码,或者说客户代码:
方程x=0.3*exp(x)在x=0.5附近有一个根。用逐次代入法求这个根的真值。eps=1e-8
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Imports System.Math
Module Module1
Sub Main()
Dim substitution As New Successive
Dim sol = substitution.Solution(Function(x) 0.3 * Exp(x), 0.5)
Console.WriteLine(sol)
Console.ReadLine()
End Sub
End Module
Module Module1
Sub Main()
Dim substitution As New Successive
Dim sol = substitution.Solution(Function(x) 0.3 * Exp(x), 0.5)
Console.WriteLine(sol)
Console.ReadLine()
End Sub
End Module