多元一次方程组的求解

求解多元一次方程组:

f(1,1)x1+f(1,2)x2+f(1,3)x3+...=f(1,n)

f(2,1)x1+f(2,2)x2+f(2,3)x3+...=f(2,n)

f(3,1)x1+f(3,2)x2+f(3,3)x3+...=f(3,n)

......

f(m,1)x1+f(m,2)x2+f(m,3)x3+...=f(m,n)

采用高斯消元法:

Option Base 1
Function Determinant(ByRef factor) As Single
Dim i As Long, j As Long, k As Long, row As Long, order As Long
Dim r As Long, c As Long, Pivot As Single, Pivot2 As Single, temp() As Single
Determinant = 1
Dim m

m = factor
row = UBound(m, 1)
If Not UBound(m, 2) = row + 1 Then MsgBox "无解或不定解!": Exit Function
ReDim temp(1 To row)

For i = 1 To row

Pivot = 0
For j = i To row
For k = i To row
If Abs(m(k, j)) > Pivot Then
Pivot = Abs(m(k, j))
r = k: c = j
End If
Next k
Next j

If Pivot = 0 Then Determinant = 0: Exit Function

If r <> i Then
order = order + 1
For j = 1 To row
temp(j) = m(i, j)
m(i, j) = m(r, j)
m(r, j) = temp(j)
Next j
End If

If c <> i Then
order = order + 1
For j = 1 To row
temp(j) = m(j, i)
m(j, i) = m(j, c)
m(j, c) = temp(j)
Next j
End If

Pivot = m(i, i)
Determinant = Determinant * Pivot

For j = i + 1 To row
Pivot2 = m(j, i)
If Pivot2 <> 0 Then
For k = 1 To row
m(j, k) = m(j, k) - m(i, k) * Pivot2 / Pivot
Next
End If
Next

Next

Determinant = Determinant * (-1) ^ order
End Function

Sub getresult(ByRef factor(), ByRef answer As String)
Dim row As Integer, i As Integer, D0 As Single
Dim m
Dim result() As String
row = UBound(factor, 1)
ReDim result(1 To row)
D0 = Determinant(factor)
If D0 = 0 Then MsgBox "无解!": Exit Sub
For i = 1 To row
m = factor
For j = 1 To row
m(j, i) = factor(j, row + 1)
Next
result(i) = "X" & i & "= " & Format(Determinant(m) / D0, "0.00") ' Di/D0
Next
answer = Join(result, vbCrLf)
End Sub

Private Sub Command1_Click()
Dim Param(3, 4) ' 三元一次方程组
Dim i As Integer
For i = 1 To 4
Param(1, i) = Choose(i, 1, 1, 1, 6) ' x1+x2+x3=6
Param(2, i) = Choose(i, 2, -1, 3, 5) ' 2x1-x2+3x3=5
Param(3, i) = Choose(i, 4, 2, -3, 3) '4x1+2x2-3x3=3
Next
Dim answer As String
getresult Param, answer
Debug.Print answer

End Sub

返回:

X1= 0.83
X2= 3.04
X3= 2.13

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值