loading...
 
Locations of visitors to this page

loading...

今日天气:

我的分类(专题)

loading...

日志更新

loading...

最新评论

loading...

留言板

loading...

搜索

链接

Blog信息

loading...






 

高斯消元法进行多元一次方程组的求解
northwolves 发表于 2006-11-3 20:05:00
 1
1
1
6
2
-1
3
5
4
2
-3
3
 
假设SHEET1中,A1:D3(上面的示例数据) 的数据代表一个 三元一次方程组:
   x1+x2+x3=6
   2x1-x2+3x3=5
   4x1+2x2-3x3=3
 
求x1,x2,x3
 
 
 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(ByVal r As Range, Optional ByRef answer As String)
  Dim row     As Integer, i       As Integer, D0       As Single
  Dim m
  Dim factor
  Dim result()     As String
  factor = r
  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
   
  Sub solver()
  Dim answer     As String
  getresult [a1:d3], answer
  MsgBox answer, 0, "答案"
  End Sub

阅读全文 | 回复(2) | 引用通告 | 编辑
 


Re:高斯消元法进行多元一次方程组的求解
mingmig(游客)发表评论于2007-7-29 16:07:35
我想问一下有没有简单点的编程,就是用比较基础的方法坐这个题目

个人主页 | 引用 | 返回 | 删除 | 回复
 


Re:高斯消元法进行多元一次方程组的求解
ychh_wy发表评论于2007-6-7 20:07:46
请问狼老师:有没有关于解决0-1整数规划(多元一次方程)的方案或者工具?
本来excel工具——规划求解具备这一功能,可是数据量大了,计算速度太慢了
我的数据特点为:
1.方程等式的左边:20-200个常数、有正数和负数、可能有1位或2位小数、整数部分可能达到8位数
2.方程的右边:是常数(正数或者负数)
如有好点子:请与我联系 ychh_wy@yahoo.com.cn

个人主页 | 引用 | 返回 | 删除 | 回复
 


发表评论:

    大名:
    密码:
    主页:
    标题:
    loading...





Google
 
Web northwolves.blog.excelhome.net

© COPYRIGHT 2006 ALL RIGHTS RESERVED loading...

 
Powered by Oblog.