发表于:2005-06-13 08:09:00
12楼
不好意思,今天才发给你,前几天比较忙。
'主要是应用了线性代数了,我好久没用了,临时看了一下书了
'因此,耽搁了这么多时间,不好意思啊
Dim A(4, 5) As Double '定义A(n,n+1),因为是四维,n=4
Dim x(4) As Double
Dim ark As Double
Dim flag As Integer
Private Sub Form_Load()
Dim i As Integer
Dim n As Integer
'下面为一示例四维方程组参数
' x1+x2 + x3 + x4 = 10
' x1+2*x2+x3+x4=12
' x1+2*x2-2*x3+x4=3
' x1+x2+2*x3-2*x4=1
n = 4 ' n 为几维方程组
' 下面为数组的参数
A(4, 1) = 1
A(4, 2) = 1
A(4, 3) = 1
A(4, 4) = 1
A(4, 5) = 10
A(3, 1) = 1
A(3, 2) = 2
A(3, 3) = 1
A(3, 4) = 1
A(3, 5) = 12
A(2, 1) = 1
A(2, 2) = 2
A(2, 3) = -2
A(2, 4) = 1
A(2, 5) = 3
A(1, 1) = 1
A(1, 2) = 1
A(1, 3) = 2
A(1, 4) = -2
A(1, 5) = 1
If (cal(4)) Then
MsgBox ("解算错误,参数不对")
End If
Text1.Text = x(1) ' 方程的值
Text2.Text = x(2)
Text3.Text = x(3)
Text4.Text = x(4)
End Sub
Public Function cal(n As Integer) As Integer
Dim r, i, j As Integer
Dim k As Integer
For k = 1 To n - 1
ark = max(n, k)
If ark = 0 Then
cal = 0
Else
If flag <> k Then
Call exchange(n, flag, k)
For i = k + 1 To n
For j = k + 1 To n + 1
A(i, j) = A(i, j) - A(k, j) * A(i, k) / A(k, k)
Next
Next
End If
End If
Next
x(n) = A(n, n + 1) / A(n, n)
Dim me As Double
For k = n - 1 To 1 Step -1
me = 0
For j = k + 1 To n
me = me + A(k, j) * x(j)
Next
x(k) = (A(k, n + 1) - me) / A(k, k)
Next
cal = 0
End Function
Public Function exchange(n As Integer, r As Integer, k As Integer)
' 该函数为交换行的距函数
Dim i As Integer
For i = 1 To n + 1
A(0, i) = A(r, i)
Next
For i = 1 To n + 1
A(r, i) = A(k, i)
Next
For i = 1 To n + 1
A(k, i) = A(0, i)
Next
End Function
Public Function max(n As Integer, k As Integer) As Double
'该函数为比较同一x的参数的大小的函数
Dim i As Integer
max = 0
For i = k To n
If Abs(A(i, k)) <> 0 Then
max = Abs(A(i, k))
flag = i
End If
Next
End Function