我编了一个求有6个未知数的线性方程组的小程序,在对01.txt求解时可以得到正确的解,但是对02.txt求解时却得不到正确的解。请问应该如何修改程序?谢谢!
注:01.txt和02.txt为线性方程组的增广矩阵。
程序代码如下:
Private Sub Form_Load()
'***********读取数据文件*************
CommonDialog1.ShowOpen
Dim files As String, i As Integer, j As Integer, L() As Single
files = CommonDialog1.FileName
Dim inbuf As String
Open files For Input As #1 '打开文件
num = 0 'num为文件行数
Do While Not EOF(1) '获取文件行数
Line Input #1, inbuf
num = num + 1
Loop
Close #1
ReDim L(1 To num + 1, 1 To 7) '矩阵
Open files For Input As #1
For i = 1 To num '将数据赋值给数组L
For j = 1 To 7
Input #1, L(i, j)
Next j
Next i
Close #1
Open files For Input As #1
Close #1
Dim L1 As Single, L2 As Single, j1 As Integer, j2 As Integer, k1 As Integer, k2 As Integer
Dim x1 As Single, x2 As Single, x3 As Single, x4 As Single, x5 As Single, x6 As Single
For i = 1 To num - 1
For j1 = i + 1 To num
If L(j1, i) >= L(i, i) Then
For k1 = i To 7
L(num + 1, k1) = L(i, k1)
L(i, k1) = L(j1, k1)
L(j1, k1) = L(num + 1, k1)
Next k1
Else
End If
Next j1
L1 = L(i, i)
For j2 = i + 1 To num
L2 = L(j2, i)
For k2 = i To 7
L(i, k2) = L(i, k2) / L1
L(j2, k2) = L(j2, k2) - L(i, k2) * L2
Next k2
Next j2
Next i
x6 = L(6, 7) / L(6, 6)
x5 = L(5, 7) - x6 * L(5, 6)
x4 = L(4, 7) - x6 * L(4, 6) - x5 * L(4, 5)
x3 = L(3, 7) - x6 * L(3, 6) - x5 * L(3, 5) - x4 * L(3, 4)
x2 = L(2, 7) - x6 * L(2, 6) - x5 * L(2, 5) - x4 * L(2, 4) - x3 * L(2, 3)
x1 = L(1, 7) - x6 * L(1, 6) - x5 * L(1, 5) - x4 * L(1, 4) - x3 * L(1, 3) - x2 * L(1, 2)
Text1.Text = x1
Text2.Text = x2
Text3.Text = x3
Text4.Text = x4
Text5.Text = x5
Text6.Text = x6
Text7 = x1 + 2 * x2 + 3 * x3 + 4 * x4 + 5 * x5 + 6 * x6
End Sub
[此贴子已经被作者于2007-6-5 13:26:30编辑过]