[求助]高斯列主元解方程组的算法
我用高斯列主元解方程组,下面是代码,数组矩阵为a(n,n),b(n),可算出来的结果就是不对啊,我不知道哪里出现问题了,请大侠帮我看看吧!Dim a(n, n) As Double '矩阵
Dim b(n) As Double 'b 项矩阵
Dim x(n) As Double '输出结果矩阵
Dim k As Integer = 0 '中间变量,假设计算到第k步
Dim i As Integer '数组下标变量
Dim j As Integer '数组下标变量
Dim z As Integer = 0 '回代时中间变量
Dim max_i As Integer '找矩阵列主元下标
Dim mida As Double '交换矩阵中间变量
Dim midb As Double '交换b项中间变量
Dim l(n, n) As Double '消元比列系数
For k = 0 To n - 1 '(1)消元过程
For j = k To n '(2)找列主元
If a(max_i, k) < a(j, k) Then
max_i = j
End If
Next
If a(max_i, k) = 0 Then '(3)如果列主元为0,则退出循环
Exit For
End If
If max_i <> k Then '(4)判断是否需要换行
For j = k To n
mida = a(k, j)
a(k, j) = a(i, j)
a(i, j) = mida
midb = b(k)
b(k) = b(i)
b(i) = midb
Next
End If
For i = k + 1 To n
l(i, k) = a(i, k) / a(k, k) '比例系数
For j = k + 1 To n
a(i, k) = a(i, k) - l(i, k) * a(k, j)
Next
b(i) = b(i) - l(i, k) * b(k)
Next
If a(n, n) = 0 Then
Exit For
End If '结束循环过程
Next
x(n) = b(n) / a(n, n) '回代求解
For i = n - 1 To 0 - 1
For j = i + 1 To n
z = z + a(i, j) * x(j)
Next
x(i) = (b(i) - z) / a(i, i)
Next