VB解方程组
如下:X1,X2,X3;与Y1,Y2,Y3这些已知,求解a,b,c
用代码解,谢谢
Y1 = a * X1 * 2 + b * X1 + c
Y2 = a * X2 * 2 + b * X2 + c
Y3 = a * X3 * 2 + b * X3 + c --------------------编程问答--------------------
--------------------编程问答-------------------- 当X1=0 ,X2=1 ,X3=2
Option Explicit
'解N元一次方程式
'Arr 中保存的是N元一次方程的矩阵
'如:
' 3X+4Y+5Z=26
' 6X+7Y+9Z=47
' 4X+8Y+6Z=38
'则 Arr 的数据是:
'Arr(1 to 3 ,1 to 4)
'Arr(1,1)=3 Arr(1,2)=4 Arr(1,3)=5 Arr(1,4)=26
'Arr(2,1)=6 Arr(2,2)=7 Arr(2,3)=9 Arr(2,4)=47
'Arr(3,1)=4 Arr(3,2)=8 Arr(3,3)=6 Arr(3,4)=38
Private Function Formulasolu(ByVal Arr, ByVal n As Long) As Double()
Dim i As Long
Dim j As Long
Dim dbTop As Double
Dim dbButtom As Double
Dim Index As Long
For Index = 1 To n
For i = 1 To n
If i <> Index Then
dbTop = Arr(Index, Index)
dbButtom = Arr(i, Index)
For j = 1 To n + 1
Arr(i, j) = Arr(i, j) * dbTop - Arr(Index, j) * dbButtom
Next
End If
Next
Next
For i = 1 To n
Arr(i, n + 1) = Arr(i, n + 1) / Arr(i, i)
Arr(i, i) = 1
Next
Dim Formul() As Double
ReDim Formul(n) As Double
For i = 1 To 3
Formul(i) = Arr(i, n + 1)
Next
Formulasolu = Formul
End Function
Private Sub Command1_Click()
Dim Arr(1 To 3, 1 To 4)
Arr(1, 1) = 3
Arr(1, 2) = 4
Arr(1, 3) = 5
Arr(1, 4) = 26
Arr(2, 1) = 6
Arr(2, 2) = 7
Arr(2, 3) = 9
Arr(2, 4) = 47
Arr(3, 1) = 4
Arr(3, 2) = 8
Arr(3, 3) = 6
Arr(3, 4) = 38
Dim solu() As Double
solu = Formulasolu(Arr, 3)
Dim i As Long
For i = 1 To UBound(solu)
MsgBox "第" & i & "个解是:" & solu(i)
Next
End Sub
Y1=770,Y2=251,Y3=808
出现溢出 --------------------编程问答-------------------- 从算法上讲,利用行列式即可解方程。
补充:VB , 基础类