主题:请教牛顿迭代法,我的结果不对帮我改改吧
myjianghe
[专家分:470] 发布于 2008-01-24 14:19:00
我按照matlab里的程序改成VB的后是可以运行的而且是对的,但是我自己的却只能迭代一次,norm_x值越来越大,(应该是越来越小)。希望各位帮我改改。谢谢啊
回复列表 (共8个回复)
沙发
老大徒伤悲 [专家分:29120] 发布于 2008-01-24 14:29:00
牛顿叠代,在我的印象里似乎是递归调用的。
板凳
jzyray [专家分:20610] 发布于 2008-01-25 12:50:00
不帖代码怎么给你改错。。。。
3 楼
wdkshp [专家分:5490] 发布于 2008-01-25 12:51:00
我记得好像求方程的近似值使用的牛顿迭代法
4 楼
myjianghe [专家分:470] 发布于 2008-01-25 16:10:00
Public Sub fff(fff_x As Location, SENSOR_1 As Location, SENSOR_2 As Location, SENSOR_3 As Location, SENSOR_4 As Location, fff_y() As Double, v As Double)
fff_y(1) = (SENSOR_1.X - fff_x.X) * (SENSOR_1.X - fff_x.X) + (SENSOR_1.Y - fff_x.Y) * (SENSOR_1.Y - fff_x.Y) + (SENSOR_1.Z - fff_x.Z) * (SENSOR_1.Z - fff_x.Z) - (SENSOR_1.T - fff_x.T) * v * (SENSOR_1.T - fff_x.T) * v
fff_y(2) = (SENSOR_2.X - fff_x.X) * (SENSOR_2.X - fff_x.X) + (SENSOR_2.Y - fff_x.Y) * (SENSOR_2.Y - fff_x.Y) + (SENSOR_2.Z - fff_x.Z) * (SENSOR_2.Z - fff_x.Z) - (SENSOR_2.T - fff_x.T) * v * (SENSOR_2.T - fff_x.T) * v
fff_y(3) = (SENSOR_3.X - fff_x.X) * (SENSOR_3.X - fff_x.X) + (SENSOR_3.Y - fff_x.Y) * (SENSOR_3.Y - fff_x.Y) + (SENSOR_3.Z - fff_x.Z) * (SENSOR_3.Z - fff_x.Z) - (SENSOR_3.T - fff_x.T) * v * (SENSOR_3.T - fff_x.T) * v
fff_y(4) = (SENSOR_4.X - fff_x.X) * (SENSOR_4.X - fff_x.X) + (SENSOR_4.Y - fff_x.Y) * (SENSOR_4.Y - fff_x.Y) + (SENSOR_4.Z - fff_x.Z) * (SENSOR_4.Z - fff_x.Z) - (SENSOR_4.T - fff_x.T) * v * (SENSOR_4.T - fff_x.T) * v
End Sub
Sub dfff(fff_x As Location, dfff_y() As Double, SENSOR_1 As Location, SENSOR_2 As Location, SENSOR_3 As Location, SENSOR_4 As Location, v As Double)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''在此直接转置''''''''''''''''''''''''''''''
dfff_y(1, 1) = 2 * (fff_x.X - SENSOR_1.X)
dfff_y(2, 1) = 2 * (fff_x.Y - SENSOR_1.Y)
dfff_y(3, 1) = 2 * (fff_x.Z - SENSOR_1.Z)
dfff_y(4, 1) = -2 * (fff_x.T - SENSOR_1.T) * v * v
dfff_y(1, 2) = 2 * (fff_x.X - SENSOR_2.X)
dfff_y(2, 2) = 2 * (fff_x.Y - SENSOR_2.Y)
dfff_y(3, 2) = 2 * (fff_x.Z - SENSOR_2.Z)
dfff_y(4, 2) = -2 * (fff_x.T - SENSOR_2.T) * v * v
dfff_y(1, 3) = 2 * (fff_x.X - SENSOR_3.X)
dfff_y(2, 3) = 2 * (fff_x.Y - SENSOR_3.Y)
dfff_y(3, 3) = 2 * (fff_x.Z - SENSOR_3.Z)
dfff_y(4, 3) = -2 * (fff_x.T - SENSOR_3.T) * v * v
dfff_y(1, 4) = 2 * (fff_x.X - SENSOR_4.X)
dfff_y(2, 4) = 2 * (fff_x.Y - SENSOR_4.Y)
dfff_y(3, 4) = 2 * (fff_x.Z - SENSOR_4.Z)
dfff_y(4, 4) = -2 * (fff_x.T - SENSOR_4.T) * v * v
End Sub
Sub semic_location(SENSOR_1 As Location, SENSOR_2 As Location, SENSOR_3 As Location, SENSOR_4 As Location, SEMIC As Location, v As Double)
'采用牛顿迭代法计算定位位置,返回(x,y,z,t)
Dim fff_y(4) As Double
Dim dfff_y(4, 4) As Double
'判断是否执行GAUSSJ(高斯-约当方程组)
IsTrue = False
Dim i, j As Integer
Dim TEMP(4) As Double
Dim norm_x As Double
Dim count As Integer
Call fff(SEMIC, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4, fff_y(), v)
Call dfff(SEMIC, dfff_y(), SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4, v)
If IsTrue = False Then
Call GAUSSJ(dfff_y(), 4, fff_y())
End If
TEMP(1) = SEMIC.X - fff_y(1)
TEMP(2) = SEMIC.Y - fff_y(2)
TEMP(3) = SEMIC.Z - fff_y(3)
TEMP(4) = SEMIC.T - fff_y(4)
count = 0
Do
SEMIC.X = TEMP(1)
SEMIC.Y = TEMP(2)
SEMIC.Z = TEMP(3)
SEMIC.T = TEMP(4)
Call fff(SEMIC, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4, fff_y(), v)
Call dfff(SEMIC, dfff_y(), SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4, v)
'x=fff_y/dfff_y;x=fff_y*inv(dfff_y)
'A=DFFF_y的转置;B=A-1B
If IsTrue = False Then
Call GAUSSJ(dfff_y(), 4, fff_y())
End If
TEMP(1) = SEMIC.X - fff_y(1)
TEMP(2) = SEMIC.Y - fff_y(2)
TEMP(3) = SEMIC.Z - fff_y(3)
TEMP(4) = SEMIC.T - fff_y(4)
count = count + 1
norm_x = Sqr((SEMIC.X - TEMP(1)) * (SEMIC.X - TEMP(1)) + (SEMIC.Y - TEMP(2)) * (SEMIC.Y - TEMP(2)) + (SEMIC.Z - TEMP(3)) * (SEMIC.Z - TEMP(3))) '+ (SEMIC.T - TEMP(4)) * (SEMIC.T - TEMP(4))
Loop While norm_x > (0.01) And (count < 1)
End Sub
5 楼
myjianghe [专家分:470] 发布于 2008-01-25 16:11:00
Sub GAUSSJ(A() As Double, N, B() As Double)
Dim IPIV(4), INDXR(4), INDXC(4)
For j = 1 To N
IPIV(j) = 0
Next j
For i = 1 To N
BIG = 0#
For j = 1 To N
If IPIV(j) <> 1 Then
For k = 1 To N
If IPIV(k) = 0 Then
If Abs(A(j, k)) >= BIG Then
BIG = Abs(A(j, k))
IROW = j
ICOL = k
End If
ElseIf IPIV(k) > 1 Then
p = MsgBox(" 系数矩阵1为奇异,无解!!! ", 0, "警告!")
GoTo 2
End If
Next k
End If
Next j
IPIV(ICOL) = IPIV(ICOL) + 1
If IROW <> ICOL Then
For L = 1 To N
DUM = A(IROW, L)
A(IROW, L) = A(ICOL, L)
A(ICOL, L) = DUM
Next L
DUM = B(IROW)
B(IROW) = B(ICOL)
B(ICOL) = DUM
End If
INDXR(i) = IROW
INDXC(i) = ICOL
If A(ICOL, ICOL) = 0# Then
p = MsgBox(" 系数矩阵2为奇异!!! ", 0, "警告!")
IsTrue = True
GoTo 2
End If
PIVINV = 1# / A(ICOL, ICOL)
A(ICOL, ICOL) = 1#
For L = 1 To N
A(ICOL, L) = A(ICOL, L) * PIVINV
Next L
B(ICOL) = B(ICOL) * PIVINV
For LL = 1 To N
If LL <> ICOL Then
DUM = A(LL, ICOL)
A(LL, ICOL) = 0#
For L = 1 To N
A(LL, L) = A(LL, L) - A(ICOL, L) * DUM
Next L
B(LL) = B(LL) - B(ICOL) * DUM
End If
Next LL
Next i
For L = N To 1 Step -1
If INDXR(L) <> INDXC(L) Then
For k = 1 To N
DUM = A(k, INDXR(L))
A(k, INDXR(L)) = A(k, INDXC(L))
A(k, INDXC(L)) = DUM
Next k
End If
Next L
2:
End Sub
6 楼
wdkshp [专家分:5490] 发布于 2008-01-31 09:55:00
楼主,代码我看的眼花,你能不能讲你要完成的任务说得清楚一点,别人才好更容易阅读你的代码
7 楼
myjianghe [专家分:470] 发布于 2008-01-31 12:21:00
fff是函数,dfff是对 fff的导函数,GAUSSJ是用来计算fff/dfff的,它们都被semic_location函数调用
8 楼
merry05 [专家分:8920] 发布于 2008-01-31 12:45:00
这简直是数学题,我编程没什么关系
我来回复